2011-08-08 12 views
1

私はディレクトリ内の区切られたテキストファイルのセットを取り、ファイル内の情報(つまり、ファイルパス、ファイル名、関連キーワード)を解析するC#プログラムに取り組んでいます。そして、これはまあ、私はこれを行う私のパートナーによっていくつかのコードを与えられたが、私はリスト変数にアクセスできるようにする必要があり、それはの1つに移入され 別のクラスのあるクラスから変数にアクセスする方法[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

それは公共のです - ので、あなたのクラスから、あなただけのようにアクセスすることができます。

//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... 
} 

EDIT:

FileIO.lImageSet 

その中の値を取得するには、同じようにそれを反復します:私はFishBasketGordoの答えを自分のloadFile()呼び出しをサンプルに組み込むことで構築しました。あなたはクラスの名前でそれにアクセスするよう

+0

foreachループでは、OPの下に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を返すので、あなたはそれから何かをアクセスするためにドット演算子を使用することはできません。 lImageSetstaticあるので、あなたはそれを得るためにFILEIOをインスタンス化する必要はありませんので

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

+1私はFileIO objの読み込みをしなかったことを思い出させるために+1。 –

1

:あなたはこのような何かをしたいと思います。

試行FileIO.lImageSet

関連する問題