Casting question

V

Valerie Hough

I have a class that contains an ArrayList object. All items in the arrayList
are of the same type, but that type depends on how the class is initialized.
I would like multiple public methods (with the same name) for retrieving my
objects WITHOUT having to have the caller cast them, i.e.

public OBJECT_TYPE1 GetAt( int arrayIndex )
{
return( (OBJECT_TYPE1) arrayList[ arrayIndex ] );
}

public OBJECT_TYPE2 GetAt( int arrayIndex )
{
return( (OBJECT_TYPE2) arrayList[ arrayIndex ] );
}

But this causes a compiler error because the the parameter lists are
identical.

I could do this in C++ - why does the C# compiler not allow this code? Is
there some way to do what I want? It may seem like a trivial problem, but it
is a nuisance to have to cast return values constantly and frequently causes
code to 'overflow' to another line.

Thanks in advance.
 
J

Jeff Louie

Valerie In 2.0 you can use generics. In 1.1 you can extend from
System.Collections.CollectionBase. Given a reusable class
NullSafeCollection,
the code is fairly trivial

http://www.geocities.com/jeff_louie/OOP/oop7.htm

class DrawableCollection : NullSafeCollection
{
// Custom implementations of the protected members of
IList
// returns -1 if parameter is null
public int Add(Drawable value)
{
return base.Add(value);
}
public void Insert(int index, Drawable value)
{
base.Insert(index, value);
}
// provide an indexer
public new Drawable this[int index]
{
get
{
//throws ArgumentOutOfRangeException
return (Drawable)base[index];
}
set
{
//throws ArgumentOutOfRangeException
base.Insert(index,value);
}
}
}
// usage
DrawableCollection dc= new DrawableCollection();
dc.Add(new Circle());

Given a generic WrapNullCollection class that wraps a NullSafeCollection
in a
read only wrapper, you can create a read only class in one line of code.

WrapNullCollection wrapDrawable= new WrapNullCollection(dc);

Regards,
Jeff
I have a class that contains an ArrayList object. All items in the
arrayList
are of the same type, but that type depends on how the class is
initialized.
I would like multiple public methods (with the same name) for retrieving
my
objects WITHOUT having to have the caller cast them,
 
J

Jon Skeet [C# MVP]

Valerie Hough said:
I have a class that contains an ArrayList object. All items in the arrayList
are of the same type, but that type depends on how the class is initialized.
I would like multiple public methods (with the same name) for retrieving my
objects WITHOUT having to have the caller cast them, i.e.

public OBJECT_TYPE1 GetAt( int arrayIndex )
{
return( (OBJECT_TYPE1) arrayList[ arrayIndex ] );
}

public OBJECT_TYPE2 GetAt( int arrayIndex )
{
return( (OBJECT_TYPE2) arrayList[ arrayIndex ] );
}

But this causes a compiler error because the the parameter lists are
identical.

I could do this in C++ - why does the C# compiler not allow this code? Is
there some way to do what I want? It may seem like a trivial problem, but it
is a nuisance to have to cast return values constantly and frequently causes
code to 'overflow' to another line.

Are you sure you could do the above in C++? I was under the impression
that C++, like C#, didn't consider the return type part of the method
signature.
 
N

NeoNet

I have a class that contains an ArrayList object. All items in the arrayList
are of the same type, but that type depends on how the class is initialized.
I would like multiple public methods (with the same name) for retrieving my
objects WITHOUT having to have the caller cast them, i.e.

public OBJECT_TYPE1 GetAt( int arrayIndex )
{
return( (OBJECT_TYPE1) arrayList[ arrayIndex ] );
}

public OBJECT_TYPE2 GetAt( int arrayIndex )
{
return( (OBJECT_TYPE2) arrayList[ arrayIndex ] );
}

But this causes a compiler error because the the parameter lists are
identical.

I hope the compiler still work this way in future :)

C# need typized objects, so it need to know how these objects looks like
(fortunately).

It seems you need inheritance or interface implementation, so you can do
something like:

interface IMyObjectType {...}

class OBJECT_TYPE1 : IMyObjectType {...}
class OBJECT_TYPE2 : IMyObjectType {...}

public IMyObjectType GetAt( int arrayIndex )
{
return (IMyObjectType)arrayList[ arrayIndex ];
}

// ... somewhere
IMyObjectType myGenericObject = MyClass.GetAt(0);
OBJECT_TYPE1 myObject1 = (OBJECT_TYPE1)MyClass.GetAt(1);
OBJECT_TYPE2 myObject2 = (OBJECT_TYPE2)MyClass.GetAt(2);

hth
 
A

Adam Clauss

Jon Skeet said:
Are you sure you could do the above in C++? I was under the impression
that C++, like C#, didn't consider the return type part of the method
signature.

Jon's right - this is NOT valid C++ and could not be done. The name of the
method (or a parameter) would have to be different.
 

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