Solved

C# Class issue

Posted on 2014-12-22
2
191 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:Michael74
Michael74 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

710 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