Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to access textbox details inside a class that relate to another form. The method displayDetails works but if I try and create a similar method with the code that is in btnSubmit_Click and take a similar approach, nothing gets displayed. Basically, is there a way to create a public method in Students to replace the code in btnSubmit_Click - I had been told {get; Set;} was the way to go. Code below:
//Form1.cs - ManageStudentsWin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageStudentsWin
{
    public partial class Form1 : Form
    {
        public int StudentID
        {
            get { return Convert.ToInt16(txtStudentID.Text); }
            set { txtStudentID.Text = value.ToString(); }
        }
        public string StudentFirstName
        {
            get { return txtStudentFirstName.Text; }
            set { txtStudentFirstName.Text = value; }
        }
        public string StudentLastName
        {
            get { return txtStudentSecondName.Text; }
            set { txtStudentSecondName.Text = value; }
        }
        public string StudentCourseCode
        {
            get { return txtCourseCode.Text; }
            set { txtCourseCode.Text = value; }
        }
        public int CurrentYear
        {
            get { return Convert.ToInt16(txtYear.Text); }
            set { txtYear.Text = value.ToString(); }
        }
        public Form1()
        {
            InitializeComponent();
        }


        Students s1 = new Students();
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //Want to call a method instead of having to put code here
            s1.StudentID = StudentID;
            s1.StudentFirstName = StudentFirstName;
            s1.StudentLastName = StudentLastName;
            s1.StudentCourseCode = StudentCourseCode;
            s1.CurrentYear = CurrentYear;
            
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            s1.displayDetails(s1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

//Students.cs - ManageStudentsWin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ManageStudentsWin
{
    class Students
    {
        public string StudentFirstName;
        public int StudentID;
        public string StudentLastName;
        public string StudentCourseCode;
        public int CurrentYear;
        
       
        public void displayDetails(Students s1)
        {
            MessageBox.Show("Your ID is: " + StudentID + "\nYour First Name is: " + StudentFirstName + "\nYour Last Name is: " + StudentLastName + "\nYour Course Code is: " + StudentCourseCode + "\nYour year is: " + CurrentYear);
        }
    }
   
}
Posted
Updated 18-Sep-12 7:37am
v3
Comments
[no name] 18-Sep-12 13:22pm    
Disregard previous. If I understand you correctly you just want to know if you can create something like public void GetDetails(Form1 frm1) { StudentFirstName = frm1.StudentFirstName; } in your Students class? Then call it like s1.GetDetails(this); in your submit event.
BrianHamilton 18-Sep-12 13:59pm    
Yes that works perfectly - I had been under the impression passing a form to a method was to be avoided, but I assume this is better than making form elements public?
[no name] 18-Sep-12 14:42pm    
There are many ways to do this. This is just one example. Not something that I would do but I chalk that up to you are learning and that was the direction you was headed anyway. You could also pass the values directly to the class instance if you wanted. Passing an instance of s1 to the display details function and not use s1 inside that function is not really neccessary. I would probably use a delegate to pass the data to the other class, if I were to do something like this.
Sergey Alexandrovich Kryukov 18-Sep-12 18:35pm    
I don't have all relevant information but -- look at the way I would advise, in my answer.
Thank you.
--SA

Few things:

1)
In this part of code:
C#
private void btnDisplay_Click(object sender, EventArgs e)
{
    s1.displayDetails(s1);
}

you're calling displayDetails(s1) method for Students class, but inside this procedure you're never use the properties of s1 (StudentId, StudentName and so on...).


2)
Have a look at this part of code:
C#
public partial class Form1 : Form
   {
       public int StudentID
       {
           get { return Convert.ToInt16(txtStudentID.Text); }
           set { txtStudentID.Text = value.ToString(); }
       }

and this one:
C#
class Students
    {
        public int StudentID;


If you want to use StudentID as a property of class Students, the declaration of StudentID should be inside of Students class, not in the Form1. So, move these properties in to right module:
C#
private int iStudentID; //first redeclared variables
public int StudentID(int value)
{
    get { return iStudentID); }
    set { iStudentID = value; }
}

and then:
C#
Students s1 = new Students();
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //Want to call a method instead of having to put code here
            s1.StudentID = Convert.ToInt16(txtStudentID.Text);
            s1.StudentFirstName = txtStudentFirstName.Text;
            s1.StudentLastName = txtStudentLastName.Text;
            s1.StudentCourseCode = txtStudentCourseCode.Text;
            s1.CurrentYear = txtCurrentYear.Text;
        }


3)
Finally, the displayDetails function should looks like:
C#
public void displayDetails()
{
    MessageBox.Show("Your ID is: " + iStudentID.ToString() + "\nYour First Name is: " + sStudentFirstName + "\nYour Last Name is: " + sStudentLastName + "\nYour Course Code is: " + sStudentCourseCode + "\nYour year is: " + iCurrentYear.ToString());
}

where sStudentLastName variable (and other) is a private member of Students class.


Sorry for my bad language...
I hope you'll understand what i mean.
 
Share this answer
 
It's much better the develop such thing starting from the idea of separation of UI from other parts of the code, in particular, from the data a form represents. In the simplest case, you may need to have a data model which is used to populate the UI, and UI can be used to update the data; and the data model can be just an object or an object graph in memory. The data should be primary (because the schema is more likely to be used in other software, but the UI can change; and it's the best to be able to change the ID quickly), so the form should "know" data, but the data should "know" nothing about the UI.

This way, you should not make public (or even internal) and form controls — this is always bad. You can have (again, in the simplest case), the form methods like Populate and Update. You pass a reference to data object (or a node in your object graph) to the form as the argument of such methods, but the form reference is not passed anywhere.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900