Jump to content

My first code troubleshooting in CodeCall :)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Demodex

Demodex

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
hi fellas.

im execising myself with little apps I make in c#.

because im at a low-level experience and knowldge im trying to assimilate the things i DO know...

so im making an app that the user enters a number and the prog tell it if its odd or even (very simple)..

as youll see in the code below..the odd and even stuff were no problem really..im not that n00b. but the exception thing is what bugs me the most.

what if the user enters nothing? or better yet a string instead of an int?

here is the code:

            /****************************************

             * convert the user input to an integer *

             *     for calculation purposes         *

             ****************************************/


            int a = Convert.ToInt32(tbinput.Text);

            string b = tbinput.Text;


            /******************************************

             * making an "if" statement the checks if *

             * there is a remainder to the number the *

             *           user has inputted            *

             ******************************************/


            

            if (b.Length < 1)

            {

                MessageBox.Show("mistake");

            }   

            else if (a % 2 == 0)

            {

                tbresult.Text = tbinput.Text + " is an even number"; //if number is even show it in textbox

            }

            else if (a % 2 != 0)

            {

                tbresult.Text = tbinput.Text + " is an odd number"; //if not, then it is odd. show it in textbox

            }

you can see the var called "b".
i made it to use the input.tb.text's length property..
i thought id declare this:

else if (b.text.length == 0)

{

messagebox.show("you must type something");

}
but its not working!!
the funny thing is if i declare this:

else if (b.text.length != 0)

{

messagebox.show("bla bla");

}
the messagebox DOES pop..

in clueless with what's wrong here :S

and to think in sometime im gonna start thinkin of building more complicated apps :)

#2
chandru8

chandru8

    Newbie

  • Members
  • Pip
  • 4 posts
you cannot use a integer like b.text.length == 0,try like convert.ToString(b.text.length ) == 0

#3
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Have you tried using exception handling? it's useful for stopping users from entering something like characters in a number field. You could add something like this:

try
{
  int a = Convert.ToInt32(tbinput.Text);
}

catch (FormatException e)
{
  tbinput.Text = "";
  MessageBox.Show("Enter a number!");
  // MessageBox.Show(e.ToString()); <-- That way you can view the exception error, but I wouldn't show a user that.
}

You can have multiple catch blocks one after another. There are a lot more exceptions you can use as well. This is probably the best way to tackle your problem, because your form will still continue.

#4
Demodex

Demodex

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
nop.

furthremore if i used this suggested method it wont let me run it (cant implement operand == on int and string)..

when i tried my syntax it let me run the prog but it just wont work as it should :S

what do u think?
thnx for the help btw, appreciated :)

#5
Demodex

Demodex

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
njr1489,

the thing i want to focus on is having the application recognize whether the user:
typed a string or didnt type anything.

in my app i added this a few minutes after starting this thread:
            }
            catch
            {
                MessageBox.Show("There was an error processing your request.");
            }

which is similar to what you suggested.

i know that now i kinda solved it..but its not enough for my pedant nature :D