You must Sign In to post a response.

How Convert String to integer- vb.net

Hi,
please I need your help.
I want to convert a string that is in textBox1.text, which is= "1+2+3" into integer value given result 6.
I tried to convert it to integer but what actually happened is that it's only taking the first value, which is 1 and convert it to integer.
can you please help me?

Private Sub Sum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sum.Click
TextBox1.Text = TextBox1.Text & "+"

Answer = Val(TextBox1.Text)
Answer += Val(TextBox1.Text)
FinalResult = Answer

thnx,


Comments

Author: friend4ever26 Feb 2008 Member Level: Silver   Points : 2

COnvert.ToInt32(TextBox1.Text)

Author: Dharmaraj Nagarajan26 Feb 2008 Member Level: Gold   Points : 2

Hi,
First declare a string and from that take the contents one by one and assign to another string and then you carry on your process.
By,
Dharma

Author: Mohan Kumar26 Feb 2008 Member Level: Gold   Points : 2

hi,

use Convert.ToInt()

-Mohan

Author: Giribabu26 Feb 2008 Member Level: Gold   Points : 2

I you are calculating sum using single text box by giving multiple value, then first you need to split the string and then you have convert it to int then calculate, you do like this. before doing calculation you need to validate the string which is entered in the textbox. check below code


private void button2_Click(object sender, EventArgs e)
{
int sum = 0;
string[] tempValue = textBox3.Text.Split('+');
for( int i = 0; i < tempValue.Length; i++ ) {
sum += int.Parse(tempValue[i].ToString());
}
MessageBox.Show(sum.ToString());
}

Author: Jawaher26 Feb 2008 Member Level: Silver   Points : 2

Thanks for your reply, but it didn't work because I think I have an addition sign within the string. This is my first time dealing with VB.Net n I'm tryin to build a calculator.. Is there other solution plz

Author: Jawaher26 Feb 2008 Member Level: Silver   Points : 2

Thanks for your reply, but it didn't work because I think I have an addition sign within the string. This is my first time dealing with VB.Net n I'm tryin to build a calculator.. Is there other solution plz

Author: friend4ever26 Feb 2008 Member Level: Silver   Points : 2

Dim i, x As Integer
Dim y As Integer
Dim Str As String = TextBox1.Text

For y = 0 To Str.Length - 1

Dim sum As String = Str.Substring(y, 1)
x = Convert.ToInt32(sum)
i = i + x


Next

Author: Victoria26 Feb 2008 Member Level: Gold   Points : 2

Dim s As String
s = TextBox1.Text
Dim str(10) As String
Dim number(10) As Integer
Dim sum As Integer
sum = 0


str = Split(s, "+")
Dim i As Integer
For i = 0 To str.Length - 1
number(i) = CInt(str(i))
sum += number(i)


Next
MessageBox.Show(sum)