C#
C#

help

Root Question Message

It's_Sam
It's_Sam12/26/2022
✅ i want to save several list to text file

I have many List<t> and want to save their values to one text file how can i do it
Angius
Angius12/26/2022
Didn't you ask about it before, and were advised to just use proper classes instead of lists of properties?
Angius
Angius12/26/2022
Indeed, #1056981630998294568
Angius
Angius12/26/2022
Something doesn't work still?
It's_Sam
It's_Sam12/26/2022
@85903769203642368
Sorry but i didn't know how to do it
It's_Sam
It's_Sam12/26/2022
@85903769203642368
I don't know how to use getters and setters to do it
I need fully explained code for it with getters and setters
Angius
Angius12/26/2022
You don't use them, not directly
Angius
Angius12/26/2022
var s = new Ship();
s.Name = "Carrots"; // this uses the setter
var n = s.Name; // this uses the getter
It's_Sam
It's_Sam12/26/2022
So how to copy list items to this?
Firehawk
Firehawk12/26/2022
There's plenty of ways you can go about serializing that data - could do it csv-style, or even json
Firehawk
Firehawk12/26/2022
depends on your needs
It's_Sam
It's_Sam12/26/2022
I'm literally a beginner and this is my first oop so i don't know how to do it unfortunately and Googled it a lot and didn't find what i want
Angius
Angius12/26/2022
var ships = new List<Ship>(){
    new Ship {
        Name = "Carrots",
        Price = 12.9,
        Quantity = 89,
        Address = "18 Boulevard Avenue, Boroughshire",
        ShippingType = "Super Ultra Express",
        PayType = "Bags of rye"
    },
    new Ship {
        Name = "Carrots",
        Price = 12.9,
        Quantity = 89,
        Address = "18 Boulevard Avenue, Boroughshire",
        ShippingType = "Super Ultra Express",
        PayType = "Bags of rye"
    },
};
Angius
Angius12/26/2022
This is how you make a list of those classes
Angius
Angius12/26/2022
If you need to add one outside of the initialization,
ships.Add(new Ship {
        Name = "Carrots",
        Price = 12.9,
        Quantity = 89,
        Address = "18 Boulevard Avenue, Boroughshire",
        ShippingType = "Super Ultra Express",
        PayType = "Bags of rye"
    });
Angius
Angius12/26/2022
And saving it to a file could not be easier
Angius
Angius12/26/2022
var json = JsonSerializer.Serialize(ships);
File.WriteAllText("my-data.json", json);
It's_Sam
It's_Sam12/26/2022
Oh my god
I going to jump from the mountain

Thank you a lot ❤️
@85903769203642368
It's_Sam
It's_Sam12/26/2022
What do you think about save each list separately and then combine the files in one?
Angius
Angius12/26/2022
Makes no sense
It's_Sam
It's_Sam12/26/2022
It's called junior solutions hahahah
Angius
Angius12/26/2022
Do you keep track of your phone contacts with
names = [ "bob", "anna", "joel" ]
numbers = [ 12343245, 4567567, 4564564 ]

or
people = [
    { name: "bob", number: 12343245 },
    { name: "anna", number: 4567567 },
    { name: "joel", number: 4564564 }
]

?
It's_Sam
It's_Sam12/26/2022
Of course second one
Angius
Angius12/26/2022
Exactly
It's_Sam
It's_Sam12/26/2022
But the deadline tomorrow and i don't understand the first way
Literally I'm going to cry
Angius
Angius12/26/2022
Whelp, happens
Angius
Angius12/26/2022
I guess you can turn your weird bunch of lists into a list of objects
Angius
Angius12/26/2022
var ships = new List<Ship>();

for (var i = 0; i < someList.Length, i++)
{
    ships.Add(new Ship {
        Name = namesList[i],
        Price = pricesList[i],
        Quantity = quentitiesList[i],
        // etc...
    });    
}
It's_Sam
It's_Sam12/26/2022
Ok
And save this final one to text file instead of bunch of Lists
Angius
Angius12/26/2022
ye
It's_Sam
It's_Sam12/26/2022
I have an Idea if it possible instead of edit all code can i copy the bunch of Lists to this one ?
@85903769203642368
Angius
Angius12/26/2022
Yes, I even showed you how
It's_Sam
It's_Sam12/26/2022
What is the namespace for saving to text command
@85903769203642368
It's_Sam
It's_Sam12/26/2022
I found it !
Put how i will put the path ?
Angius
Angius12/26/2022
my-data.json is the path
Angius
Angius12/26/2022
Adjust as you need
It's_Sam
It's_Sam12/26/2022
I want to thank you very much
Because off many things
1st you helped me a lot
2nd your idea to clone the lists made me know how to use getters and setters
3rd you saved my back
It's_Sam
It's_Sam12/26/2022
@85903769203642368
Angius
Angius12/26/2022
I take it, that everything works now?
Angius
Angius12/26/2022
Nice, glad to hear
It's_Sam
It's_Sam12/26/2022
Yes everything works 🙌🏻
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy