Hack Forums
[C#] C# Parsing [Intermediate Level Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# Parsing [Intermediate Level Tutorial] (/showthread.php?tid=755182)



C# Parsing [Intermediate Level Tutorial] - Pulser - Oct 20, 2010

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());
        }



* - ImSpare - Oct 20, 2010

A more advanced version would be

Convert.ToInt32(textbox1.Text);


* - Pulser - Oct 20, 2010

(Oct 20, 2010 07:42 PM)drewsun 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.


* - xSilent - Oct 20, 2010

(Oct 20, 2010 07:42 PM)drewsun 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.


* - Pulser - Oct 20, 2010

(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.