2013-05-09 40 views
0

我對C#真的很陌生。我有多個計算機實例,每個計算機實例都有一個名稱,停止,啓動和重新啓動與每個計算機相關的命令。我想從文件中讀取信息。具有多個對象的陣列陣列

所以我想最終以instancelist[0].Instance_name=Enterprise1instancelist[0][email protected]_stopinstancelist[1].Instance_name=Enterprise5等等。我可以弄清楚如何做這個聲明。

public class Instance 
{ 
    public string Instance_name; 
    public string Instance_stop; 

    public string Instance_restart; 
    public string Instance_backup; 
} 

public static void Main(string[] args) 
{ 
    int num_instances=0; 

    /** CAN'T figure out the declaration. I'm currently thinking array of array? */ 

    System.IO.StreamReader file = new System.IO.StreamReader(@"C......"); 

    while(true) 
    { 
     instancelist[num_instance].Instance_name=file.ReadLine(); 
     instancelist[num_instance].Instance_stop=file.ReadLine(); 
     // and so on....... 
     num_instance++; 
    } 
} 
+0

我猜你是在談論「鋸齒狀」陣列:這裏是解釋用法的鏈接:http://msdn.microsoft.com/en-us/library/2s05feca.aspx – 2013-05-09 00:21:29

+2

您好!我花時間格式化代碼,但將來你應該在發佈你的問題之前讓你的代碼呈現出來。 (要立即發佈大量代碼,請使用「代碼示例」選擇將其全部縮進四個空格,這會使其格式化爲代碼) – Patashu 2013-05-09 00:22:05

回答

2

您可能會更好使用集合而不是數組。如果元素數量發生變化,處理起來會更容易。就你而言,你正在從文件中讀取字符串,所以你不可能提前知道列表的大小。

但是,您還需要一個DTO類。因此,這裏的一些代碼(未經測試):

// DTO Class 
public class Instance 
{ 
    public string Instance_Start { get; set; } 
    public string Instance_Stop { get; set; } 
} 

var instanceList = new List<Instance>; 
var file = new System.IO.StreamReader(myFile); 

while(!file.EndOfStream) 
{ 
    var instance = new Instance 
    { 
     Instance_Start = file.Readline(); 
     Instance_Stop = file.Readline(); 
    }; 

    instanceList.Add(instance); 
    num_instance++; 
} 

請注意,您仍然可以通過索引訪問instanceList元素,如

instanceList[0].InstanceStart 
+0

您實際上沒有正確回答OP的問題......實例列表是不是一組字符串,而是一組實例對象。 – guysherman 2013-05-09 00:22:36

+0

@guysherman:固定。 – 2013-05-09 00:33:34

0

這聽起來像你想獲得這些Instance對象的集合,其中的每一個都將從文件中的一組連續行進行分析。

我建議是這樣的:

// First: a static method to create an Instance object from a few 
// consecutive lines in a stream 
class Instance 
{ 
    public static Instance ReadFromStream(StreamReader reader) 
    { 
     var instance = new Instance(); 
     instance.InstanceName = reader.ReadLine(); 
     instance.InstanceStop = reader.ReadLine(); 
     // etc. 
     return instance; 
    } 
} 

// Then, elsewhere: use a List<T> (an expandable collection) 
// to store all the instances you create from reading the file. 
// Note that the 'using' statement automatically closes the file 
// for you when you're done. 
var instances = new List<Instance>(); 
using (var reader = new StreamReader(filePath)) 
{ 
    while (!reader.EndOfStream) 
    { 
     instances.Add(Instance.ReadFromStream(reader)); 
    } 
} 
0

您的問題是相當模糊的,但這裏是我的上述問題的回答。

Apparantly您有一個名爲「實例」類,我建議建立「實例」類實例的列表:

using System.Collections.Generic; // Add this to the rest of 'usings' 

public static void Main(string[], args) 
{ 
    // Create a new stream to read the file 
    StreamReader SReader = new StreamReader("C:\file.txt"); 

    // Create a list of 'Instance' class instances 
    List<Instance> AllInstances = new List<Instance>(); 

    // Keep reading until we've reached the end of the stream 
    while(SReader.Peek() > 0) 
    { 
     // Read the line 
     string CurrentLine = SReader.ReadLine(); // if you call this multiple times in this loop you proceed multiple lines in the file.. 

     // Create an new instance of the 'Instance' class 
     Instance CurrentInstance = new Instance(); 

     // Assign the 'Name' property 
     CurrentInstance.Name = CurrentLine; 

     // Add class instance to the list 
     AllInstances.Add(CurrentInstance); 
    } 

    // Close the stream 
    SReader.Close(); 
} 

在結束時,你就會有「實例」類的列表。有關列表的詳細信息:

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

而且關於StreamReader的:

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

0

數組是真的只有當你知道你需要多少時間提前適當的,如果你不會插入或刪除元素。即使如此,像List這樣的集合對象通常也是可取的。

你可以做這樣的事情:

// Convention in C# is to use properties instead of fields for something like this 
// also, having the class name in the field name is redundant 
public class Instance 
{ 
    public string Name {get;set;} 
    public string Stop {get;set;} 

    public string Restart {get;set;} 
    public string Backup {get;set;} 
} 

public static void Main(string[] args) 
{ 
    List<Instance> items = new List<Instance>(); 

    // the using block will close the file handle 
    using (System.IO.StreamReader file = new System.IO.StreamReader(@"C......")) 
    { 
     while(true) 
     { 
      String name = file.ReadLine(), stop = file.ReadLine(), restart = file.ReadLine(), backup = file.ReadLine(); 
      if (name == null || stop == null || restart == null || backup == null) 
       break; // I didn't test it, but this should work for determining the end of the file 
      items.Add(new Instance(){ 
       Name = name, 
       Stop = stop, 
       Restart = restart, 
       Backup = backup 
      }); 
     } 
    } 
} 

如果你需要能夠找到值特定的名稱,有兩種方法。一種是通過比較Name屬性並存儲List索引來循環。這樣做(儘管它不是那麼容易,一個新來閱讀)的更地道的方式是:

String nameToFind = "..."; 
String stop = items.Where(item => item.Name == nameToFind).FirstOrDefault().Stop; 

注意FirstOrDefault將返回null,如果沒有這樣的元素被發現,在這種情況下.Stop提領將拋出一個異常。另一方面,如果您確實希望通過名稱對整個數據結構進行索引,則List(或數組)可能不是最好的選擇。另一種解決辦法是:

public class Instance2 
{ 
    public string Stop {get;set;} 
    public string Restart {get;set;} 
    public string Backup {get;set;} 
} 

Dictionary<String, Instance2> items = new Dictionary<String, Instance2>(); 

// ... 

items[name] = new Instance2(){Stop = stop, 
    Restart = restart, 
    Backup = backup}; 

有了這樣的數據結構,按名稱查找效率要高得多(O(log(n))相比O(n))。你會只是做:

String nameToFind = "..."; 
String stop = items[nameToFind].Stop; 
+0

我喜歡這個,但是在它被讀入列表之後,我需要能夠搜索「名稱」並在「停止」中返回值。如果列表未被編入索引,我將如何搜索名稱並在Stop中返回字符串。也許我只是不明白列表概念?因爲它看起來像沒有名字的一長串對象,停止,重新啓動和備份被捆綁在一起。 – 2013-05-09 14:17:46

+0

已更新。列表很容易搜索。 – bmm6o 2013-05-09 15:32:42

+0

我不確定你的意思是他們被綁在一起。這些相關數據片段都存在於同一個對象中。也許在調試器中運行它,你可以看到List或Dictionary的外觀。 – bmm6o 2013-05-09 15:40:15

0

您有幾種選擇,如果你知道實例的數量事先你可以使用一個數組,但除此之外,你不妨使用List<Instance>

public static void main(string[] args) 
{ 
    List<Instance> instancelist = new List<Instance>(); 

    System.IO.StreamReader file = new System.IO.StreamReader(@"C......"); 
    while (! file.EndOfStream) // rather than while(true) which never stops 
    { 
      // Even if this were in an array, instancelist[i].Instance_name would be a null pointer exception, 
      // So we create the instance and then add it to the list 
      var instance = new Instance(); 
      instance.Instance_name = file.ReadLine(); 
      //... etc 

      instancelist.Add(instance); 
    } 
}