Results 1 to 9 of 9

Thread: need some correction of syntax

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: need some correction of syntax

    gep13 has already tackled that and there will be no need for

    Code:
    string[] grade ={ "Pass", "Merit", "Fail" };
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width