Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one TextBox with a decimal value typed in of "3039".
I want to convert that text to hexadecimal, with six characters (3 Bytes) into a second TextBox or Label.

The below code works:
VB
Dim iValue as Integer = Val(TextBox1.Text)
TextBox2.Text = iValue.ToString("X6")

BUT when I attempt to combine the two lines, the below doesn't work:
VB
TextBox2.Text = Val(TextBox1.Text).ToString("X6")

What am I missing?
VB.Net in Visual Studio Express 2013 for Windows Desktop

What I have tried:

Dim iValue as Integer = Val(TextBox1.Text)
TextBox2.Text = iValue.ToString("X6")

TextBox2.Text = Val(TextBox1.Text).ToString("X6")
Posted
Updated 17-Mar-17 10:17am

1 solution

And the error you say you're getting is ...... ?

It's better to leave those two lines as separate lines of code for debugging purposes. There is no need to combine them at all. You're not gaining anything by doing so.

You're also not allowing for any validation of the text in the TextBox to make sure it's even possible to get a valid value from it. Val will stop parsing a string on the first non-numeric character it sees.

Also, Val() is deprecated. It only exists for backwards compatibility with converted VB6 code. Use Integer.TryParse() instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900