I need some help with using variables that belong to one class inside another class.
I have Semester
class that contains Weeks
and StartDate
properties.
In another class CalcDisplay
I have method CalculateStudyHours
that should calculate study hours based on Weeks
and StartDate
properties from Semester
class.
I created a Semester
object in my WPF class and I am passing through constructor weeks
and startDate
values that are provided by user.
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
int weeks = int.Parse(txtbWeeks.Text);
DateTime startDate = (DateTime)dteDate.SelectedDate;
Semester s = new Semester(weeks, startDate);
ModuleListHandler.semesterDetails.Add(s);
MessageBox.Show("Semester details added successfully");
btnNext.Visibility = Visibility.Hidden;
}
Semester
class
namespace TimeManagementClassLibrary
{
public class Semester
{
public int Weeks { get; set; }
public DateTime StartDate { get; set; }
//default constructor
public Semester()
{
}
public Semester(int weeks, DateTime startdate)
{
Weeks = weeks;
StartDate = startdate;
}
}
}
CalcDisplay
class
public class CalcDisplay
{
//Method calculating study hours
public double CalculateStudyHours()
{
return (credits * 10 / weeks) - classHours;
}
}
What would be the most effective way to pass credits
and weeks
parameters to CalcDisplay
class?
Additionally to my comment, let's imagine you have 3 classes:
public class Semester
{
public int Weeks { get; set; }
public Semester() { }
public Semester(int weeks)
{
Weeks = weeks;
}
}
public class Credit
{
public int Credits { get; set; }
public Credit() { }
public Credit(int credits)
{
Credits = credits;
}
}
public class SomeClass
{
public int ClassHours { get; set; }
public SomeClass() { }
public SomeClass(int classHours)
{
ClassHours = classHours;
}
}
You obviously would create their instances and set/calculate it's Weeks
, Credits
, ClassHours
values in some way.
Then you use that values to pass as arguments to some method of another class:
public class CalcDisplay
{
// May be static as uses passed in arguments values, non class members
public static double CalculateStudyHours(int credits, int weeks, int classHours)
{
return (credits * 10 / weeks) - classHours;
}
}
And passing them from somewhere:
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
// Get values in some way
int weeks = int.Parse(txtbWeeks.Text);
int credits = int.Parse(txtbCredits.Text);
int classHours = int.Parse(txtbClassHours.Text);
// Create your instances
Semester s = new Semester(weeks);
Credit c = new Credit(credits);
SomeClass sc = new SomeClass(classHours);
// Then pass it's properties values
double result = CalcDisplay.CalculateStudyHours(c.Credits, s.Weeks, sc.ClassHours);
}
Or CalcDisplay
class may has own fields/properties, which you set through its Constructor:
public class CalcDisplay
{
private int credits;
private int weeks;
private int classHours;
// Constructor
public CalcDisplay() {}
public CalcDisplay(int credits, int weeks, int classHours)
{
this.credits = credits;
this.weeks = weeks;
this.classHours = classHours;
}
// Non-static as we use class members
public double CalculateStudyHours()
{
return (credits * 10 / weeks) - classHours;
}
}
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
// ... same as upper
CalcDisplay = new CalcDisplay(c.Credits, s.Weeks, sc.ClassHours);
double result = c.CalculateStudyHours();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.