Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to access textbox details inside a class that relate to another form. I now have the code working to an extent using {get; set;} but while the method displayDetails works fine from the class Students, if I try making a public method in the Students class to add the details (by passing the student object as an argument (the code is currently located in Private void btnSubmit_Click) the code will compile but the details will not be displayed in the MessageBox. Basically what I want it the two methods in the Students class (which are simply called by the button click event) so I assume I'm making an idiotic but fixable mistake. Any help greatly appreciated. Revised code is 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)
        {
            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 5:26am
v3
Comments
[no name] 16-Sep-12 9:44am    
It really helps to know what the exception is... There are a couple of ways to do this. Pass the data that you want to this class, pass the form instance, use properties... I suspect that your problem actually arises from the fact that you are creating a new instance of Form1 instead of using the existing instance and so the data that is in Form1 does not exist.
BrianHamilton 16-Sep-12 10:02am    
The error is: An unhandled exception of type 'System.StackOverflowException' occurred in ManageStudentsWin.exe. The line it occurs on is

Form1 details = new Form1();

How do I pass an instance of the form to the class? I had looked on the web but didn't see anything about that.
[no name] 18-Sep-12 11:54am    
I think that this should be a seperate question as you have already accepted an answer to your previous question. But, when you put a breakpoint on the line "s1.displayDetails(s1);" and inspect s1 do the class fields in your Students object in fact contain the correct values?
BrianHamilton 18-Sep-12 12:48pm    
will repost.

1 solution

The first thing to note is that you really shouldn't be trying at all.

If you expose your form controls directly (by making them public) then you lock the design of your form - you can no longer change the way the form works without potentially affecting any other code which may use it. This is not a good idea, and is why all controls are private by default.

Instead, you should provide public properties which access the information, and let external code use them. You can then change the way your form works when you need to, and provided the properties still return the same values, no external code is affected:

C#
public string StudentFirstName
   {
   get { return txtStudentFirstName.Text; }
   set { txtStudentFirstName.Text = value; }
   }


The reason you are getting an exception is probably that you are trying to construct a form within an instance of itself - Form1 is the default name for the main form in Visual Studio and if you haven't changed it then you are trying to construct Form1 inside Form1, which means that it tries to build another Form1 inside that, which... and so on util you run out of memory and it throws an exception.
 
Share this answer
 
Comments
BrianHamilton 16-Sep-12 10:45am    
I get what you are saying but if I put the above in my class, it will not recognise the textboxes, is it supposed to go in the form?
OriginalGriff 16-Sep-12 10:57am    
Yes - the property provides a defined interface between the form and the outside world.
The outside world just treats the form as a "black box" which provides a string Name, an integer year, and so forth - it doesn't need to know or care about how the form gets the info.
BrianHamilton 16-Sep-12 13:30pm    
Got it working - Thans
OriginalGriff 17-Sep-12 2:29am    
You're welcome!

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