string and integer

  • Thread starter Thread starter poldoj
  • Start date Start date
P

poldoj

Hi all, I have a simple question. I must adding the content of a textbox,
that is actually a string to a variable that his datatype is an integer.
Example, I declare a public integer variable then I retrive a textbox.text
value that is a string but is a number.(hold_text=textbox.text) hold text is
"5".
Then I must add 5 to my public variable.

public hold_sum as integer
 
Poldoj,

Strange, this should work
public hold_sum as integer
hold_sum += Cint(textbox.text)

I hope this helps,

Cor
 
Try: hold_sum += Int32.Parse (textbox.Text)

If the textbox text contains non-numeric characters, exception will be
thrown.

Hi all, I have a simple question. I must adding the content of a textbox,
that is actually a string to a variable that his datatype is an integer.
Example, I declare a public integer variable then I retrive a textbox.text
value that is a string but is a number.(hold_text=textbox.text) hold text is
"5".
Then I must add 5 to my public variable.

public hold_sum as integer
 
Hi,

If you know that the Text in the textbox contains an integer value then you
can do the following:

Public varx as integer
varx = DirectCast(MyTextBox.Text,System.Integer)

I would recommend validating the textbox first to check it contains/accepts
an integer: which can be done in many ways.
 
Sorry I missed the addition part of the question

Public varx as integer
varx += DirectCast(MyTextBox.Text,System.Integer)
 
I tried hold_sum += Int32.Parse (textbox.Text)
and it worked!
Thank you so much
I will try also the directcast function ^_^
 
Back
Top