473,378 Members | 1,438 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,378 software developers and data experts.

What class is the object ??

GTi
I need to figure out what kind of class a object is:

public class ClassA
{
public string Name = null;
}

public class ClassB
{
public string Name = null;
}

public class ClassC
{
public string Name = null;
}

ArrayList List = new ArrayList();
List.Add(new ClassA());
List.Add(new ClassB());
List.Add(new ClassC());
List.Add(new ClassA());

foreach(object obj in List)
{
if(obj == ClassA) { ... } <-------------- HOW?
if(obj == ClassC) { ... } <-------------- HOW?
}

Sep 13 '06 #1
13 1649
I need to figure out what kind of class a object is:
>
public class ClassA
{
public string Name = null;
}

public class ClassB
{
public string Name = null;
}

public class ClassC
{
public string Name = null;
}

ArrayList List = new ArrayList();
List.Add(new ClassA());
List.Add(new ClassB());
List.Add(new ClassC());
List.Add(new ClassA());

foreach(object obj in List)
{
if(obj == ClassA) { ... } <-------------- HOW?
if(obj == ClassC) { ... } <-------------- HOW?
}
if (obj is ClassA) { ... }
Hans Kesting
Sep 13 '06 #2
GTi
Hans Kesting wrote:
>
if (obj is ClassA) { ... }
Hans Kesting
Fundamental c#
:$

Sep 13 '06 #3
GTi <tu****@gmail.comwrote:

<snip>
foreach(object obj in List)
{
if(obj == ClassA) { ... } <-------------- HOW?
if(obj == ClassC) { ... } <-------------- HOW?
}
if (obj is ClassA)
{
....
}
if (obj is ClassC)
{
....
}

Note that *often* (but not always) code like this suggests that you
could do more with polymorphism. Perhaps classes A and C could
implement an interface, one method of which the foreach loop could
call.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 13 '06 #4
GTi
Jon wrote:
GTi <tu****@gmail.comwrote:

<snip>
foreach(object obj in List)
{
if(obj == ClassA) { ... } <-------------- HOW?
if(obj == ClassC) { ... } <-------------- HOW?
}

if (obj is ClassA)
{
...
}
if (obj is ClassC)
{
...
}

Note that *often* (but not always) code like this suggests that you
could do more with polymorphism. Perhaps classes A and C could
implement an interface, one method of which the foreach loop could
call.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
THAT was my intention ;-)

Sep 14 '06 #5
GTi
OK - now we can try to extend it:

public interface iBasics
{
void Display();
}

public class ClassA : iBasics
{
public string Name = null;
public void Display()
{
Console.Writeline("Class A " + Name);
}
}

public class ClassB : iBasics
{
public string Name = null;
public void Display()
{
Console.Writeline("Class B " + Name);
}
}

public class ClassC : iBasics
{
public string Name = null;
public void Display()
{
Console.Writeline("Class C " + Name);
}
}

ArrayList List = new ArrayList();
List.Add(new ClassA());
List.Add(new ClassB());
List.Add(new ClassC());
List.Add(new ClassA());

foreach(object obj in List)
{
obj.Display(); <------What do we need here ????
}

....insteed of using
if( obj is ClassA)
{
ClassA o = (ClassA)obj;
o.Display();
}
if( obj is ClassB)
{
ClassB o = (ClassB)obj;
o.Display();
}
Any hot tips here ?

Sep 15 '06 #6
GTi wrote:
OK - now we can try to extend it:

public interface iBasics
{
void Display();
}
<snip>
foreach(object obj in List)
{
obj.Display(); <------What do we need here ????
}
You just need to change the variable declaration in the foreach loop:

foreach(iBasics obj in List)
{
obj.Display(); <------What do we need here ????
}

Jon

Sep 15 '06 #7
GTi

Jon Skeet [C# MVP] wrote:
GTi wrote:
OK - now we can try to extend it:

public interface iBasics
{
void Display();
}

<snip>
foreach(object obj in List)
{
obj.Display(); <------What do we need here ????
}

You just need to change the variable declaration in the foreach loop:

foreach(iBasics obj in List)
{
obj.Display(); <------What do we need here ????
}

Jon
c# is FUN :)

Sep 15 '06 #8
GTi
Jon Skeet [C# MVP] wrote:
GTi wrote:
OK - now we can try to extend it:

public interface iBasics
{
void Display();
}

<snip>
foreach(object obj in List)
{
obj.Display(); <------What do we need here ????
}

You just need to change the variable declaration in the foreach loop:

foreach(iBasics obj in List)
{
obj.Display(); <------What do we need here ????
}

Jon
Hmm that gives me this error:
System.InvalidCastException: Unable to cast object of type 'ClassA' to
type 'iBasics'.

Sep 15 '06 #9
"GTi" <tu****@gmail.coma écrit dans le message de news:
11**********************@b28g2000cwb.googlegroups. com...

| Hmm that gives me this error:
| System.InvalidCastException: Unable to cast object of type 'ClassA' to
| type 'iBasics'.

In that case try this :

foreach(object obj in List)
{
(obj as IBasics).Display();
}

This is so much simpler in .NET 2.0 using generic lists; then you can have a
List<IBasics>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 15 '06 #10
GTi
Joanna Carter [TeamB] wrote:
"GTi" <tu****@gmail.coma écrit dans le message de news:
11**********************@b28g2000cwb.googlegroups. com...

| Hmm that gives me this error:
| System.InvalidCastException: Unable to cast object of type 'ClassA' to
| type 'iBasics'.

In that case try this :

foreach(object obj in List)
{
(obj as IBasics).Display();
}

This is so much simpler in .NET 2.0 using generic lists; then you can have a
List<IBasics>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
I'm using .NET 2.0 and I unfamiliar with generics.
Would you please update my simple code using generics?
Thanks in advance.

Sep 15 '06 #11
GTi

Joanna Carter [TeamB] wrote:
"GTi" <tu****@gmail.coma écrit dans le message de news:
11**********************@b28g2000cwb.googlegroups. com...

| Hmm that gives me this error:
| System.InvalidCastException: Unable to cast object of type 'ClassA' to
| type 'iBasics'.

In that case try this :

foreach(object obj in List)
{
(obj as IBasics).Display();
}

This is so much simpler in .NET 2.0 using generic lists; then you can have a
List<IBasics>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
That gives me this error message:
System.NullReferenceException: Object reference not set to an instance
of an object.

Sep 15 '06 #12
GTi <tu****@gmail.comwrote:

<snip>
Hmm that gives me this error:
System.InvalidCastException: Unable to cast object of type 'ClassA' to
type 'iBasics'.
That would suggest that you haven't actually made ClassA implement
iBasics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 15 '06 #13
GTi

Jon wrote:
GTi <tu****@gmail.comwrote:

<snip>
Hmm that gives me this error:
System.InvalidCastException: Unable to cast object of type 'ClassA' to
type 'iBasics'.

That would suggest that you haven't actually made ClassA implement
iBasics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
How did u.....
Yes - I'm terrible sorry, In my working complex source I have two
interfaces that have similar names (is now renamed) and ofcourse I have
used the wrong interface.
So your first answer was the perfect answer.

Sep 15 '06 #14

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
3
by: Sathyaish | last post by:
I wanted to practice some Linked List stuff, so I set out to create a linked list. The plan was to create the following: (1) A linked list class in Visual Basic (2) A non-class based linked list...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.