2014-12-06 35 views
1

是否有任何选项做这样的事情:列表添加一个裁判字符串对象

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

class Student 
{ 


    public string PassPort { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Message { get; set; } 
    public List<string> AllProperties { get; set; } 

    public Student() 
    { 
     AllProperties = new List<string>(); 
     AllProperties.Add(ref PassPort); 
     AllProperties.Add(ref FirstName); 
     AllProperties.Add(ref LastName); 
     AllProperties.Add(ref Message); 


    } 

} 

所以当我改变AllProperties[0]它会改变PassPort字符串变量?

+0

你想AllProperties [ 0]调用属性“PassProt”?如果是的话,你应该反思 – ZivS 2014-12-06 09:20:23

+0

我使用DataTable来显示信息,我想这样做索引我得到 – 2014-12-06 09:39:20

+0

我在做execl数据表和我需要字符串作为属性 但消息属性是可选的,所以我想确保只有相关的属性是fullfill和不输入null属性 – 2014-12-06 09:41:54

回答

2

我真的不知道它是什么你是后,但你可能能够使用一个索引:

class Student 
{ 
    public string PassPort { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Message { get; set; } 

    public string this[int index] 
    { 
     get { throw new NotImplementedException(); } 
     set 
     { 
      switch (index) 
      { 
       case 0: 
        PassPort = value; 
        break; 
       case 1: 
        // etc. 
       default: 
        throw new NotImplementedException(); 
      } 
     } 
    } 
} 

而且使用的是这样的:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Student student = new Student(); 
     student[0] = "PASS"; 
    } 
} 
+0

非常感谢你对我的项目完美的工作 – 2014-12-06 09:39:43

+0

我在做execl数据表,我需要字符串作为属性 但消息属性是可选的,所以我想确保只有相关的属性填满并且不对属性输入null – 2014-12-06 09:41:04

相关问题