Jump to content
  • Advertisement
Sign in to follow this  
Rick D

C# Question.

This topic is 4115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am an early level C# programmer in school. I am just starting this last week in GUI and we have to make a averaging program. Everything but one part is working fine, and this one thing is something that I have had trouble with before but was never a real issue as I didn't need to run things the way I am now. I have to pass a variables value from the form to my class. The following is my code.
Quote:
Form1 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace LetterGrade { public partial class Form1 : Form { Class1 averageObject = new Class1(); public Form1() { InitializeComponent(); }//Closes Form 1 Method. private void btnScore_Click(object sender, EventArgs e) { string tempVal = "",errorFlag = ""; int score = 0; try { tempVal = txtScore.Text; score = int.Parse(tempVal); errorFlag = "noError"; txtScore.Clear(); }//Closes the try. catch (System.FormatException) { MessageBox.Show("Error! You have entered an invalid interger!"); errorFlag = "error"; }//Closes the catch. if (errorFlag == "error") { txtName.Clear(); txtName.Focus(); }//Closes the if. if (score < 0 || score > 100) { MessageBox.Show("Error! You must enter an interger from 0 through 100!"); errorFlag = "error"; }//Closes the if. if (errorFlag == "noError") { MessageBox.Show("Score entered " + score, "Grade Average"); averageObject.TotalScoreMethod(); averageObject.CounterMethod(); }//Closes the if. }//Closes the btnScore_Click method. private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); }//Closes the btnExit_Click method. private void btnClear_Click(object sender, EventArgs e) { txtScore.Clear(); txtName.Clear(); txtId.Clear(); txtOutput.Clear(); }//Closes the btnClear_Click method. private void btnOutput_Click(object sender, EventArgs e) { string name = "", id = "",data = ""; name = txtName.Text; id = txtId.Text; MessageBox.Show("name " + name + "\nid " + id + "\naverage " + averageObject.AverageMethod() + "\nletter grade " + averageObject.LetterGradeMethod()); data = "name " + name + "\r\nid " + id + "\r\naverage " + averageObject.AverageMethod() + "\r\nletter grade " + averageObject.LetterGradeMethod(); txtOutput.Text = data; }//Closes the btnOutput_Click method. }//Closes the class. }//Closes the namespace.
Quote:
Class1 using System; using System.Collections.Generic; using System.Text; namespace LetterGrade { class Class1 { private string letterGrade; private int totalScore; private int counter; private int average; public void CounterMethod() { counter = counter + 1; }//Closes the counter method. public void TotalScoreMethod() { totalScore = totalScore + score; }//Closes the totalscore method. public int AverageMethod() { average = totalScore / counter; return average; }//Closes the average method. public string LetterGradeMethod() { if (average >= 90) { letterGrade = "A"; }//Closes the if. else if (average >= 80) { letterGrade = "B"; }//Closes the if. else if (average >= 70) { letterGrade = "C"; }//Closes the if. else if (average < 70) { letterGrade = "F"; }//Closes the if. return letterGrade; }//Closes the lettergrade method. }//closes the class. }//Closes the namespace.
I need to get the value of score in the form to pass on to the average method I have in class 1. Appreciate any help as I am still new to this :)

Share this post


Link to post
Share on other sites
Advertisement
Never mind I got help on another website...

If anyone else ever has a question like this the answer is you do it like this:

In the btnScore_Clk I have averageObject.TotalScoreMethod(); Need to make it averageObject.TotalScoreMethod(score);

And in the class I have a method TotalScoreMethod(). Need to be TotalScoreMethod(int score).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!