繁体   English   中英

如何使用来自不同类的变量值 WPF C#

[英]How to use variable values from different classes WPF C#

我需要一些帮助来使用属于另一个类中的一个类的变量。

我有包含WeeksStartDate属性的Semester类。

在另一个类CalcDisplay我有方法CalculateStudyHours ,它应该根据Semester类的WeeksStartDate属性计算学习时间。

我在 WPF 类中创建了一个Semester对象,并且正在传递用户提供的构造函数weeksstartDate值。

 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;
   }          
}

creditsweeks参数传递给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;
    }
}

您显然会创建它们的实例并以某种方式设置/计算它的WeeksCreditsClassHours值。

然后你使用这些值作为参数传递给另一个类的某个方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM