开发者

Vb Convert.ToInt32 syntax

What does the following code do in VB?

Convert.ToInt32(rmaValidationCode.ToCharArray().GetValue(0), 16) Mod 2) = 1

note :Pri开发者_JS百科vate rmaValidationCode As String


This is actually slightly tricksy:

Let's pick it apart:

rmaValidationCode.ToCharArray().GetValue(0)

gets the first character in the string. Replacing that expression with c for simplicity:

Convert.ToInt32(c, 16)

is interesting... because there's no overload to Convert.ToInt32(char, int). Instead, the VB compiler inserts an implicit conversion from Char to String and then calls Convert.ToInt32(string, int). In this case it's parsing it as hex. So we've got "parse the first character of the string as a hex digit (after converting that first character back to a string)". Now let's substitute that expression with x:

(x Mod 2) = 1

that just takes the remainder after dividing by 2 and tests for it being 1.

So overall, this code tests whether the first character in the string is 1, 3, 5, 7, 9, B, D or F (case-insensitively). If so, the result is true. If the first character is 0, 2, 4, 6, 8, A, C, or E, the result is false. If the string is null, empty or the first character is none of the ones listed, an exception will be thrown.


Convert.ToInt32() is a function that will convert a data type into a 32 bit integer, or just a common Integer. EX:

Dim example as Integer

Other Example:

Dim Fart as String = "6"
Private Sub Conversion()
    Dim ConvertedFart as Integer = Convert.ToInt32(Fart)
End Sub

This code will create a new Integer ConvertedFart from the String Fart.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜