Pulling "Certain Numbers" from textbox

Getting certain numbers from textbox and displaying them as "magi

Page 1 of 1

8 Replies - 2697 Views - Last Post: 31 January 2008 - 01:10 AM Rate Topic: -----

#1 kkgaming  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 07-February 07

Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 10:37 PM

I'm struggling to pull numbers out of a textbox (my chosen number is 11). So the user will enter a number lets say, that number will then go through an if statement tell me if its == to 11 if it is it will display a line saying "11 is a secret number" will be printed out. If its not 11 it will just post the number...which I have working if I take out the if statement. I also have some portion the name part of the assignment (ignore that) I figured that out and it works. Its just the number thing that I can't get working. Do I have to parse a string to int? First day with C#, I know a little in C (I would know how to do it in C). I just don't get how you get the .Text file to work in the if statment. Because you could do blah.Text == var. Having var be 11. But there is a conflict. Any help would be great.


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Problem2 : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
	   //This is for the name portion of the program  
		
		if (Session["NameInTextBox"] != null)
			Label1.Text = Session["NameInTextBox"].ToString();

		else
			Label1.Text = "not Registered";

	  //Start of number portion of assignment

		if (Session["NumberInTextBox1"] != null)
			Label5.Text = Session["NumberInTextBox1"].ToString();
			
		else
			Label5.Text = "Enter a number!";


		
		
	   
	}
	protected void Button1_Click(object sender, EventArgs e)
	{
		Session["NumberInTextBox1"] = TextBox2.Text;
			   int var1 = 11;
		
				 if (TextBox2.Text == var1)
			Console.WriteLine("This number is a magic number!");
	}
}



This post has been edited by kkgaming: 30 January 2008 - 10:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pulling "Certain Numbers" from textbox

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 11:05 PM

You cannot compare a string to an integer. The Text property is of type string, you need to convert it to type integer first. However, you must make sure there is a number in there otherwise your program will crash.

This is when you want to use a Try/Catch to handle any exceptions.
if (Convert.ToInt32(TextBox2.Text) == var1)

Was This Post Helpful? 0
  • +
  • -

#3 kkgaming  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 07-February 07

Re: Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 11:10 PM

View Postjayman9, on 30 Jan, 2008 - 11:05 PM, said:

You cannot compare a string to an integer. The Text property is of type string, you need to convert it to type integer first. However, you must make sure there is a number in there otherwise your program will crash.

This is when you want to use a Try/Catch to handle any exceptions.
if (Convert.ToInt32(TextBox2.Text) == var1)



Yeah I did something similar int var1 = int.Parse(TextBox2.Text); and got that error you where talking about. Okay I set a 0 in the textbox to start out,...hmm there is no other work around huh.

This post has been edited by kkgaming: 30 January 2008 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 11:14 PM

Set it in the Text property of the TextBox.
Was This Post Helpful? 0
  • +
  • -

#5 kkgaming  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 07-February 07

Re: Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 11:37 PM

View Postjayman9, on 30 Jan, 2008 - 11:14 PM, said:

Set it in the Text property of the TextBox.



if (var1 == num)


labelsecret.Text = "Yes";
else
labelsecret.Text = "NO";


can I put num1 and num2, num3, num4 in the same if. Because when I use multiple ifs it doesnt work. I need 5 secret numbers

This post has been edited by kkgaming: 30 January 2008 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 11:41 PM

You could also use the TryParse Method, something along the lines of


private int numToCheck;
private bool isInt;

isInt = Integer.TryParse(TextBox2.Text,numToCheck);
if(isInt == true)
{
	  if(numToCheck == val)
	  {
			//Do something here
	   }
	   else
	   {
			//Do something else here
		}
}
else
{
	 //Do your error message here
}



What TryParse does is it attempts to convert the value to an integer, if its successful then the value is assigned to the variable numToCheck, otherwise it returns false. The main difference between TryParse and Parse is TryParse doesn't throw an exception if the conversion fails.

Assigning the results of the TryParse to a bool variable lets you know if the conversion failed or not without having to catch an exception if the conversion fails like you do with Parse, and if it succeeds then the second value in the signature of TryParse is assigned the int value of the textbox value.

Happy Coding! :)
Was This Post Helpful? 0
  • +
  • -

#7 kkgaming  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 07-February 07

Re: Pulling "Certain Numbers" from textbox

Posted 30 January 2008 - 11:54 PM

View PostPsychoCoder, on 30 Jan, 2008 - 11:41 PM, said:

You could also use the TryParse Method, something along the lines of


private int numToCheck;
private bool isInt;

isInt = Integer.TryParse(TextBox2.Text,numToCheck);
if(isInt == true)
{
	  if(numToCheck == val)
	  {
			//Do something here
	   }
	   else
	   {
			//Do something else here
		}
}
else
{
	 //Do your error message here
}



What TryParse does is it attempts to convert the value to an integer, if its successful then the value is assigned to the variable numToCheck, otherwise it returns false. The main difference between TryParse and Parse is TryParse doesn't throw an exception if the conversion fails.

Assigning the results of the TryParse to a bool variable lets you know if the conversion failed or not without having to catch an exception if the conversion fails like you do with Parse, and if it succeeds then the second value in the signature of TryParse is assigned the int value of the textbox value.

Happy Coding! :)



Hey thanks man, I got it to working already though. Your ways seems better though, alittle more straight forward. Thanks for the help from both of you!

This post has been edited by kkgaming: 30 January 2008 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 kkgaming  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 07-February 07

Re: Pulling "Certain Numbers" from textbox

Posted 31 January 2008 - 12:00 AM

One quick question. When I enter a number and hit my button it takes two clicks to have it show up on my label. However my secret number thing (with the if) updates on the first click to either yes or no. Any reason why that would be. I guess it would be kind of hard to pin point though for you guys since you cant see all the code etc. Just thought I'd ask.

This post has been edited by kkgaming: 31 January 2008 - 12:01 AM

Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Pulling "Certain Numbers" from textbox

Posted 31 January 2008 - 01:10 AM

Why don't you post your code and we will be able to give you an answer. Without the code, we would be just guessing. Use :code: tags.

I had forgotten about TryParse since I usually already have a try/catch in place. It is very quick to add another catch. Anyway, good point PsychoCoder, thanks for reminding me of it. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1