MyEnum.ToString() broke on .NET 3.0 machine

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I have a basic Enum:

enum MyEnum
{
Name1,
Name2,
}

And I called MyEnum.ToString(), and this crashed on a machine
running .NET 3.0, twice. But, every other time, it ran fine.
Strange. The error was System.ArgumentException:

Message = "All enums must have an underlying value__ field."
TargetSite = "System.Reflection.FieldInfo GetValueField(System.Type)"
StackTrace = "at System.Enum.GetValueField(Type type)
at System.Enum.ToString()
at ........."

Looks like I'll keep my machines running NET 2.0 for now.

Zytan
 
.NET 3.0 should not handle enumerations any differently, as it is just
additional libraries on top of the .NET 2.0 runtime, so your enumerations
should behave exactly the same.
 
That's what I would think. I thought it was strange that the NET 3.0
code threw an exception. How could anyone possibly screw up
MyEnum.ToString? I wonder what caused this.

Zytan
 
That's what I would think. I thought it was strange that the NET 3.0
code threw an exception. How could anyone possibly screw up
MyEnum.ToString? I wonder what caused this.

Zytan

Name1.ToString(),
Name2.ToString(),
or MyEnum.ToString()

?

~O
 
Name1.ToString(),
Name2.ToString(),
or MyEnum.ToString()
?

Sorry, this is what happened:

enum MyEnum
{
Name1,
Name2,
}

MyEnum myVar;

string s = myVar.ToString(); <--- System.ArgumentException exception
thrown here, but only twice at same time (two programs were running)
on the same computer running .NET 3.0, and it's never happened before
or since.

Message = "All enums must have an underlying value__ field"

Zytan
 
enum MyEnum
{
Name1,
Name2, // wrong: the last item must not have a comma
}

MyEnum myVar;

// I think here you need to init myVar: myVar = MyEnum.Name1;

string s = myVar.ToString();


HTH,
Luigi.
 
enum MyEnum
{
Name1,
Name2, // wrong: the last item must not have a comma
}

Yes it can. Try it!
MyEnum myVar;

// I think here you need to init myVar: myVar = MyEnum.Name1;

string s = myVar.ToString();

Given that the code was failing at execution time rather than compile-
time, I think it's fair to assume that either myVar is an instance
variable, or that Zytan *did* actually initialize it somewhere.
 
enum MyEnum
{
Name1,
Name2, // wrong: the last item must not have a comma
}

As Jon explained, this is ok. It's quite convenient, actually, so you
can add elements or remove them easily, without worry.
MyEnum myVar;

// I think here you need to init myVar: myVar = MyEnum.Name1;

Yes, I do initialize it in my code. Sorry I wasn't exactly verbose, I
am just trying to show the minimal amount of code to show that I am
doing something completely normal, but NET 3.0 blew up in my face,
just twice.

Thanks,

Zytan
 
Given that the code was failing at execution time rather than compile-
time, I think it's fair to assume that either myVar is an instance
variable, or that Zytan *did* actually initialize it somewhere.

Yes, I initialized it. Let me try again:

enum MyEnum
{
Name1,
Name2,
Name3,
}

MyEnum myVar = MyEnum.Name1;

string s = myVar.ToString(); // exception was thrown here, twice, in
NET 3.0, but is not consistent

I think NET 3.0 is rather new, so maybe this will be fixed soon?

Zytan
 
string s = myVar.ToString(); // exception was thrown here, twice, in
NET 3.0, but is not consistent

I think NET 3.0 is rather new, so maybe this will be fixed soon?

..NET 3.0 uses the .NET 2.0 CLR, so this does sound very odd to me. If
you're ever able to reproduce it reliably, do let us know.
 
.NET 3.0 uses the .NET 2.0 CLR, so this does sound very odd to me. If
you're ever able to reproduce it reliably, do let us know.

Certainly. But, I doubt it will happen since I immediately
requested .NET 3.0 to be uninstalled, since it was the only machine we
had that was running .NET 3.0.

I would be inclined to say that it was likely my code at fault, but I
just can't see how that would be possible. Perhaps from some strange
multi-threading bug? If so, I should (eventually) see the same
behaviour on the .NET 2.0 machines.

Zytan
 
Zytan said:
Certainly. But, I doubt it will happen since I immediately
requested .NET 3.0 to be uninstalled, since it was the only machine we
had that was running .NET 3.0.

I would be inclined to say that it was likely my code at fault, but I
just can't see how that would be possible. Perhaps from some strange
multi-threading bug? If so, I should (eventually) see the same
behaviour on the .NET 2.0 machines.

Until it's reproduced, we're unlikely to know - but I very much doubt
that it's anything to do with .NET 3.0 itself.
 
Back
Top