在类中处理全局变量的方法

本文关键字:方法 全局变量 处理 | 更新日期: 2023-09-27 18:36:56

有一个这样的php代码,(这里是php函数的一部分)要把它转换成C#。

function jaktDate2()
{
    Global $nameofselectbox,$startYear,$endYear,$year,
    $startDate,$endDate,$startMounth,$endMounth,$startDay,$endDay;
    $today = getdate();
    $year=$today['year'];
    $mounth=$today['mon'];
    $day=$today['mday'];

我的C#代码尝试是这样的,

  public class HuntingDate
    {
        public string StartYear;
        public string EndYear;
        public string Year;
        public DateTime StartDate;
        public DateTime EndDate;
        public string StartMonth;
        public string EndMonth;
        public DateTime StartDay;
        public DateTime EndDay;

    }

我以这种方式开始..这种方式正确吗?接下来我应该怎么做?

在类中处理全局变量的方法

在 .NET 中,您不需要具有私有成员变量,因为编译器将在编译时添加此变量。

因此,您可以更改

private string _year;
public string Year
{
    get { return _year;}
    set { _year = value;}
}

变成更好的阅读。

public string Year { get; set; }

若要在 .NET 中使用日期,只需使用System.DateTime.Now然后访问任何属性,如System.DateTime.Now.DaySystem.DateTime.Now.Year等。

你可以这样做:

public int Num { get; set; }

取而代之的是:

private int _num;
public int Num { get { return _num; } set { _num = value; } }

至于最后一部分:

DateTime today = DateTime.Now;
int year = today.Year;
int month = today.Month;
int day = today.Day;

请注意,today还将具有小时、分钟、秒和毫秒。如果您只想Date Now的一部分,或任何其他 DateTime 对象(也称为"修剪"从小时开始的"小计数"),您可以执行以下操作:

DateTime dateOnly = someDate.Date;
// someDate: 2.4.2012 10:04:12:0004
// dateOnly: 2.4.2012 00:00:00:0000

看起来您还需要从年,月和日创建一个DateTime对象,因此:

this.StartDate = new DateTime(startYear, startMonth, startDay);

编辑

// This gets called the moment you initialize the object
public HuntingDate()
{
    DateTime today = DateTime.Now;
    int year = today.Year;
    int month = today.Month;
    int day = today.Day;
    // more logic, to set StartDate and EndDate
}

现在从外面你可以做一些事情:

HuntingDate hd = new HuntingDate();
DateTime sd = hd.StartDate;
DateTime ed = hd.EndDate;

你定义的类看起来更像是一个随机的值抓取袋,而不像一个定义明确的类。 所有这些值实际上代表什么?

public string StartYear;
public string EndYear;
public string Year;
public DateTime StartDate;
public DateTime EndDate;
public string StartMonth;
public string EndMonth;
public DateTime StartDay;
public DateTime EndDay;

例如,StartDateStartDay有什么区别? 什么是StartYearStartMonth? 这个类实际上定义了什么? 看起来您正在尝试将DateTime值分解为组件。 你不需要这样做。 一个简单的DateTime值将充分存储必要的信息,您可以直接从该值获取组件:

public DateTime StartDate;
public DateTime EndDate;

例如,如果您需要知道月份,则可以从该值中获取它:

myObject.StartDate.Month;

若要继续改进类,您需要使用属性而不是公共成员。 在 C# 中,这些看起来像这样:

public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

这些属性专门称为自动实现的属性。 它们是完整属性的编译器简写:

private DateTime _startDate;
public DateTime StartDate
{
    get { return _startDate; }
    set { _startDate = value; }
}
// repeat for EndDate

属性的好处是,该类对其内部结构的公开较少。 如果您需要向类添加任何逻辑(例如检查日期的特定边界,例如确保过去没有 StartDate),则可以将其添加到属性中,而不会破坏类的二进制兼容性。 因此,使用代码永远不需要知道其中的区别。 例如:

private DateTime _startDate;
public DateTime StartDate
{
    get { return _startDate; }
    set
    {
        if (value < DateTime.Now)
            throw new ArgumentException(string.Format("Start Date must not be in the past: {0}", value.ToString()));
        _startDate = value;
    }
}

您可以通过继续定义此类的行为来继续更进一步。 即使公开了这些属性,仍然使类更像是"数据结构"而不是"对象"。 (为了进一步阅读数据/对象反对称性,我推荐Robert Martin的Clean Code。 适当的面向对象设计的目标是让对象隐藏其数据并公开在内部对该数据执行有状态操作的方法。

例如,如果您需要将EndDate延长一天,则可以执行以下操作:

myObject.EndDate += new TimeSpan(1, 0, 0, 0);

但是一种更面向对象的方法(坚持"告诉,不问"原则)是告诉对象本身延长时间,而不是直接告诉其数据延长时间(从而在此过程中"询问"对象的数据,这可以说也违反了得墨忒耳定律):

myObject.ExtendEndDate(new TimeSpan(1, 0, 0, 0));

甚至:

myObject.ExtendEndDateInDays(1);

您只需要在对象上实现这样的方法,该方法将在内部扩展 EndDate 的值。

类本身应该封装它所表示的概念所需的所有功能。 如果绝对必要,它可以为其内部成员提供公共读取访问权限,但从设计的角度来看,值得问问自己真正需要对对象的数据进行哪种查询,并为这些查询提供更有针对性的方法,而不是直接访问类的数据成员。

最好将这些定义为属性。原因请看这里。无论如何,你写的类是完全有效的,只是不被认为是一个好的做法。您可能还会添加一个构造函数,也可以像 php 代码一样初始化一些值。

这是属性:

public class HuntingDate
{
    public string StartYear{
        get;
        set;
    }
    public string EndYear{
        get;
        set;
    }
}
没有

办法像在 PHP 中那样在 C# 中执行全局操作。您可以使用单例或静态类来执行此操作。

public static class HuntingDate
{
    public static string StartYear { get; set; }
}

然后在您的代码中简单地

HuntingDate.StartYear = DateTime.Now

var startYear = HuntingDate.StartYear

C# 中没有全局变量声明。因为它是一种面向对象的编程语言。在您的情况下,您可以声明一个静态类

public static class HuntingDate
{
    public static string StartYear 
    {
      get; 
      set; 
    }
    public static string EndYear 
    {
      get; 
      set; 
    }
    // and so on
}

您可以从类名访问这些属性。

HuntingDate.StartDate = DateTime.Now;
HuntingDate.EndDate = DateTime.Now.AddDays(10);