Question A number As String ??

jaybee33

Member
Joined
Mar 12, 2009
Messages
9
Location
fife, dunfermline
Programming Experience
Beginner
hey, this is my first post here, but last night i was writing some code for my first program, i dont know how this worked, but i put a number in my program, but when i clicked the final button to display my results "as string" it still showeed the number?? here is the code i wrote, is relativley simple, but the theory doesnt make scence to me :D

Basicly its a box with 3 lables, (first name, surname and age), 3 text boxes (for the user to input this info) and a button at the bottom called "display in message box" so when i click the button, it displays the users input in the form as a message box.

here is the code, (no screen shots sorry, im at work atm)
VB.NET:
Dim firstname As String
Dim surname As String
Dim age As Integer
Dim fullname As String
Dim Complete As String

firstname = textbox1.text
surname = textbox2.text
age = howold.text
fullname = firstname & " " & surname
complete = fullname & " " & age

msgbox (complete)

==========================

what i dont get is if i said to VB that vaiable "Complete" was STRING then howcome it still displays the AGE ..... which is an Integer (a number) not a string????

thanks guys.



John
 
Since it is not determined that the user is writing a number in the Textbox you can use Integer.TryParse to try and parse the text to an integer value. Better yet use a NumericUpDown control where user can only input/select the number.
 
Turn Option Explicit and Option Strict "On". With Option Strict on you would have received an error telling you to explicitly convert this back and forth to the proper datatype. With it off, VS is trying to do the conversions on its own.
 
Turn Option Explicit and Option Strict "On". With Option Strict on you would have received an error telling you to explicitly convert this back and forth to the proper datatype. With it off, VS is trying to do the conversions on its own.
It's not trying to do the conversion on it's own it is doing the conversion on it's own. This is the difference between implicit (VS does the conversion with a best guess) and explicit (doesn't compile unless the casting is explicitly done)
 
Back
Top