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