# [.net] Converting a fucntion from VB to C#

This topic is 4182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i was converting a project from VB to c# (i don't know VB and very new to C# (it has been 4 days:)..but i am a good C/C++programmer) i saw an assignment statement which is passin a argument to a function in VB ...
Private Function TirnakKontrol(ByVal str As String) As String
If InStr(str, "'") > 0 Then
Dim lenVal As Integer
Dim i As Integer
lenVal = Len(str)
TirnakKontrol = ""
For i = 1 To lenVal
TirnakKontrol = TirnakKontrol & Mid(str, i, 1)
If Mid(str, i, 1) = "'" Then
TirnakKontrol = TirnakKontrol & "'"
End If
Next i
Return TirnakKontrol
Else
Return str
End If
End Function


is TirnakKontrol = TirnakKontrol & Mid(str, i, 1) means (in the code above) TirnakKontrol ( TirnakKontrol(str.Substring(i,1)) ); (in c#) and return TirnakKontrol means actually return TirnakKontrol("") ; ?? there was no argument how VB takes these Function Names (in C++ they are adress of functions or in other words,literals for function pointers) .can anyone tell me about VB's this attitude?

##### Share on other sites
Visual Basic has a really icky method for returning values from functions. Whenever you define a Function (as opposed to a Sub), you get a "magic" variable with the same name as the function itself.

In this case, there is a magic variable named TirnakKontrol that exists within the scope of that function. When the function exits (by any means other than a Return), the value of the magic variable is used as the return value of the function. This is kept mainly as a backwards-compatibility hack with previous flavours of Basic (Visual Basic and others). However, it is generally very horribly bad style, and you should use Return instead of magic variables.

I'd suggest modifying the VB code first to eliminate the magic variable - just declare a new string variable like ReturnValue inside the If block. Then it should be much more clear how to translate the code to C#.

##### Share on other sites
is this really true? what a unneccessary property of VB

now as i understood from what you've said TirnakKontrol is actually a string variable (because function returns a string) and if we (for example) rename it as StrReturnValue ...
TirnakKontrol = "" means StrReturnValue="";

TirnakKontrol = TirnakKontrol & Mid(str, i, 1) means

StrReturnValue=StrReturnValue+str.Substring(i,1)

and if myfunction is not returning a value but the execution of function finished
then (in VB ) it will return TirnakKontrol...(i got error while i were converting another Function from vb to C# .it said that "not all control paths return a value
" ..probably that function was there returnin a magic-hidden parameter (in VB code) )

You mean that .i didn't understand wrong .did i?

one more thing is this rule also applies to PROPERTY keyword..for example in the property exmple below ..is cValue a magical predefined variable
Public Property cValue(ByVal FieldName As String)        Get            Dim i As Integer            For i = 0 To ColCount - 1                If FieldArray(i) = FieldName Then                    If IsDBNull(ValueArray(i)) Then                        Return ""                    Else                        Return ValueArray(i)                    End If                End If            Next            cValue = ""            MsgBox(FieldName & " is wrong !")        End Get        Set(ByVal Value)            Dim i As Integer            For i = 0 To ColCount - 1                If FieldArray(i) = FieldName Then                    ValueArray(i) = Value                    Exit Property                End If            Next            MsgBox(FieldName & " is wrong !")        End Set    End Property

[Edited by - tonymontana on September 8, 2006 8:08:00 AM]

##### Share on other sites
You might be able to throw it through the Online Code Converter and get some results in C#.