13 Replies - 1348 Views - Last Post: 11 December 2013 - 06:18 PM Rate Topic: -----

#1 Foxfire140   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 13

Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 01:16 PM

Hello. First time here. Just wanted to get that out of the way.
I'm studying C# in school and I've been trying to work out one of my assignments in terms of getting my program to function correctly but I seem to have hit a crossroads.
My goal is to create a program that accepts student information via seperate textboxes and displays them via Console.WriteLine. In order to set this up, I have a seperate class called Student which has been filled with objects that are used in the main form that contains the code to pull data from the textboxes.

The problem that I'm having is that the application will have a fill button that takes user input and fills it into the properties but I don't know how to get that data into the display button. I already know that I can't change the properties of a button click event and I've been racking my brain trying to figure out what I'm doing wrong.

Here's the code for the Student class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_10_thru_12
{
    public class Student
    {
        //===============Properties==================//
        private int sid;
        private string name;
        private string addr;
        private int phoneNum;
        private string major;
        private double gpa;
        //==============behaviors===================//
        public int getSid() { return sid; }
        public void setSid(int s) { sid = s; }

        public string getName() { return name; }
        public void setName(string n) { name = n; }

        public string getAddr() { return addr; }
        public void setAddr(string a) { addr = a; }

        public int getPhoneNum() { return phoneNum; }
        public void setPhoneNum(int pn) { phoneNum = pn; }

        public string getMajor() { return major; }
        public void setMajor(string m) { major = m; }

        public double getGpa() { return gpa; }
        public void setGpa(double g) { gpa = g; }
        //============Display Function=============//
        public void Display()
        {
            Console.WriteLine("Student ID Number: " + getSid());
            Console.WriteLine("Name: " + getName());
            Console.WriteLine("Address: " + getAddr());
            Console.WriteLine("Home Phone: " + getPhoneNum());
            Console.WriteLine("Major: " + getMajor());
            Console.WriteLine("Current GPA: " + getGpa());
        }
        //===========Constructors===============//
        public Student()
        {
            sid = 0;
            name = "";
            addr = "";
            phoneNum = 0;
            major = "";
            gpa = 0;
        }
        public Student(int s, string n, string a, int pn, string m, double g)
        {
            sid = s;
            name = n;
            addr = a;
            phoneNum = pn;
            major = m;
            gpa = g;
        }
    }
}


and this is the code that I have so far for the form:

namespace Lab_10_thru_12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void fillButton_Click(object sender, EventArgs e)
        {
            Student s1 = new Student();
            s1.setSid(int.Parse(sidInputBox.Text));
            s1.setName(nameInputBox.Text);
            s1.setAddr(addrInputBox.Text);
            s1.setPhoneNum(int.Parse(phoneNoInputBox.Text));
            s1.setMajor(majorInputBox.Text);
            s1.setGpa(double.Parse(gpaInputBox.Text));
            
        }

        private void displayButton_Click(object sender, EventArgs e)
        {
            
            s1.Display();
        }
    }
}


I just need some sort of enlightenment into where my error is because, at the moment, s1 in the display button doesn't exist in the current context.

I did try to make a new method FillData(Student s1) that handled the filling of the data seperately so that the fill button event only had to deal with assigning s1 to student, creating a new s1, and then calling the FillData method...
This is what it looked like before
        private void fillData(Student stu)
        {
            stu.setSid(int.Parse(sidInputBox.Text));
            stu.setName(nameInputBox.Text);
            stu.setAddr(addrInputBox.Text);
            stu.setPhoneNum(int.Parse(phoneNoInputBox.Text));
            stu.setMajor(majorInputBox.Text);
            stu.setGpa(double.Parse(gpaInputBox.Text));
        }

        private void fillButton_Click(object sender, EventArgs e)
        {
            Student s1 = new Student();
            fillData(s1);
            
        }

        private void displayButton_Click(object sender, EventArgs e)
        {
            s1.Display();
        }


[

but I've since removed that code because I couldn't figure out how to get the filled data acquired from the fillButton_Click event to be called upon by the s1.Display(); method call in the displayButton_Click.

I'm missing something and it's probably painfully obvious but I just can't figure out what it is.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help figuring out how to pass data between buttons

#2 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 01:18 PM

How To Get Values From Form1 To Form2 (Or Any Other Forms)

or you could read this SO topic, answer contains 5 ways to pass data.

This post has been edited by Michael26: 10 December 2013 - 01:22 PM

Was This Post Helpful? 1
  • +
  • -

#3 Foxfire140   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 13

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 01:52 PM

Thank you for that. I read up on both links but I'm still feeling a bit confused. (Mind you, I've been at this for days.)
AS far as I can tell, I have passed the data from the form class to the main form...unless there's a disconnect that I'm not catching regarding the data being set by the textboxes and what the student class is doing with that data?
I feel like I'm supposed to be calling s, n, etc., from the behaviors somehow but I can't pass all of the data and display it with the same button.
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 451
  • View blog
  • Posts: 2,187
  • Joined: 07-April 08

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 02:11 PM

Your problem is related to scope. The reason why you can't successfully call S1.Display() in the Display button click is because it doesn't know what S1 is.

Because you created the Student object S1 within the Fill button click it is only valid for that method. To get around this you have to declare S1 outside of the method.

Create S1 as a Class level variable

EX

Public Class foo{
Student s1;  

private void fillButton_Click(object sender, EventArgs e){
 s1 = new Student;

//fill it
}
}



this will make it so that S1 is visible anywhere within the class.
Was This Post Helpful? 2
  • +
  • -

#5 Foxfire140   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 13

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 03:53 PM

Hmm, that helped quite a bit however, now all of the input boxes have errored.

        public class Stu1
        {
            Student s1;


            private void fillButton_Click(object sender, EventArgs e)
            {
                s1 = new Student();
                s1.setSid(int.Parse(sidInputBox.Text));
                s1.setName(nameInputBox.Text);
                s1.setAddr(addrInputBox.Text);
                s1.setPhoneNum(int.Parse(phoneNoInputBox.Text));
                s1.setMajor(majorInputBox.Text);
                s1.setGpa(double.Parse(gpaInputBox.Text));
            }

"Error 1 Cannot access a non-static member of outer type 'Lab_10_thru_12.Form1' via nested type 'Lab_10_thru_12.Form1.Stu1'"
I've done a bit of research and I think that I'm going to use some inheritance to make the code work. 'll take a while since I haven't practiced inheritance yet so I'm going to need to learn it first before I can fix the code. Thanks for all your help. =)
Was This Post Helpful? 0
  • +
  • -

#6 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 04:17 PM

Quote

via seperate textboxes and displays them via Console.WriteLine.

Textbox is control in Winforms and you can't Console class in WinForms project.

Also regarding error, nested classes are not the same as inherited classes. In C#, nested classes are completely separate instances, and have no direct relation to the outer class instance.

This post has been edited by Michael26: 10 December 2013 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 04:31 PM

You can "Console Class" in WinForms the output is redirected somewhere else. (I think either the Debug / Trace.) In debug in the VS IDE it appears in the Output window. I kinda think a of a Windows Application as a Superclass of a Console application.
Was This Post Helpful? 0
  • +
  • -

#8 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 04:35 PM

Quote

You can "Console Class" in WinForms the output is redirected somewhere else.

Well yes, that was the general idea. I don't know why i didn't write that.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 07:22 PM

Quote

My goal is to create a program that accepts student information via seperate textboxes and displays them via Console.WriteLine.


That right there is a hot mess. Why would you create a GUI program only to output via console? If you're going to make a WinForms program, then provide the feed to the user through your Winforms GUI.

Quote

user input and fills it into the properties I don't know how to get that data into the display button.

You don't get data into a button. The button starts an action of some kind, like running a method. The method will use the information in the properties to do its work.
There is a tutorial liked in my signature block on properties. It should help. You might consider doing the tutorial on classes as well. It really sounds like you are confused about how programs break down into classes, and how classes talk to each other etc.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6451
  • View blog
  • Posts: 22,083
  • Joined: 05-May 12

Re: Need help figuring out how to pass data between buttons

Posted 10 December 2013 - 08:04 PM

I hate to admit it, but I've done something similar to this answer in StackOverflow before to get a console window in a WinForms program. The console was for an ITP (interactive test program) built into to the program. (At the time, management had not bought into Unit Tests as viable option.)
Was This Post Helpful? 0
  • +
  • -

#11 Foxfire140   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 13

Re: Need help figuring out how to pass data between buttons

Posted 11 December 2013 - 01:37 AM

Yeah, it's clear that I still have a lot to learn about classes and passing data correctly.
The reason why I thought about Inheritance, as a little background, was because the 3 final topics that I had been learning about in school this semester were classes, constructors & inheritance. Looking up the issue that I was having, rewriting things to use either inheritance or static methods was the most recommended solution that I had been able to find:
http://stackoverflow.com/a/16321059

This particular exercise just dealt with practicing working with classes for the first time. (The constructors at the end of the Student class are for the next lab dealing with this same application but I went ahead and added them in anyway since it doesn't really hurt anything.)
Creating the display method within the Student class is also a part of the assignment

As for why I'd create a gui just to output via the console, that's what the lab asks all of the students to do.

Michael26, Adam & tlhIn`toq: My class has been using the Output window in VisualBasic for a few of the labs that we've done so that when we run our code, we know that everything is working properly. In fact, our very first ever lab dealt with clicking a gui button that made a specific string statement output into the console exactly 10 times.

tlhIn`toq, thanks for the help. I'll read up those 2 tutorials tomorrow. I definitely still have a lot to learn about classes & managing properties so I'll have to check those out.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Need help figuring out how to pass data between buttons

Posted 11 December 2013 - 06:10 AM

Quote

As for why I'd create a gui just to output via the console, that's what the lab asks all of the students to do.

Michael26, Adam & tlhIn`toq: My class has been using the Output window in VisualBasic for a few of the labs that we've done so that when we run our code, we know that everything is working properly. In fact, our very first ever lab dealt with clicking a gui button that made a specific string statement output into the console exactly 10 times.


I find this both scary and sad. It's very sad that you're paying for a course where this is being taught. If you're only a couple weeks into the course and can still drop it and get your fees refunded then you should. You can learn more AND BETTER than this with any "Learn C# in 30 days" book out there. This is such a horrible practice and all it is teaching you is bad habits that you'll have to break and re-learn good ones later on your own. Why do that when you can just learn correctly the first time?

Since the console is just being use for output then here's an easy fix:
  • Put a ListView on your form.
  • Change your Display() method from console.writeline to ListView.Items.Add()
  • Now you'll have your output on your WinForms GUI


Please please please... By a "Learn C# in 30 days" type book. Work it. There is no law that says you ONLY have to learn from your textbook and professor. While (s)he's teaching you trash you can teach yourself 10x faster and 100x better from a book, in your free time. Your projects will stand out from the rest of the class and you might just learn something useful to you.

Remember, colleges aren't the in business if preparing you for careers. They are in the business of selling course credits: That's their revenue model. So if they can stretch 6 months of learning into 4 years of classes they make more money. Your professor is not someone who has a passion for coding. They have a passion for a tenured position that's hard to get fired from, while reading someone else's textbook to a class - and dreams of sleeping with young co-ed that want better grades.
Was This Post Helpful? 0
  • +
  • -

#13 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Need help figuring out how to pass data between buttons

Posted 11 December 2013 - 06:29 AM

Quote

Remember, colleges aren't the in business if preparing you for careers. They are in the business of selling course credits: That's their revenue model. So if they can stretch 6 months of learning into 4 years of classes they make more money. Your professor is not someone who has a passion for coding. They have a passion for a tenured position that's hard to get fired from, while reading someone else's textbook to a class - and dreams of sleeping with young co-ed that want better grades.


It's like all this is just one big scam to take your money. With invention of internet and online learning these CS professors will have to pick up their pace or be left behind, just like elevator operator :)
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Need help figuring out how to pass data between buttons

Posted 11 December 2013 - 06:18 PM

I'd like to see a change in the revenue model. Make the teachers have a reason to get the student properly skilled.

Teacher receives 1% of the student's earnings for the first 2 years so long as they are employed in the field they studied. So the better the teacher prepares the student, the better the job, the better the pay, the more the professor receives. Seems like motivation to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1