CS1003 Error

arimakidd

New member
Joined
Oct 5, 2011
Messages
2
Programming Experience
1-3
Why am I getting the following compile error:
C#:
 error CS1003: Syntax error, ':' expected

It's a pretty straight forward gui form. Here is the code I am using?
C#:
using System;
using System.Windows.Forms;


public class FrmMain
{
    TextBox txt1 = new TextBox();
    Button btn1 = new Button();
    Label lblMsg = new Label();
    
    public FrmMain()
    {
        Form frmMain = new Form();
        frmMain.Text = "Simple GUI 1 Demo";
        frmMain.StartPosition = FormStartPosition.CenterScreen;
        frmMain.Closed += new EventHandler(frmMain_Closed);
        
        txt1.Location = new System.Drawing.Point(10,10);
        
        btn1.Text = "Go!";
        btn1.Location = new System.Drawing.Point(150,10);
        btn1.Click += new EventHandler(btn1_Click);
        
        lblMsg.Text = "System Message Here";
        lblMsg.AutoSize = true;
        lblMsg.Location = new System.Drawing.Point(10,50);
        
        frmMain.Controls.Add(txt1);
        frmMain.Controls.Add(btn1);
        frmMain.Controls.Add(lblMsg);
        
        frmMain.Show();
        //Application.Run();
        
    }
    
    private static void frmMain_Closed(object sender, System.EventArgs e)
    {
        Application.Exit();
    }
    private static void btn1_Click(object s, System.EventArgs e)
    {
        lblMsg.Text = (txt1.Text.Equals(String.Empty))? "Enter some text";
    }
}
What's my error?
 
Last edited:

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
903
Location
Norway
Programming Experience
10+
static void btn1_Click
You have declared the method static, read about static for example here: static (C# Reference)
Basically a static method can not access instance members of a class, for that it would need an object reference of the class.
Why would you declare an event handler for an object instance static?
error CS1003: Syntax error, ':' expected
which is given by code line
lblMsg.Text = (txt1.Text.Equals(String.Empty))? "Enter some text";
is expecting the second part of the ?: operator in conditional expression, see ?: Operator (C# Reference)
?? operator would have been more convenient code-wise, but unfortunately TextBox.Text property returns an empty string when not set and not a null reference.
 

arimakidd

New member
Joined
Oct 5, 2011
Messages
2
Programming Experience
1-3
Thanks

Yes, you are right and it works now. I have to remove static and finish off the rest of the ternary condition. The test application works now.
 
Top Bottom