Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# equivalent of Val() function of VB

Status
Not open for further replies.

jonathanlimcy

Programmer
Apr 2, 2008
7
TW
Anyone know the equivalent of Val() function of VB in C#. I tried searching and cannot seems to find it. Please do give me a example or a function. Thanks.
 
What does the val() function do?
I don't know VB very well, but tell me what val() does and maybe i can help you.
 
Ths simple answer is to use the Microsoft.VisualBasic namespace.

JurkMonkey's is the more correct answer.

C
 
But the online conversion tools show different
Code:
    Sub GetValue()
        MessageBox.Show(Val(12332.0))
    End Sub
Code:
public void GetValue() 
{ 
    MessageBox.Show(Conversion.Val(12332.0)); 
}
Who is correct?


________________________________________________________
Zameer Abdulla
Help to find Missing people
 
In classic VB, Val() returns the integer value of a string or floating point data type. In essence the function looks at a value and returns all integers up to the first non-integer value and it ignores leading spaces. Examples of returned values are listed below:
1234 = Val("1234")
1234 = Val(" 1234")
1234 = Val("1234.5")
1234 = Val("1234asdf")
1234 = Val(1234.567)

One other quirk of this function was that non-numeric values always translated to zero rather than throwing an exception. Examples below:
0 = Val("")
0 = Val("asdf")


That being said, there is no core .Net conversion library that will provide you with the same functionality. You will need to reference and use the .Net Visual Basic Library to perform this exact function. This is not considered good form though and I would consider changing your coding constructs to create well formed C# and not a C#/VB Classic hybrid.

Scott Travis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top