|
Good afternoon all,
I'm learning to work with 'Lists'
I've set up a list of 'zooAnimals' and in the list i've added objects as they have been created.
zooAnimals = new List<Animal>();
newcat1 = new Cat(cat1, catPen, fircat);
newcat2 = new Cat(cat2, catPen, seccat);
newtig1 = new Tiger(tiger1, tigerPen, firtig);
newtig2 = new Tiger(tiger2, tigerPen, sectig);
zooAnimals.Add(newcat1);
zooAnimals.Add(newcat2);
zooAnimals.Add(newtig1);
zooAnimals.Add(newtig2);
I would like to now step through the list to perform certain actions when a button is pressed.
For example - the form has a number of buttons on it - 1 being for individual groups of animals to eat().
I tried with
foreach (Cat thisCat in zooAnimals)
{
if (thisCat.hunger == 7)
{
thisCat.hunger = thisCat.hunger - 5;
}
}
but this code throws an InvalidCast Exception......
Unable to cast object of type 'Zoo_Animals.Tiger' to type 'Zoo_Animals.Cat'.
Is there a way of stepping through the list to do this ?
Thanks in advance !
Neil
|
|
|
|
|
If you're using .NET 3.5 you can use
foreach(Cat thisCat in zooAnimals.OfType<Cat>())
{
...
}
otherwise you'll have to check each item
foreach(Animal animal in zooAnimals)
{
if(animal is Cat)
{
...
}
}
|
|
|
|
|
Thank you very much indeed - worked a treat !
|
|
|
|
|
J4amieC wrote: foreach(Cat thisCat in zooAnimals.OfType<cat>())
That's neat - and my trigger to start looking at 3.5 finally...
Knowledge is hereditary, it will find its way up or down.
Luc Pattyn
and since what every time when i want to add button to this control one add two times
posted in C# forum
|
|
|
|
|
Yeah, didn't know about the OfType extension too, looks really useful in this situation.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi
I have done a project on .Net using SQL compact edition ...once i take the build i am getting the Exception Memory corrupted ..Do any one have idea regarding this...Can anyone help me out
|
|
|
|
|
Hi,
I've a problem using VC++ 6.0 dlls in C#.net, I'm using dllimport
dlls consists of classes and I want to call functions contained in classes but they are not static functions.
I tried this code
[DllImport("XYZ.dll", EntryPoint = "?XYZ@XYZ@@XYZ")]
public static extern int XYZ();
when I tried to call XYZ function I got this exception:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
How can I do this?
thanks in Advance,
Akram
|
|
|
|
|
I was able to send mail via simple smtp server
the problem is that as i understood gmail needs ssl authentication.
this is my code but it fails with the following message :
530 5.7.0 Must issue a STARTTLS command first
btw this project is done in vs2003 so im using only System.Net.Sockets and I cant use System.Net.Mail
mailSubject = ModifyMailContent(mailSubject);
mailBody = ModifyMailContent(mailBody);
int code;
using(TcpClient smtpSocket = new TcpClient(smtpServer, mailServerPort))
using(NetworkStream ns = smtpSocket.GetStream())
{
code = GetSmtpResponse(ReadBuffer(ns));
WriteBuffer(ns, "ehlo\r\n");
string buffer = ReadBuffer(ns);
code = GetSmtpResponse(buffer);
if(buffer.IndexOf("AUTH=LOGIN") >= 0)
{
WriteBuffer(ns, "auth login\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
WriteBuffer(ns, System.Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username)) + "\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
WriteBuffer(ns, System.Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(password)) + "\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
}
WriteBuffer(ns, "mail from: <" + mailSender + ">\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
string[] splitRecipient = mailRecipient.Split(',');
foreach(string sEmailTo in splitRecipient)
{
WriteBuffer(ns, "rcpt to:<" + sEmailTo + ">\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
}
WriteBuffer(ns, "data\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
WriteBuffer(ns, "from:<" + mailSender + ">\r\n");
foreach(string sEmailTo in splitRecipient)
{
WriteBuffer(ns, "to:<" + sEmailTo + ">\r\n");
}
WriteBuffer(ns, "Subject:" + mailSubject + "\r\n");
WriteBuffer(ns, "\r\n" + mailBody + "\r\n.\r\n");
code = GetSmtpResponse(ReadBuffer(ns));
WriteBuffer(ns, "quit\r\n");
}
|
|
|
|
|
Hi Guys,
does anyone know how to add a trackbar into a menustrip?
Many thanks,
Phil
|
|
|
|
|
lane0p2 wrote: how to add a trackbar into a menustrip?
I didn't get you . can you be more specific ?
Thanks in advance !!
cheers,
Abhijit
|
|
|
|
|
Hi,
sorry for being vague.
if you add a menuStrip with the standard items, say within the Tools button you add another item called Opactiy, once you've clicked on this anther menu opens but this is a trackBar. so you could change the opacity by sliding the trackbar.
does this make sense?
Many thanks,
Phil
|
|
|
|
|
Sorry !!! I am still Confused . Can you please give any link of some screenshots.
cheers,
Abhijit
|
|
|
|
|
I need to aquire the bitrate(kps) for wmv files without the use of the media player, attempts to Parsing an ASF Stream in C#, C++ and VB have failed, including the examples/articles post on his site. My C++ is not that great but the example provided within the VS documentation will not compile.
Any assistance appreciated.
dotman1
|
|
|
|
|
hi. i was able to successfully place my table on the crystal report viewer however, I have too many columns that are crowded in. I want to make them fit on the page and make the text visible. How do I do that? Some column names also have quite long labels like, "Registered Address" or "Registered City Code". How do you make this visible and fit in one page. Say I have 12 columns?
|
|
|
|
|
Throw some columns out, use a very small font, change the layout..?
|
|
|
|
|
I already have set the font size to 7 but still its the same. They overlap. I can't omit the other columns because they are required information. For the layout, how do you set that? Are you referring to the list of layouts of the crystal report like "Drop Table?"
|
|
|
|
|
Hey,
I now this i kind of a broad question, if not completly trivial, but how would one program a c sharp application that would only need to open a sqlconnection once. For example:
SqlConnection mySqlConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=cache;Integrated Security=SSPI;");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlConnection.Open();
This works fine and all but i can only access the Sql command inside that class and have to open a new sql connection if i am to use this command again... Hope this is explain good enough...
|
|
|
|
|
You do exactly that, Create the SQL connection only once, and pass the object around as a parameter to wherever you need to use it.
Or you could use the singleton pattern[^] to make it available globally (but this would be considered quite bad practise)
SQL server actually pools it's connections internally, so you will find that even if your app creates a new connection every time it needs data, SQL server will most likely be reusing the same connection every time, so if it's performance you are worried about, don't worry, it's probably already being taken care of.
Simon
|
|
|
|
|
Ok, so how would i for example make use of my "mySqlCommand" command in another class?
modified on Thursday, November 27, 2008 9:24 AM
|
|
|
|
|
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
|
|
|
|