Forums » .NET » .NET »

How Convert String to integer- vb.net


Posted Date: 26 Feb 2008      Posted By:: Jawaher     Member Level: Silver    Member Rank: 0     Points: 2   Responses: 8



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,




Responses

#194858    Author: friend4ever      Member Level: Silver      Member Rank: 0     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     Points: 2

COnvert.ToInt32(TextBox1.Text)

 
#194860    Author: Dharmaraj Nagarajan        Member Level: Gold      Member Rank: 17     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     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

All is well.
Thanks,
Dharma
Editor,Mentor,MVM
Try and fail but don't fail and try


 
#194864    Author: Mohan Kumar      Member Level: Gold      Member Rank: 71     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     Points: 2

hi,

use Convert.ToInt()

-Mohan





 
#194867    Author: Giribabu      Member Level: Gold      Member Rank: 343     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     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());
}


--Giri
http://giribabu.wordpress.com


 
#194871    Author: Jawaher      Member Level: Silver      Member Rank: 0     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     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

 
#194876    Author: Jawaher      Member Level: Silver      Member Rank: 0     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     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

 
#194879    Author: friend4ever      Member Level: Silver      Member Rank: 0     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     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


 
#194889    Author: Victoria      Member Level: Gold      Member Rank: 355     Date: 26/Feb/2008   Rating: 2 out of 52 out of 5     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)


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Return to Discussion Forum
Start new thread

Subscribe to Subscribers
Awards & Gifts
Talk to Webmaster Tony John

Online Members

Priya jain
More...
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India