Home > database >  How i can assign result methods in variable? C#
How i can assign result methods in variable? C#

Time:09-25

Code:

using System;
using System.IO;

namespace TimeTress
{
    class Program
    {
        static void Main(string[] args)
        {
            GetString("../../../../timeline.csv");
            GetString("../../../../people.csv");
        }

        static void GetString(string path)
        {
            if (File.Exists(path))
            {
                foreach (var line in File.ReadAllLines(path))
                {
                    Console.WriteLine(line);
                }
            }
            else
            {
                Console.WriteLine($"Файл не найден по пути {Path.GetFullPath(path)}");
            }
        }
    }
}

It is necessary that the result should not be simply displayed, but written into two different variables, preferably into the arrays string [] [] or string [], so that you can work with them in the future. File text: timeline: {event_date}; {event_description} people: {Name}; {Date of Birth}; {Date of death}

CodePudding user response:

Change the method's return type to string[] or List<string> (if you don't know the difference, use Google)

then, instead of:

Console.WriteLine(line);

create a list and add lines to it:

List<string> result = new ListString();
foreach (var line in File.ReadAllLines(path))
{
    result.Add(line);
}
return line; // or `lines.ToArray()`, if your return type is `string[]`

Now you need to think about the return value when the file does not exist. One option would be returning null. But that's not good and forces the users (of the method, I mean, the programmers) to check for null result whenever the method is called. A better option in my opinion is not checking for the existence of the file. Yes, you heard me right. Let the FileNotFoundException be thrown.

For more elaborate ways of processing the CSV file and parsing fields, consult other posts on the internet, including the ones available in StackOverflow

CodePudding user response:

First, the variable(s) need to be defined before a value can be assigned to the variable(s). This variable definition is done using a statement similar to the following:

string[] stringArray;

After defining a variable, it is possible to assign values to the intended variable(s), as shown below:

stringArray = new string[] { "message one", "message two" };

Building upon this, if one would like to read through the lines in a text file, assign the lines to an array of strings, and then return a variable containing that array of strings, the following function would be one way to accomplish this:

static string[] GetListOfStringsFromTextFile(string filePath)
{
    string[] stringArray;

    if (File.Exists(filePath))
    {
        // ReadAllLines() already returns a string array. No need to loop.
        stringArray = File.ReadAllLines(filePath);
    }
    else
    {
        // If file not found, return empty string array as default.
        stringArray = new string[0];
    }
    
    return stringArray;
}
  • Related