|
Pass it around as a parameter to a constructor or method, or as a property:
class Program
{
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection("YourConnectionString");
ClassA a = new ClassA(connection);
a.DoStuff();
ClassB b = new ClassB(connection);
b.DoStuff();
ClassC c = new ClassC();
c.DoStuff(connection);
ClassD d = new ClassD();
d.ConnectionForD = connection;
d.DoStuff();
}
}
public class ClassA
{
private SqlConnection _sharedConnectionInA;
public ClassA(SqlConnection connection)
{
_sharedConnectionInA = connection;
}
public void DoStuff()
{
_sharedConnectionInA.Open();
}
}
public class ClassB
{
private SqlConnection _sharedConnectionInB;
public ClassB(SqlConnection connection)
{
_sharedConnectionInB = connection;
}
public void DoStuff()
{
_sharedConnectionInB.Open();
}
}
public class ClassC
{
public void DoStuff(SqlConnection connectionForC)
{
connectionForC.Open();
}
}
public class ClassD
{
private SqlConnection _sharedConnectionInD;
public SqlConnection ConnectionForD
{
get
{
return _sharedConnectionInD;
}
set
{
_sharedConnectionInD = value;
}
}
public void DoStuff()
{
_sharedConnectionInD.Open();
}
}
Simon
|
|
|
|
|
public class X
{
public readonly SqlCommand Command ;
public X
(
string ConnectionString
)
{
Command = ( new SqlConnection ( ConnectionString ) ).CreateCommand() ;
return ;
}
}
There, that wasn't so hard was it? (You can get the Connection from the Command.)
I would not leave the connection open constantly.
See also this[^] outstanding article.
|
|
|
|
|
I want to ask if someone has already have an article or working algorithm where it can get all the combination possible in a given word.
Example: "ABCD"
possible combinations or arrangement without duplicates:
"ABCD"
"ABC"
"ABD"
"BCD"
"AB"
"AC"
"AD"
"BC"
"CA" <--- is the same as "AC"
"CBA" <--- is the same as "ABC" or "BCA"
"BD"
|
|
|
|
|
You could do it recursively.
All combinations of "ABCD" can be calculated as
"A",
and all combinations of "BCD",
and all combinations of "BCD" with an "A" added at the beginning.
All combinations of "BCD" is:
"B"
and all combinations of "CD"
and all combinations of "CD" with a B added at the beginning
All combinations of "CD" is:
"C"
and all combinations of "D"
and all combinations of "D" with a C added at the beginning.
All combinations of "D" is:
"D"
Pass back up.
All combinations of "CD" is:
"C" and "D" and "CD".
Pass back up.
All combinations of "BCD" is:
"B" and "C" and "D" and "CD" and "BC" and "BD" and "BCD".
Pass back up.
all Combinations of "ABCD" is:
"A" and "B" and "C" and "D" and "CD" and "BC" and "BD" and "BCD" and "AB" and "AC" and "AD" and "ACD" and "ABC" and "ABD" and "ABCD".
And there is your answer:
"A"
"B"
"C"
"D"
"AB"
"AC"
"AD"
"BC"
"BD"
"CD"
"ABC"
"ABD"
"ACD"
"BCD"
"ABCD"
Of course recursion probably isn't the best way. It would be easier with a for loop, but everyone knows recursion is more fun.
Simon
|
|
|
|
|
That is easy by using recursion. Combine each letter with all combinations of the letters after it:
All combinations in "ABCD" is:
Combine "A" with each of all combinations in "BCD".
Combine "A" with each of the letters in "BCD".
Combine "B" with each of all combinations in "CD".
Combine "B" with each of the letters in "CD".
Combine "C" with each of the letters in "D".
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Where's the problem? I have thre listboxes. For example, the first one has 23 image paths, the second one has 7 imagepaths. First i check if any of them are the same then i add them to the third lsitbox. Then i want to create pictureboxes depending of the number of items in the third listbox.
Here's the code of the function:
void prikaz(int stevilo)<br />
{<br />
PictureBox[] picBOX = new PictureBox[stevilo];<br />
CheckBox[] chkBOX = new CheckBox[stevilo];<br />
<br />
for(int i = 0; i<lb1.items.count;> {<br />
Image a;<br />
Image b;<br />
<br />
a = System.Drawing.Image.FromFile(@txtNacrti.Text + @"\" + lb1.Items[i].ToString());<br />
<br />
picBOX[i] = new PictureBox();<br />
picBOX[i].Size = new Size(100, 100);<br />
picBOX[i].Name = "picc" + i.ToString();<br />
picBOX[i].Location = new System.Drawing.Point(picX, picY);<br />
picBOX[i].BorderStyle = BorderStyle.FixedSingle;<br />
picBOX[i].ImageLocation = @txtNacrti.Text + @"\" + lb1.Items[i].ToString();<br />
<br />
chkBOX[i] = new CheckBox();<br />
chkBOX[i].AutoSize = true;<br />
chkBOX[i].Name = "chk" + i.ToString();<br />
chkBOX[i].Location = new System.Drawing.Point(chkX, chkY);<br />
chkBOX[i].Text = lb1.Items[i].ToString();<br />
<br />
chkY = chkY + 106;<br />
picY = picY + 106;<br />
}<br />
}
Regards,
Matjaž
modified on Thursday, November 27, 2008 8:17 AM
|
|
|
|
|
Matjaž Grahek wrote: Where's the problem?
Yes, can you please tell us what the problem is?
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Nothing happens, thats the problem.
Regards,
Matjaž
|
|
|
|
|
You are creating Controls, but I do not see you adding them to any container.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Hmm... how does that go?
Regards,
Matjaž
|
|
|
|
|
me.controls.add('yourcontrol')
or if in panel
panelname.controls.add('yourcontrol')
or ...
any container control should have the controls methode use that to add them
|
|
|
|
|
yeah OR this.Controls.Add(control)
Thanks to u too
Regards,
Matjaž
|
|
|
|
|
OK, Just got it Thanks!
Regards,
Matjaž
|
|
|
|
|
When this is all the code that runs, then the controls won't belong to any parent (form or group box or something) - and consequently will not show up on the screen, since they are not part of any container control. There must be something like Form.Controls.Add(pic/checkBox[i]) for every control.
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
I need to find all the visible windows of an other application or childs windows of a window extern to my application.
I'm not a native api expert , i searched a few but it's not an easy task because the 'window' keyword is too generic and procuces a lot of results.
Can someone put me on the right way please?
|
|
|
|
|
|
I read articles on Reflection still can't figure out a practical use for it?
Why dynamical invocation? I can see many practical application for "late-binding"/polymorphism but reflection ...
Perhaps NUnit? Test fixtures are not know until at runtime user specify dll to test... is NUnit built using reflection? Can somebody gives more practical application of reflection?
Thanks
dev
|
|
|
|
|
Plugins. Scripting. Dynamically binding to different versions of libraries.
I recently wrote a bunch of wrapper classes that used reflection to load a library assembly and call methods. It would try to load several different versions of the assembly in preference order.
Simon
|
|
|
|
|
most apps I seen before dont need to load dll dynamically... so I can't really quote another example of practical use except NUnit
that said, attributed programming (declare webmethod for instance) relies on reflection - but could that not be done with inheritance? (but say C# only allows single inheritance)
dev
|
|
|
|
|
Just because most apps don't use it doesn't mean that it's not useful.
Yes, I'd agree about 90% of the stuff I write doesn't use reflection at all. There are some things however that reflection is useful for. Plugins is a common example where dynamically loading assemblies is often used.
Simon
|
|
|
|
|
Many a DAL is built with reflection
|
|
|
|
|
DAL - data abstraction layer? ... dont think Hibernate relies on that...
dev
|
|
|
|
|
I don't know about Hibernate (the Java version), but NHibernate (its .NET clone) does heavily rely on reflection.
So do many other advanced tools like as for example Reflector, NCover or NDepend. Addmittedly, the mentioned are used for code analysis rather than programming itself.
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
devvvy wrote: DAL - data abstraction layer?
Yup
devvvy wrote: dont think Hibernate relies on that...
I dunno about NHibernate, but there are more custom layers out there.
With reflection you can 'ask' a class whether it contains a specific property or method. Combine this with the dynamic loading of an assembly, and querying what classes the assembly got - you could load your RichEdit (or any other control) dynamic from an assembly that the user selects (during runtime) and manipulate it using reflection.
Hope this helps,
|
|
|
|
|
thanks guys, just trying to stretch my mind so I learn reflection beyond theories
I read about nhibernate seems like it does use reflection to determine properties type at runtime - this seems to be a pattern that's opposite to that implemented by NUnit.
In NUnit - framework relies on reflection to trigger methods/test cases from dynamically looaded DLL.
In NHibernate - seems like while application and nhibernate dll are linked compile time - but NHibernate dll still need to find out object/field types in runtime because the information is not know apriori..
don't know reflection well enuf to see what other application there is (dynamic load Rich Edit ... hum... why'd you want to let user to dynamically load a RichEdit control?)
dev
|
|
|
|