What class is the object ??

G

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?
}
 
H

Hans Kesting

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
 
J

Jon Skeet [C# MVP]

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.
 
G

GTi

Jon said:
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.

THAT was my intention ;-)
 
G

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 ?
 
J

Jon Skeet [C# MVP]

GTi said:
OK - now we can try to extend it:

public interface iBasics
{
void Display();
}

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
 
G

GTi

Jon said:
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 :)
 
G

GTi

Jon said:
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'.
 
J

Joanna Carter [TeamB]

"GTi" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
G

GTi

Joanna said:
"GTi" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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

I'm using .NET 2.0 and I unfamiliar with generics.
Would you please update my simple code using generics?
Thanks in advance.
 
G

GTi

Joanna said:
"GTi" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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

That gives me this error message:
System.NullReferenceException: Object reference not set to an instance
of an object.
 
J

Jon Skeet [C# MVP]

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.
 
G

GTi

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

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.
 

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