Results 1 to 4 of 4

Thread: If statement problem

Hybrid View

  1. #1
    Addicted Member Mr.Joker's Avatar
    Join Date
    Apr 12
    Posts
    140

    If statement problem

    Ok. I have one button and one textbox. Now, user should enter how old he is. I want to make statement that if he is older then 13 some messagebox will popup. Problem is that with VB i know how to do that but in C# its not working on that way. Here is the code I made:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int[] num = {1,2,3,4,5,6,7,8,9,10,11,12,13 };
                for (int i = 0 ; i<=13;i++) {
                    if (textBox1.Text > num[i].ToString())
                    {
                        MessageBox.Show("You are enough old to join us.");
                    }
                }
    
            }
        }
    }
    What can be the problem?

  2. #2
    Fanatic Member
    Join Date
    Feb 00
    Location
    Dunmow,Essex,England
    Posts
    892

    Re: If statement problem

    you need : if (Convert.ToInt32(textBox1.Text) > num[i]) Though why you need an Array and loop I dont know. Just do if (Convert.ToInt32(textBox1.Text) > 13)

  3. #3
    Addicted Member Mr.Joker's Avatar
    Join Date
    Apr 12
    Posts
    140

    Re: If statement problem

    For practice. Learning loops and arrays, thats why

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: If statement problem

    I would use Integer.Parse instead as it's directly optimized for string input. Although instead of looping through each value, if you really desire to use an array here (which keep in mind, is definitely unnecessary and convoluted), then you could just check whether the parsed TextBox Text value is contained within the array, instead of looping and checking for comparable values. Which is also slower.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •