如何在我的其他类'方法中引用我的公共类属性Event.Day?

时间:2013-04-26 04:32:29

标签: c#

我试图在其他类'方法中引用我的公共类属性,但我不能正确地执行它。有人可以提供帮助吗?它是ExtractData( Event special.Day )部分。

public static List<Event> ExtractData(Event special.Day)
{
   int intChosenDay = special.Day;  

    StreamReader textIn =
    new StreamReader(
    new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));

     //create the list
     List<Event> events = new List<Event>();



     string[] lines = File.ReadAllLines(path);

     for (int index = 4; index < lines.Length; index += 5)
     {
        Event special = new Event();
        special.Day = Convert.ToInt32(lines[index - 4]);
        special.Time = (lines[index - 3]);
        special.Price = Convert.ToDouble(lines[index - 2]);
        special.StrEvent = lines[index - 1];
        special.Description = lines[index];
        events.Add(special);
     }

     textIn.Close();

     return events;

}

2 个答案:

答案 0 :(得分:1)

如果要将参数Day传递给方法,则应指定其类型,而不是事件类型。像:

public static List<Event> ExtractData(int Day)
{
 //....your code
}

(如果日期为int类型,则指定int,否则请相应指定类型。

稍后您可以将其称为:

Events event = new Event();
var list = ExtractData(event.Day);

由于ExtractData是一个静态方法,如果你是从类外部调用它,你必须使用类名称来调用它:

var list = Event.ExtractData(event.Day); //if the class name is Event

答案 1 :(得分:0)

您无法在方法定义中访问成员属性,只能定义传入的函数参数(通过“[type] [variable-name]”格式)。

实现您尝试做的事情的一种方法是在您的方法中,您可以访问该属性并直接使用它或将其存储到另一个变量:

public static List<Event> ExtractData(Event theEvent) {
    int day = theEvent.Day;
    // ...
相关问题