#c to vb.net conversion

DekaFlash

Well-known member
Joined
Feb 14, 2006
Messages
117
Programming Experience
1-3
Hi,

I have the following code in #c, which I know nothing about.

btnBack.Text = (backPrice1 <> 0.0) ? backPrice1.ToString()
:
String.Empty()

I am trying to convert the above line into vb.net.

Would the code below be correct?

If
(backPrice1 <> 0.0) Then
btnBack.Text = backPrice1.ToString
Else
btnBack.Text = ""
End If

Thanks,
Boulent
 
Would the code be correct?

That it is. There exist also a counterpart in the IIF function:
VB.NET:
btnBack.Text = IIf(backPrice1 <> 0.0, backPrice1.ToString(), String.Empty())

!Your duplicate thread was deleted, don't double-post.
 
Just incidently, if you want to use an IIF function and you want to aviod using the visualbasic assembly you can write your own. Here's an example using generics....

VB.NET:
Friend Shared Function IIf(Of T)(ByVal Expression As [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=1"]Boolean[/URL], ByVal TruePart As [URL="http://www.vbdotnetforums.com/"]T[/URL], ByVal FalsePart As [URL="http://www.vbdotnetforums.com/"]T[/URL]) As [URL="http://www.vbdotnetforums.com/"]T[/URL]      
If [URL="http://www.vbdotnetforums.com/"]Expression[/URL] 
Then            
Return [URL="http://www.vbdotnetforums.com/"]TruePart[/URL]      
End If      
Return [URL="http://www.vbdotnetforums.com/"]FalsePart[/URL]
End Function
 
that would help if only generics where available in vs 2003

thought it's one of the reasons i want to upgrade to 2005
 
Just note that IIf is a function, unlike the C# ternary operator. To give you an example of the difference, this C# code:
VB.NET:
int myInt = myString == null : 0 ? myString.Length;
will assign the value zero to the myInt variable if the myString variable is a null reference. This VB code:
VB.NET:
Dim myInt As Integer = IIf(myString Is Nothing, 0, myString.Length)
wil crash if myString is a null reference. The reason is that in the C# code myString.Length is never evaluated if the condition is true. In the VB however, myString.Length must be evaluated regardless because its value must be passed to the IIf function as a parameter. Be VERY careful when using the IIf function. Check and double check that this type of scenario cannot arise in your code. It's very easy to forget about this fact so it's crucial that you drum it into your head or just don't use IIf. vis's generic version will have the same issue.
 
Hi,
btnBack.Text = (backPrice1 <> 0.0) ? backPrice1.ToString()
: String.Empty()


The C# line was more likely to be this:
VB.NET:
[SIZE=2]btnBack.Text = (backPrice1 <> 0.0) ? backPrice1.ToString() [/SIZE][SIZE=2]: [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty;[/SIZE]

C# enforces that brackets are used in method calls only, whereas Empty is a property - the compiler would complain at you..

VB is less strict and, as you note in this code:

If (backPrice1 <> 0.0) Then

btnBack.Text = backPrice1.ToString
Else
btnBack.Text = ""
EndIf


though tostring is a method, vb lets you get away with not putting the brackets on.

So why am I telling you this (because it's a little nit-picky)? Because most of the C# translators you find on the net will actually compile the code fragement you paste in, from C# into IL (the binary language of the .net framework) then decompile back into VB.NET

If the code sample cannot be compiled, it cannot be converted :)

-
As the others have noted, there is no direct equivalent for the ternary operator in c-like languages (test?truepart:falsepart) and the best, most safe option is always to expand it out as you have done.

The following documents may assist you in reading future C# code samples:

http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_comparison.html
http://www.codeproject.com/dotnet/vbnet_c__difference.asp
 
Back
Top