2011-08-08 80 views
1

因此,我正在研究一個C#程序,該程序在目錄中接收一組分隔文本文件並解析文件中的信息(即文件路徑,文件名,關聯的關鍵字)。這是一個什麼樣的文件看起來像......如何訪問另一個類中的一個類的變量? [C#]

C:\Documents and Settings\workspace\Extracted Items\image2.jpeg;image0;keyword1, keyword2, keyword3, keyword4 
C:\Documents and Settings\workspace\Extracted Items\image3.jpeg;image1;keyword1, keyword2, keyword3, keyword4 
C:\Documents and Settings\workspace\Extracted Items\image4.jpeg;image2;keyword1, keyword2, keyword3, keyword4 
C:\Documents and Settings\workspace\Extracted Items\image5.jpeg;image3;keyword1, keyword2, keyword3, keyword4 

嗯,我給我的合作伙伴,這是否一些代碼,但我需要能夠訪問列表中的變量,那就是一個內填充方法。這是代碼:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApp 
{ 
    public class FileIO 
    { 
     private static Boolean isTextFile; 
     private static Boolean debug; 

     private static int semiColonLoc1, semiColonLoc2, dblQuoteLoc1; 
     private static int lineLength, currentTagLength; 
     private static int numImages; 
     private static int numFiles; 
     public static List<Image> lImageSet; 

     /* 
      **************************************************** 
      ***** CHANGE THIS PATH TO YOUR PROPERTIES FILE ***** 
      **************************************************** 
     */ 
     private static readonly string propertiesFileDir = "C:/Documents and Settings/properties.properties"; 

     public PropertyKeys getProperties(string propertiesFileDir, PropertyKeys aPropertyKeys) 
     { 
      string line; 
      string directoryKey = "extractedInfoDirectory"; 
      string debugKey = "debug2"; 
      string directory; 

      Boolean isDirectoryKey; 
      Boolean isDebugKey; 

      System.IO.StreamReader file = new System.IO.StreamReader(propertiesFileDir); 

      while ((line = file.ReadLine()) != null) 
      { 

       isDirectoryKey = false; 
       isDebugKey = false; 

       // If the current line is a certain length, checks the current line's key 
       if (line.Length > debugKey.Length) 
       { 
        isDebugKey = line.Substring(0, debugKey.Length).Equals(debugKey, StringComparison.Ordinal); 

        if (line.Length > directoryKey.Length) 
        { 
         isDirectoryKey = line.Substring(0, directoryKey.Length).Equals(directoryKey, StringComparison.Ordinal); 
        } 
       } 

       // Checks if the current line's key is the extractedInfoDirectory 
       if (isDirectoryKey) 
       { 
        directory = line.Substring(directoryKey.Length + 1); 
        aPropertyKeys.setExtractedInfoDir(directory); 
       } 

       // Checks if the current line's key is the debug2 
       else if (isDebugKey) 
       { 
        debug = Convert.ToBoolean(line.Substring(debugKey.Length + 1)); 
        aPropertyKeys.setDebug(debug); 
       } 
      } 

      return aPropertyKeys; 
     } 

     public void loadFile() 
     { 

      string line; 
      string tempLine; 
      string fileToRead; 
      string fileRename; 
      string imagePath, imageName, imageTags, currentTag; 
      string extractedInfoDir; 
      string extension; 
      string textfile = "txt"; 
      string[] filePaths; 

      PropertyKeys aPropertyKeys = new PropertyKeys(); 

      // Finds extractedInfoDir and debug values 
      aPropertyKeys = getProperties(propertiesFileDir, aPropertyKeys); 
      extractedInfoDir = aPropertyKeys.getExtractedInfoDir(); 
      debug = aPropertyKeys.getDebug(); 

      // Finds all files in the extracted info directory 
      filePaths = Directory.GetFiles(extractedInfoDir); 
      numFiles = filePaths.Length; 

      // For each file in the directory... 
      for (int n = 0; n < numFiles; n++) 
      { 
       int k = filePaths[n].Length; 

       // Finds extension for the current file 
       extension = filePaths[n].Substring(k - 3); 

       // Checks if the current file is .txt 
       isTextFile = extension.Equals(textfile, StringComparison.Ordinal); 

       // Only reads file if it is .txt 
       if (isTextFile == true) 
       { 

        fileToRead = filePaths[n]; 
        Console.WriteLine(fileToRead); 
        System.IO.StreamReader file = new System.IO.StreamReader(fileToRead); 

        // Reset variables and create a new lImageSet object 
        lImageSet = new List<Image>(); 

        line = ""; tempLine = ""; imagePath = ""; imageName = ""; imageTags = ""; currentTag = ""; 
        semiColonLoc1 = 0; semiColonLoc2 = 0; dblQuoteLoc1 = 0; lineLength = 0; currentTagLength = 0; numImages = 0; 

        while ((line = file.ReadLine()) != null) 
        { 

         // Creates a new Image object 
         Image image = new Image(); 
         numImages++; 

         lineLength = line.Length; 

         // Finds the image path (first semicolon delimited field) 
         semiColonLoc1 = line.IndexOf(";"); 
         imagePath = line.Substring(0, semiColonLoc1); 
         image.setPath(imagePath); 

         tempLine = line.Substring(semiColonLoc1 + 1); 

         // Finds the image name (second semicolon delimited field) 
         semiColonLoc2 = tempLine.IndexOf(";"); 
         imageName = tempLine.Substring(0, semiColonLoc2); 
         image.setName(imageName); 

         tempLine = tempLine.Substring(semiColonLoc2 + 1); 

         // Finds the image tags (third semicolon delimited field) 
         imageTags = tempLine; 

         dblQuoteLoc1 = 0; 

         // Continues to gather tags until there are none left 
         while (dblQuoteLoc1 != -1) 
         { 
          dblQuoteLoc1 = imageTags.IndexOf("\""); 
          imageTags = imageTags.Substring(dblQuoteLoc1 + 1); 
          dblQuoteLoc1 = imageTags.IndexOf("\""); 

          if (dblQuoteLoc1 != -1) 
          { 
           // Finds the next image tag (double quote deliminated) 
           currentTag = imageTags.Substring(0, dblQuoteLoc1); 
           currentTagLength = currentTag.Length; 

           // Adds the tag to the current image 
           image.addTag(currentTag); 
           image.iNumTags++; 
           imageTags = imageTags.Substring(dblQuoteLoc1 + 1); 
          } 
         } 

         // Adds the image to the current image set 
         lImageSet.Add(image); 

        } 

        // Prints out information about what information has been stored 
        if (debug == true) 
        { 
         Console.WriteLine("Finished file " + (n + 1) + ": " + filePaths[n]); 

         for (int i = 0; i < numImages; i++) 
         { 
          Console.WriteLine(); 
          Console.WriteLine("***Image " + (i + 1) + "***"); 
          Console.WriteLine("Name: " + lImageSet.ElementAt(i).getName()); 
          Console.WriteLine("Path: " + lImageSet.ElementAt(i).getPath()); 
          Console.WriteLine("Tags: "); 

          for (int j = 0; j < lImageSet.ElementAt(i).iNumTags; j++) 
          { 
           Console.WriteLine(lImageSet.ElementAt(i).lTags.ElementAt(j)); 
          } 

         } 
        } 

        file.Close(); 

        // Changes destination file extension to .tmp 
        fileRename = fileToRead.Substring(0, fileToRead.Length - 4); 
        fileRename += ".tmp"; 

        // Changes file extension to .tmp 
        System.IO.File.Move(fileToRead, fileRename); 
       } 

       // Not a text file 
       else 
       { 
        Console.WriteLine("Skipping file (no .txt extension)"); 
       } 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

但是,我不想惹他的代碼太多,因爲他暫時不在這裏解決任何問題。所以我只想知道如何在他的代碼中訪問另一個類中的lImageSet。我希望它會像使用FileIO fo = new FileIO實例化FileIO,然後做類似fo.loadFile().lImageSet的事情,但情況並非如此。有任何想法嗎?

+0

爲什麼所有成員都是靜態的?如果你有這個類的多個實例...? –

回答

1

這是公共 - 所以從你的類,你可以訪問它:

FileIO.lImageSet 

要得到它的價值,只是遍歷它:

//from FishBasketGordo's answer - load up the fo object 
FileIO fo = new FileIO(); 
fo.loadFile(); 

foreach(var img in FileIO.lImageSet) { 
    //do something with each img item in lImageSet here... 
} 

編輯:我建立在FishBasketGordo的回答中,將他的loadFile()調用加入我的示例中。

+0

所以在foreach循環中,如果你注意到OP在向底部發布的代碼中有如何使用'Console.WriteLine(「Name:」+ lImageSet.ElementAt(i).getName());',我無法訪問「ElementAt」方法,那麼我如何到達列表中我想要的位置? –

+0

Nvm,我弄明白了。謝謝你和其他人的幫助!自從你加倍努力之後,我會給你答案,並在那裏添加這個foreach。 :) –

+0

謝謝 - 很高興我能幫上忙。 –

3

由於lImageSet是靜態的,所有你需要做的訪問,它是:

List<image> theList = FileIO.lImageSet;

沒有實例化的對象是需要得到該領域的參考。

2

該列表是靜態的,所以你用的名稱,該類訪問:

List<Image> theList = FileIO.lImageSet 
2

loadFile方法返回void,所以你不能用點運算符來從中獲取任何東西。你會想要做這樣的事情:

FileIO fo = new FileIO(); 
fo.loadFile(); 
List<Image> theImages = FileIO.lImageSet; 
+0

+1提醒我,我沒有加載FileIO obj。 –

1

因爲lImageSetstatic,這樣你就不會需要實例FileIO專注得到它。

嘗試FileIO.lImageSet

相關問題