Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Parsing [Intermediate Level Tutorial]

C# Parsing


What Is It:
In C# "Parsing" is basically analyzing something and attempting to convert it.

Getting Started:
Start by creating a new textBox and Button. What we want to accomplish here is popping up a MessageBox that shows the value of your integer. However, we cannot just show the integer value, we must parse it!

First, create a new integer and set it equal to your textbox's text.

Code
int test = textBox1.Text;

Now, this will not work! This is because we are attempting to set a number variable equal to text. So, in order for this to work, we need to parse it.

Code
int test = int.Parse(textBox1.Text);

int.Parse(textBox1.Text)
All this does is parse the text inside of the textbox into an integer value, pretty easy, huh!

Showing The Number
Code
MessageBox.Show(test.ToString());
This was explained in my previous tutorial.

Spoiler (Click to View)


Code:
Code
private void button1_Click(object sender, EventArgs e)
        {
            int test = int.Parse(textBox1.Text);

            MessageBox.Show(test.ToString());
        }
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
A more advanced version would be

Convert.ToInt32(textbox1.Text);
(Oct 20, 2010 07:42 PM)ImSpare Wrote:  A more advanced version would be

Convert.ToInt32(textbox1.Text);

Thanks for the feedback! As I'm sure you can tell, I am catering to beginners.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
(Oct 20, 2010 07:42 PM)ImSpare Wrote:  A more advanced version would be

Convert.ToInt32(textbox1.Text);

Code
int ToInt(object obj)
{
return Convert.ToInt32(obj);
}

Code
int i = ToInt(textBox1.text)

Easier to keep track if your dealing with alot of code.
(Oct 20, 2010 08:02 PM)xSilent Wrote:  
Code
int ToInt(object obj)
{
return Convert.ToInt32(obj);
}

Code
int i = ToInt(textBox1.text)

Easier to keep track if your dealing with alot of code.

Thanks a lot! Hopefully the people learning will take a look at all of our snippets.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[.NET] by outlawNov 28, 2020 04:25 AM 12 367 Nov 28, 2020 04:25 AM
Last Post: Rmdy
[C#] by DeadShot32Sep 15, 2019 02:36 AM 3 187 Sep 15, 2019 02:36 AM
Last Post: riu41776