Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms
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 16-Sep-12 4:39am
Edited 18-Sep-12 6:26am
v3
Comments
Wes Aday at 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 at 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.
Wes Aday at 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 at 18-Sep-12 12:48pm
   
will repost.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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.
  Permalink  
Comments
BrianHamilton at 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 at 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 at 16-Sep-12 13:30pm
   
Got it working - Thans
OriginalGriff at 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)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100