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
Solved

C# Class issue

Posted on 2014-12-22
2
184 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
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 35

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

839 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