Reputation: 314
UPDATE: Whenever values are entered in classes 2 onwards the only result that is shown is that of the last entered inputs. So for example: if data is entered in class1 row and class2 row only calculations for class2 row is displayed. Could someone tell me why this is happening please?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Grade_Point_Average_Calculator
{
public partial class GPA_Calculator : Form
{
SaveFileDialog saveFileDialog1;
public GPA_Calculator()
{
InitializeComponent();
}
private void GPA_Calculator_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
// Closes application after exitToolStripMenuItem_Click
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
// Displays information about the application version number and creator
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Version 1.0., "About");
}
private void calculateBtn_Click(object sender, EventArgs e)
{
int maskbox1, maskbox2, maskbox3, maskbox4, maskbox5, maskbox6;
CalculatorLogics oCalculatorLogics1 = new CalculatorLogics(this);
oCalculatorLogics1.SelectedGrade = double.Parse(comboBox1.SelectedValue.ToString());
if (!int.TryParse(maskedTextBox1.Text, out maskbox1))
{
maskbox1 = 0; // Assign zero is parse fails
}
else
{
oCalculatorLogics1.CourseCredit = int.Parse(maskedTextBox1.Text);
}
oCalculatorLogics1.performGpaCalculations();
answerLabel.Text = oCalculatorLogics1.CalcGrade.ToString();
CalculatorLogics oCalculatorLogics2 = new CalculatorLogics(this);
oCalculatorLogics2.SelectedGrade = double.Parse(comboBox2.SelectedValue.ToString());
if (!int.TryParse(maskedTextBox2.Text, out maskbox1))
{
maskbox2 = 0; // Assign zero is parse fails
}
else
{
oCalculatorLogics2.CourseCredit = int.Parse(maskedTextBox2.Text);
}
oCalculatorLogics2.performGpaCalculations();
answerLabel.Text = oCalculatorLogics2.CalcGrade.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Grade_Point_Average_Calculator
{
class CalculatorLogics
{
private double grade;
private double gradeValue;
private double calculateGrades;
private int credits;
private GPA_Calculator _GPA_Calculator;
// Pass form to CalculatorLogics constructor
public CalculatorLogics(GPA_Calculator theGPA_Calculator)
{
_GPA_Calculator = theGPA_Calculator;
}
public double SelectedGrade
{
get { return grade; }
set { grade = value; }
}
public int CourseCredit
{
get { return credits; }
set { credits = value; }
}
public double CalcGrade
{
get
{
return calculateGrades;
}
}
public void performGpaCalculations()
{
gradeVal = grade * credits;
totalCredits += credits; // Add the amount of credits
totalGradeValue += gradeVal;
calculateGrades = totalGradeValue/totalCredits; // Calculates gpa
return calculateGrades;
}
}
}
Upvotes: 0
Views: 326
Reputation: 61
To obtain the selected values of the comboBoxes, it does not help to just create a new instance of the form class.
Instead, you may pass the current form to the method like:
public void performGpaCalculations(GPA_Calculator form)
or better just pass a list of the selected items:
public void performGpaCalculations(int[] selectedValues)
int[] because i would suggest you to not use selectedItem of the comboBox but selectedIndex and create a static grade array
double[] grades = {4.00, 3.67, // ...
so you don't need that switch.
UPDATE: OK, the answer above was updated with what I wrote, so my answer won't be helpful anymore...
Upvotes: 1
Reputation: 34846
As the code stands, your issue is that you are not calling the performGpaCalculations()
method in your CalculatorLogics
class and this is causing the calculateGrades
value returned from the CalcGrade
property to be the default value of a double
(0.0).
You need to call the performGpaCalculations()
method in your code, like this:
private void calculateBtn_Click(object sender, EventArgs e)
{
CalculatorLogics oCalculatorLogics = new CalculatorLogics();
oCalculatorLogics.performGpaCalculations();
answerLabel.Text = oCalculatorLogics.CalcGrade.ToString();
}
This will allow the CalcGrade
class to determine the value of calculateGrades
.
UPDATE:
But your real problem is that you are instantiating a new instance of the form class GPA_Calculator
, which blows away the values the user selected.
Instead you need to pass an instance of the GPA_Calculator
form class to the constructor of your CalculatorLogics
class, like this:
public CalculatorLogics(GPA_Calculator theGPA_Calculator)
{
}
Now that your form is being passed to the constructor, you need somewhere to store it so the CalculatorLogics
class can use it, here is a rewrite of your CalculatorLogics
class to support this:
public class CalculatorLogics
{
private double grade;
private double gradeValue;
private double calculateGrades;
private int credits;
private GPA_Calculator _GPA_Calculator;
public CalculatorLogics(GPA_Calculator theGPA_Calculator)
{
_GPA_Calculator = theGPA_Calculator;
}
public double SelectedGrade
{
get { return grade; }
set { grade = value; }
}
public int CourseCredit
{
get { return credits; }
set { credits = value; }
}
public double CalcGrade
{
get
{
return calculateGrades;
}
}
public void performGpaCalculations()
{
const double grade_A = 4.00;
const double grade_A_minus = 3.67;
const double grade_B_plus = 3.33;
const double grade_B = 3.00;
const double grade_B_minus = 2.67;
const double grade_C_plus = 2.33;
const double grade_C = 2.00;
const double grade_C_minus = 1.67;
const double grade_D = 1.33;
const double grade_F = 0.00;
switch (_GPA_Calculator.comboBox1.SelectedItem.ToString())
{
case "A":
gradeValue = grade_A;
break;
case "A-":
gradeValue = grade_A_minus;
break;
case "B+":
gradeValue = grade_B_plus;
break;
case "B":
gradeValue = grade_B;
break;
case "B-":
gradeValue = grade_B_minus;
break;
case "C+":
gradeValue = grade_C_plus;
break;
case "C":
gradeValue = grade_C;
break;
case "C-":
gradeValue = grade_C_minus;
break;
case "D":
gradeValue = grade_D;
break;
case "F":
gradeValue = grade_F;
break;
}
calculateGrades = gradeValue * credits;
}
}
Finally, when you instantiate your CalculatorLogics
class you will need to pass a reference to the form you are in, like this:
CalculatorLogics oCalculatorLogics = new CalculatorLogics(this);
Note: this
is a pointer to the class itself, in this case the GPA_Calculator
form.
Upvotes: 2