![](/img/trans.png)
[英]Same Variable Names - 2 Different Classes - How To Copy Values From One To Another - Reflection - C#
[英]How to use variable values from different classes WPF C#
我需要一些帮助来使用属于另一个类中的一个类的变量。
我有包含Weeks
和StartDate
属性的Semester
类。
在另一个类CalcDisplay
我有方法CalculateStudyHours
,它应该根据Semester
类的Weeks
和StartDate
属性计算学习时间。
我在 WPF 类中创建了一个Semester
对象,并且正在传递用户提供的构造函数weeks
和startDate
值。
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
班
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
类
public class CalcDisplay
{
//Method calculating study hours
public double CalculateStudyHours()
{
return (credits * 10 / weeks) - classHours;
}
}
将credits
和weeks
参数传递给CalcDisplay
类的最有效方法是什么?
除了我的评论,让我们假设你有 3 个类:
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;
}
}
您显然会创建它们的实例并以某种方式设置/计算它的Weeks
、 Credits
、 ClassHours
值。
然后你使用这些值作为参数传递给另一个类的某个方法:
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;
}
}
并从某个地方传递它们:
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);
}
或者CalcDisplay
类可能有自己的字段/属性,您可以通过其构造函数进行设置:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.