|
-
Feb 7th, 2009, 12:54 PM
#1
Thread Starter
Frenzied Member
need some correction of syntax
Hi ,
Im switching from vb to c# and have syntax errors in this piece of code, which I couldnt figure out what. Any help pls? thanks
Code:
namespace arraystringcheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string grades;
int mark;
private void button1_Click(object sender, EventArgs e)
{
string[] grade ={ "Pass", "Merit", "Fail" };
foreach (Control c in this.controls)
{
if ((typeof (c is TextBox )) && (textbox.Text != null)
{
if (string m in grade)
{
switch (grades)
{
case "Merit":
mark += 10;
case "Pass":
mark += 5;
case "Fail":
mark = mark;
default:
MessageBox.show ("grade is Invalid");
}
}
}label1.Text= mark.ToString;
}
}
}
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Feb 7th, 2009, 01:10 PM
#2
Re: need some correction of syntax
What is the line
Code:
if (string m in grade)
supposed to do for you? And may we see the VB.Net version?
-
Feb 7th, 2009, 01:16 PM
#3
Re: need some correction of syntax
Hey,
I have had to make a couple assumptions here, namely that m is the Text within the TextBox, but here you go:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CSharpExamples
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string grades;
int mark;
private void button1_Click(object sender, EventArgs e)
{
string[] grade ={ "Pass", "Merit", "Fail" };
foreach (Control c in this.Controls)
{
if (c is TextBox && textBox.Text != null)
{
switch (c.Text)
{
case "Merit":
mark += 10;
break;
case "Pass":
mark += 5;
break;
case "Fail":
break;
default:
MessageBox.Show("grade is Invalid");
break;
}
}
}
label1.Text = mark.ToString();
}
}
}
You need to watch the case of what you are writing, there were a couple of mistakes, i.e. this.controls, rather than this.Controls. C# is case sensitive.
Also, you needed to add a break; for each of your cases.
Let me know if this is not what you want.
Gary
-
Feb 7th, 2009, 01:26 PM
#4
Thread Starter
Frenzied Member
Re: need some correction of syntax
HI ,
Many thanks for your prompt syntax help.
The line
(string m in grade)
means that I want to check the 3 textboxes whether the user has a member of the array :
string[] grade ={ "Pass", "Merit", "Fail" };
otherwise I want to exit the Switch and give MessageShow.(Invalid Grade)
Last edited by angelica; Feb 7th, 2009 at 01:44 PM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Feb 7th, 2009, 01:45 PM
#5
Re: need some correction of syntax
gep13 has already tackled that and there will be no need for
Code:
string[] grade ={ "Pass", "Merit", "Fail" };
-
Feb 7th, 2009, 02:03 PM
#6
Thread Starter
Frenzied Member
Re: need some correction of syntax
Yes Dee-u it works however if anyone of the textboxes does not contain (Fail, Pass or Merit ) it still works out the grade and also gives the (grade is Invalid ). I have attempted the code below however it contains errors!
Any more help pls?
Code:
private void button1_Click(object sender, EventArgs e)
{
string[] grade ={ "Pass", "Merit", "Fail" };
string m;
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Text != null)
{c.Text = c.Text.ToUpper();
if (c.Text in grade)= true
switch (c.Text)
{
case "MERIT":
mark += 10;
break;
case "PASS":
mark += 5;
break;
case "FAIL":
break;
}
else
MessageBox.Show("grade is Invalid");
}
}
label1.Text = mark.ToString();
}
}
}
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Feb 7th, 2009, 02:09 PM
#7
Re: need some correction of syntax
Hey,
Why have you removed the default case?
i.e. when it doesn't match any of the three, it should do nothing.
Also, you don't need this, it is irrelevant:
Code:
if (c.Text in grade)= true
You do this check is the case statement.
Gary
-
Feb 9th, 2009, 04:50 PM
#8
Thread Starter
Frenzied Member
Re: need some correction of syntax
ok, thanks guys
now I have extended the coded to include :
if (c is TextBox && c.Text = null) MessageBox.Show("Textbox cannot be left blank");
however on the red line it says I cant check a bool and string .
How does the correct syntax go in C# pls?
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Feb 9th, 2009, 04:52 PM
#9
Re: need some correction of syntax
Hey,
In order to check for equality in C#, you have to use == not =.
Therefore, what you should have is:
Code:
if (c is TextBox && c.Text == string.Empty)
{
MessageBox.Show("Textbox cannot be left blank");
}
Hope that helps
Gary
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
|