2014-12-06 36 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

相關問題