Solved

C# Class issue

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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