473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Class Casting with ArrayList ?

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[i];
if (CurrEmplyee.Name == "test")
{
}
}
Regards,
Cybertof.
Nov 15 '05 #1
7 1414
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[i] ).Name == "test" )
{
}
}

hope that helps.

Dan.

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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[i];
if (CurrEmplyee.Name == "test")
{
}
}
Regards,
Cybertof.
Nov 15 '05 #2
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
Nov 15 '05 #3
Daniel & Jon,

Thanks a lot for your advises.

The newsgroup is a great knowledge place !

Regards,
Cybertof.
Nov 15 '05 #4
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
Nov 15 '05 #5
In your 3 versions, which is the best one regarding 'performance' ?
I mean :less memory consumming, less object creation, fastest speed...
Thanks,
Cybertof.

In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...
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")
{
}
}

Nov 15 '05 #6
Cybertof <cy****************@gmx.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...
... 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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: c | last post by:
Hello All, I am struggling with simple casting issue within c# and would really appreciate some insight into where I am going wrong. I have two classes, ClassA and ClassB. Each implements a...
1
by: Joe | last post by:
Hello All: I am writing a function which accepts an ArrayList as its parameter and converts the contents of the ArrayList into an XmlNodeList. The ArrayList will contain one of several...
3
by: Andy Chen | last post by:
Hi, I have a Hashtable, key is string and value is ArrayList. The problem is I cannot cast the value from object to ArrayList. like this: Hashtable ht = new Hashtable(); ArrayList al = new...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
3
by: Trev | last post by:
Hi, I have a series of functions which do the following: ValidateData( args ); //args if of type ArrayList // Work out if the data is valid or not, and work out the type of args - int, string,...
1
by: bjwillykajilly | last post by:
Well, I got an assignment due this morning, so i guess ill end up turning it in a day late eh. anyways. I have a couple problems that I don't know what to do with. the objective is here: Write a...
3
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I am going crayz, i cant get this to work. and i don't know what the problem is. I have this method public ArrayList ResolveData() { ArrayList workingList = new ArrayList();
3
by: jupiter | last post by:
The Collections class is supposed to, among other things, return type safe collections from existing collections with static methods such as .checkedList(). My question is: What is so special...
13
by: looper | last post by:
Hi, i am having a problem as im new to c#. I did an arraylist and store my "uid" and want to put it into a row of pictures pic 1 , pic 2 , pic 3, pic 4 this is my code for putting the "uid"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.