Someone can explain this???

  • Thread starter Thread starter Anatoly
  • Start date Start date
A

Anatoly

Hi. I wonder if someone could explain why this code gives different results
in VB.NET and C#:

C#

public enum myEnum{
One = 1,
Two = 2
}

public void Foo(){
MessageBox.Show(Convert.ToString(myEnum.One));
}

The result of Foo is message 'One'

VB.NET

public enum myEnum
One = 1
Two = 2
end enum

public sub Foo()
MessageBox.Show(Convert.ToString(myEnum.One))
end sub

The result of Foo is message '1'

Thanks
 
It's for a very technical reason, which is: VB is rubbish.

Hope that helps.






(PS I'm kidding)
 
Anatoly said:
Hi. I wonder if someone could explain why this code gives different results
in VB.NET and C#:

<snip>

Well, I'm not an expert, but I believe that in the VB case you're
ending up calling Convert.ToString(int) whereas in C# you're ending up
calling Convert.ToString(object) which is doing the appropriate
converting.

As for why each of them choose those particular methods to call, you'll
need to look in the language reference for each language.

If you cast myEnum.One to object, you get the same result in VB as you
do by default in C#.
 
Anatoly,
In addition to Jon's comments.

Why not use:
MessageBox.Show(myEnum.One.ToString())

Which for converting to strings seems much easier than getting the Convert
class involved...

Note the above displays 'One'.

Hope this helps
Jay
 
Back
Top