What's wrong with this code?

B

Brian

I'll provide a working example if this isn't some cluelessness on
my part (i.e. someone can spot the issue right away). Can someone
tell me why a cast isn't working below as an index?

Fields is a simple enum.
enum fields {field1,field2,field3};

mydata is an array of MyAbstractDataType. Actual elements of the
array are instances of a derived class.

When I define a integer and assign it f casted to an integer, it
works as an array index with no problems. (first line) If I cast
f to an integer and use that as an array index, it always has a
value of zero.

I'm really puzzled. Thanks.

public Hashtable Data
{
get
{
int x;
Hashtable data = new Hashtable();

foreach (Fields f in Enum.GetValues(typeof(Fields)))
{
x = (int)f;
data.Add(f,mydata[x]); // works
data.Add(f,mydata[((int)f)]); // doesn't work
}
return data;
}
}
 
J

Jon Skeet [C# MVP]

Brian said:
I'll provide a working example if this isn't some cluelessness on
my part (i.e. someone can spot the issue right away). Can someone
tell me why a cast isn't working below as an index?

That certainly seems quite odd. A short but complete example which
demonstrates the problem would definitely be helpful.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

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