0

This code comes from the .aspx.cs file for a Web User Control the code below gives two errors: the first { under public void Button2_Click says: "} expected". then, the last } in the code says "type or namespace definition, or end-of file expected". how can I make this code better? and how can I actually make it function?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tutorial_Examples_1
{
public partial class UserInfoBoxControl : System.Web.UI.UserControl
{
    public void Page_Load(object sender, EventArgs e)
    {

    }

    public void Button2_Click(object sender, EventArgs e)
    {
        private string userName;
        private int userAge;
        private string userCountry;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public int UserAge
        {
            get { return userAge; }
            set { userAge = value; }
        }

        public string UserCountry
        {
            get { return userCountry; }
            set { userCountry = value; }
        }            
    }
}
}
1
  • This looks a lot like a homework assignment... Because there are a lot of mistakes/errors. Commented Oct 25, 2016 at 23:27

2 Answers 2

1

You're nesting member variables and properties within a method body. This is not allowed in C#. That should be done outside that block, and within the class body instead.

Sign up to request clarification or add additional context in comments.

1 Comment

And this should be a comment, not an answer.
1
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tutorial_Examples_1
{
public partial class UserInfoBoxControl : System.Web.UI.UserControl
{

private string userName;
private int userAge;
private string userCountry;

public string UserName
{
     get { return userName; }
     set { userName = value; }
}

public int UserAge
{
     get { return userAge; }
     set { userAge = value; }
}

public string UserCountry
{
     get { return userCountry; }
     set { userCountry = value; }
} 

public void Page_Load(object sender, EventArgs e)
{

}

public void Button2_Click(object sender, EventArgs e)
{

}

}//class


}//namespace

Better way is to create a separate class for User like

public class UserInfo
{
private string userName;
private int userAge;
private string userCountry;

public string UserName
{
     get { return userName; }
     set { userName = value; }
}

public int UserAge
{
     get { return userAge; }
     set { userAge = value; }
}

public string UserCountry
{
     get { return userCountry; }
     set { userCountry = value; }
} 

}


public partial class UserInfoBoxControl : System.Web.UI.UserControl
{

    public void Page_Load(object sender, EventArgs e)
    {

    }

    public void Button2_Click(object sender, EventArgs e)
    {
        UserInfo user = new UserInfo();
    //Do you stuff with user object
    }
}

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.