简体   繁体   中英

How to make an array of fruit from a txt file with each fruit have four properties in C#

So here is the text file for example:

Apple, red, crunchy
Orange, orange, juicy
Watermelon, green, juicy

Okay so I can get C# to read each line and put it into an array. What I need is for C# to look at it and make and array of objects called fruit 3 items long. So for the first item in the array I want to be able to access each individual entry instead of it all as just one string. The idea would be that then I could send each property into a class to make the object.

So assuming I have creating the FileStream and that stuff.

Fruit[] fruits = new Fruit[3];

Now I want to take the three entries into the first line and send it to the constructor for Fruit.

Any help would be greatly appreciated Hopefully I am clear enough.

This question isn't well focused. There's several things you need to do.

First, add your text file to your project (if you are using visual studio, right click the file and "Copy to output directory". Otherwise place it next to your .exe in your /bin folder.

Then use System.IO.ReadLines (research this) to get the contents of the file. https://www.dotnetperls.com/file-readlines

This will give you an array of strings, each array contains a fruit item with all the properties.

Iterate over that array and use string.Split(',') to parse that string into a string array with the Fruit and properties in each index. https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.7.2

Once you have the properties indexed, you can loop through it and create a new Fruit() { Name=string2dArray[i][0], Prop2 =string2dArray[i][1], ... } https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer

You need to take the time to understand how to do these steps, I don't think anyone here can (or should) do it for you.

You can simply use Linq to split items:

IEnumerable<string[]> items = File.ReadAllLines("filename.txt").Select(x=> x.Split(','));
Fruit[] fruits = items.Select(x=> new Fruit{Name = x[0], Color = x[1], Type = x[2]}).ToArray();

you have to write the fruit class, make a new file in your project called Fruit.cs

then this should do the trick:

class Fruit
{
    string fruitName { get; set; }
    string desc1 { get; set; }
    string desc2 { get; set; }
    string desc3 { get; set; }
    string desc4 { get; set; }

    public Fruit(string name,string description1,string description2)
    {
        this.fruitName = name;
        this.desc1 = description1;
        this.desc2 = description1;
    }


}

i think should move you in the right direction. Zakk Diaz is correct though, you will have to parse the text, line by line, into these fruit objects. then add them to your array. if possible i would use List<Fruit> instead of array, but thats just me.

it would look like this:

List<Fruit> fruits = new List<Fruit>();

        Fruit addfruit = new Fruit(firstLineOfParsed(name), secondLineOfParsed(desc1), thirdLineOfParsed(desc2));
        fruits.Add(addfruit);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM