Search code examples
c#classtextbox

Value from TextBox into Class C#


I'm new to c# so maybe I'm wondering a stupid question. I'm trying to get a value from a textBoxFirstname. and get the value into ClassRegistrate.

The reason for this is because I have a Stored Procedure that will put the values that the User writes in textboxUsername into a sql table. But is there any way to check if the value that the user enters in the textBoxUsername enters the classRegistrate?

this is how it looks in my btnRegistrate_click event:

ClassRegistrate registrate = new ClassRegistrate(); 
   
private void btnRegistrate_Click(object sender, EventArgs e)
    {
        
        registrate.FName = textBoxUsername.Text;
    }

And this is what my class look like:

public class ClassRegistrate
{
    private string fname;

    public string FName
    {
        get { return fname; }
        set { fname = value; }
    }
}

why wont it get the value from my textBox? thanks for the help


Solution

  • // Define the class ClassRegistrate outside a method 
    // so that it can be accessed by other methods
    // Therefore it has a bigger scope
    private ClassRegistrate registrate2 { get; set; }
    
    private void startBtn_Click(object sender, EventArgs e)
    {
        //ClassRegistrate registrate = new ClassRegistrate();
        //This class definition above will only be accessed inside the function
        //Therefore it has a smaller scope which is limited to this function alone
        
        // assign the class that you already created
        registrate2 = new ClassRegistrate();
        registrate2.FName = textBoxUsername.Text;
    
        MessageBox.Show(registrate2.FName);
    }
    
    public class ClassRegistrate
    {
        private string fname;
    
        public string FName
        {
            get { return fname; }
            set { fname = value; }
        }
        
        //MessageBox.Show(FName);
        //that is a statement and can only exist/executed inside a method/function
    }
    

    Example:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        // Define the class ClassRegistrate outside a method 
        // so that it can be accessed by other methods
        private ClassRegistrate registrate { get; set; }
    
        // 1st Button
        private void AssignValue_Click(object sender, EventArgs e)
        {
            registrate = new ClassRegistrate();
            registrate.FName = textBox1.Text;
        }
    
        // 2nd Button
        private void CheckValue_Click(object sender, EventArgs e)
        {
            textBox2.Text = registrate.FName;
            MessageBox.Show(registrate.FName);
        }
    
        public class ClassRegistrate
        {
            private string fname;
            public string FName
            {
                get { return fname; }
                set { fname = value; }
            }
        }
    }
    

    Output:

    Output for checking class property value