Hi,
I have a query regarding the struct.
1) Can a method be created in a class with a return type struct? But my return type in the method should be a ArrayList?
Please find the below sample code;
Code Snippet:
The below code is giving me an error.
public Color GetRecentColor()
{
if (colorCollection.Count > 0)
{
return (Color)colorCollection[colorCollection.Count - 1];
}
return null;
}
In the above code, Color is a struct and colorCollection is an ArrayList.
Is there a solution to typecast a struct to ArrayList?
Thanks in advance,
Mayur M
AlanPosted Mar 28, 2008, 5:24 AM
Yes, the return type of a method can be a struct such as System.Drawing.Color.
You don't say what the error is but I suspect it's nothing to do with the ArrayList but is this line instead:
return null;
Unless it's declared as a nullable type, a struct instance always contains some sort of a value and can never be null. So you need to substitute an actual color there. Try:
return Color.Black;
or some other default color that you'd use if your collection was empty.