Class Casting with ArrayList ?

C

Cybertof

Hello,

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;
if (CurrEmplyee.Name == "test")
{
}
}


Regards,
Cybertof.
 
D

Daniel Bass

I don't think you can do this in the way that you mean.

The only other option to the one you already have is to have a static method
in your Employee class called

Convert(object obj)

whihc returned an Employee object.

so you'd end up with something like

for (int i = 0;i<MyArrayList.Length;i++)
{
if ( Employee.Convert( MyArrayList ).Name == "test" )
{
}
}

hope that helps.

Dan.

Hello,

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;
if (CurrEmplyee.Name == "test")
{
}
}


Regards,
Cybertof.
 
J

Jon Skeet [C# MVP]

Cybertof said:
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;
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;
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)).Name=="test")
{
}
}
 
C

Cybertof

Daniel & Jon,

Thanks a lot for your advises.

The newsgroup is a great knowledge place !

Regards,
Cybertof.
 
D

Daniel Bass

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.

Cybertof said:
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;
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;
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)).Name=="test")
{
}
}
 
C

Cybertof

In your 3 versions, which is the best one regarding 'performance' ?
I mean :less memory consumming, less object creation, fastest speed...


Thanks,
Cybertof.
 
J

Jon Skeet [C# MVP]

Cybertof said:
In your 3 versions, which is the best one regarding 'performance' ?
I mean :less memory consumming, less object creation, fastest speed...

They're likely to be pretty much identical - but there's an important
thing to note here, which is that you shouldn't worry about that kind
of thing unless it proves to be an issue. Readability is far more
important than performance for most code - chances are it's only going
to be a tiny part of your code which ends up as the bottleneck, so only
think about sacrificing readability for performance when you know
exactly where that is.
 
G

Guinness Mann

... but there's an important thing to note here,
which is that you shouldn't worry about that kind
of thing unless it proves to be an issue.
Readability is far more important than performance
for most code - chances are it's only going to be
a tiny part of your code which ends up as the
bottleneck, so only think about sacrificing
readability for performance when you know
exactly where that is.


Hear! Hear!

-- Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top