简体   繁体   English

如何将参数传递给C#类?

[英]How to pass parameter to class C#?

I have code as below 我的代码如下

public  class LocalDB
{
    public static int e_SessionID;

    public static string e_EventName;

    public static string e_TimeCreate;
}

in other class: 在其他课程中:

public static LocalDB newEvent ;
public void something()
    {
      newEvent.e_SessionID=123;
    }

but it is can not pass value. 但它无法传递价值。

Problem : You are trying to access the static feilds using instance reference variable newEvent as below: 问题:您正尝试使用实例引用变量newEvent访问静态newEvent ,如下所示:

newEvent.e_SessionID=123;
//^^^Here

Solution : You need to use classname to access the static fields 解决方案:您需要使用classname来访问static字段

newEvent.e_SessionID=123;
//^^^Replace with classname LocalDB here

Replace this: 替换这个:

 newEvent.e_SessionID = 123;

With this: 有了这个:

LocalDB.e_SessionID = 123;

Static methods and variables can only invoke using the class name and you are trying to call using the class object. 静态方法和变量只能使用类名调用,并且您尝试使用类对象进行调用。

if you want to set the value of e_SessionID set the value using class name as follows 如果要设置e_SessionID的值,请使用类名设置值,如下所示

LocalDB.e_SessionID=123;

Why don't you set them up as properties? 你为什么不将它们设置为属性? Have a read of this SO post why prefer properties to public variables 阅读此SO帖子为什么更喜欢属性到公共变量

"Fields are considered implementation details of classes and exposing them publicly breaks encapsulation." “字段被认为是类的实现细节,并暴露它们公开打破封装。”

public class LocalDB
{
    public int SessionID { get; set; }
}

try to use property instead: 尝试使用属性:

public  class LocalDB
{
   public int e_SessionID { get; set; }
   public string e_EventName { get; set; }
   public string e_TimeCreate { get; set; }
}

Prefer instance data to static data. 首选实例数据到静态数据。

Static data is effectively global state. 静态数据实际上是全局状态。 Do you have only one event in the lifetime of your program? 您的计划有效期内只有一个活动吗? What if you need to support multithreading? 如果您需要支持多线程怎么办? This is object-oriented programming; 这是面向对象的编程; use objects. 使用对象。

Encapsulate data. 封装数据。

Avoid making fields public. 避免公开领域。 Prefer properties, as others have stated. 像其他人所说的那样,选择房产。 Note that this allows assigning the creation time at construction (and only then). 请注意,这允许在构造时分配创建时间(并且仅在此时)。

Use appropriate types. 使用适当的类型。

If you are storing a date/time value, normally you would use the DateTime class. 如果要存储日期/时间值,通常可以使用DateTime类。

Favor immutability. 赞成不变性。

If you know the values of properties at construction time, set them then and don't allow them to be changed. 如果您在构造时知道属性的值,则设置它们然后不允许更改它们。

Think about names. 想想名字。

Descriptive names matter, especially when you're doing maintenance after six months. 描述性名称很重要,特别是当您在六个月后进行维护时。 I didn't change the name of LocalDB in my example, as I don't know your domain or use case. 我没有在我的示例中更改LocalDB的名称,因为我不知道您的域或用例。 However, this class looks more like an event than a database to me. 但是,对于我来说,这个类看起来更像是一个事件而不是数据库。 Would Event be a better name? Event会更好吗?

The following example uses C# 6 syntax; 以下示例使用C#6语法; earlier versions would need to add private setters and move the initialization to the constructor. 早期版本需要添加私有setter并将初始化移动到构造函数。

public class LocalDB
{
    public LocalDB(int sessionID, string eventName)
    {
        SessionID = sessionID;
        EventName = eventName;
    }

    public int SessionID { get; }
    public string EventName { get; }
    public DateTime TimeCreate { get; } = DateTime.UtcNow;
}

public class Other
{
    public void DoSomething()
    {
        NewEvent = new LocalDB(1, "Other Event");
    }

    public LocalDB NewEvent { get; private set; }
}

A flaw in this example is that the NewEvent property of an Other instance will be null on creation. 此示例中的一个缺陷是,在NewEvent时, Other实例的NewEvent属性将为null Avoid nulls where possible. 尽可能避免使用空值。 Perhaps this should be a collection of events; 也许这应该是一系列事件; not knowing your use case I can't say. 不知道你的用例我不能说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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