Solved

C# Class issue

Posted on 2014-12-22
2
195 Views
Last Modified: 2014-12-23
Hi see code below, i like to know this is write way to do that. code is working fine, but i feel something is not right.

My purpose is this code is  - in the text file has 3 lines, i need to access this 3 file  one by one.and need to put 3 variables.

see below code
string line = string.Empty;
Class ClientFiles
{

public  List<string []>  ClientSaveFileDetails()
{
List<string []> clientFileList = new List<string []>();
SaveFilename=@"C:\test.txt";
   using (StreamReader clientRead = new StreamReader(SaveFilename))
            {
                while ((line = clientRead.ReadLine()) != null)
                {
                    clientFileList.Add(new string[] { line });
                }
            }

            return clientFileList;
}



}


I have already called the above class using below code

ClientFiles cl=new ClientFiles();
List<string[]> clSitename = cl.ClientSaveFileDetails();


string rowdetail1=clSitename[0][0];//1st line of the file
string rowdetail2=clSitename[1][0];//2st line of the file
string rowdetail3=clSitename[2][0];//3st line of the file


Console.writeline(rowdetail1);

Console.writeline(rowdetail2);


Console.writeline(rowdetail3);

Open in new window


can some one show me its not right way? if yes please show me what is the best way to do that.thx
0
Comment
Question by:ukerandi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Assisted Solution

by:Michael Fowler
Michael Fowler earned 250 total points
ID: 40514280
Not sure why you are using a string array for the lines of the file, a string variable will work just fine. Other than this I would also tend to declare the file name as an class variable to make editing it easier.

Class ClientFiles
{
   private string saveFileName = @"C:\test.txt";
   
   public  List<string>  ClientSaveFileDetails()
   {
      List<string> clientFileList = new List<string>();
      using (StreamReader clientRead = new StreamReader(saveFilename))
      {
             while ((line = clientRead.ReadLine()) != null)
             {
                 clientFileList.Add(line);
             }
       }

        return clientFileList;
   }
}

Open in new window

0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 40514342
A simple approach to access those strings, use  the  File.ReadAllLines method:

public  List<string>  ClientSaveFileDetails(string saveFileName)
{
  string[] readText = File.ReadAllLines(saveFileName);
  return new new List<string>(readText);
}

Open in new window

Note: There is no need to use StreamReader.
Usage:
ClientFiles cl=new ClientFiles();
List<string> clSitename = cl.ClientSaveFileDetails(@"C:\test.txt");
//Your existing code goes here

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question