|
-
Aug 3rd, 2012, 09:34 AM
#1
Thread Starter
Addicted Member
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?
-
Aug 3rd, 2012, 09:46 AM
#2
Fanatic Member
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)
-
Aug 3rd, 2012, 11:04 AM
#3
Thread Starter
Addicted Member
Re: If statement problem
For practice. Learning loops and arrays, thats why
-
Aug 4th, 2012, 12:20 AM
#4
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.
<<<------------
.NET Programming (2012 - 2018)
®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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|