Jon,
could you give me your opinion on the question I asked today two hours
before this one was posted. ("string or stream")
thanks.
Dan.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Cybertof <cy****************@gmx.net> wrote:
In the code below, is there a shortcut not to use the temp var
CurrEmployee ?
I would like to use something like
"if (Employee)MyArrayList.Name"
but don't know the syntax....
*******
CODE : (MyArrayList is a list of Employee)
*******
Employee CurrEmployee;
for (int i = 0;i<MyArrayList.Length;i++)
{
CurrEmployee = MyArrayList[i];
if (CurrEmplyee.Name == "test")
{
}
}
Well, you could improve the code above by declaring the variable inside
the loop:
for (int i=0; i < MyArrayList.Length; i++)
{
Employee CurrEmployee = (Employee) MyArrayList[i];
if (CurrEmployee.Name=="test")
{
}
}
or, for preference, you could use a foreach loop:
foreach (Employee CurrEmployee in MyArrayList)
{
if (CurrEmployee.Name=="test")
{
}
}
You *could* cast directly, but I wouldn't recommend it (as I reckon the
above is more readable):
for (int i=0; i < MyArrayList.Length; i++)
{
if (((Employee)(MyArrayList[i])).Name=="test")
{
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too