C
C#2y ago
Sam DZ

✅ 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
27 Replies
Angius
Angius2y ago
Didn't you ask about it before, and were advised to just use proper classes instead of lists of properties? Indeed, #✅ write multiple Lists to file Something doesn't work still?
Sam DZ
Sam DZ2y ago
@Angius Sorry but i didn't know how to do it
MODiX
MODiX2y ago
TheRanger#3357
something like
public class Ship
{
public string Name {get;set;}
public decimal Price {get;set;}
public int Quantity {get;set;}

public string Address {get;set;}
public string ShippingType {get;set;}
public string PayType {get;set;}
}
public class Ship
{
public string Name {get;set;}
public decimal Price {get;set;}
public int Quantity {get;set;}

public string Address {get;set;}
public string ShippingType {get;set;}
public string PayType {get;set;}
}
and in ur class Shipping
class Shipping
{
public static List<Ship> Ships = new List<Ship>();
}
class Shipping
{
public static List<Ship> Ships = new List<Ship>();
}
Quoted by
React with ❌ to remove this embed.
Sam DZ
Sam DZ2y ago
@Angius 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
Angius2y ago
You don't use them, not directly
var s = new Ship();
s.Name = "Carrots"; // this uses the setter
var n = s.Name; // this uses the getter
var s = new Ship();
s.Name = "Carrots"; // this uses the setter
var n = s.Name; // this uses the getter
Sam DZ
Sam DZ2y ago
So how to copy list items to this?
Firehawk
Firehawk2y ago
There's plenty of ways you can go about serializing that data - could do it csv-style, or even json depends on your needs
Sam DZ
Sam DZ2y ago
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
Angius2y ago
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"
},
};
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"
},
};
This is how you make a list of those classes 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"
});
ships.Add(new Ship {
Name = "Carrots",
Price = 12.9,
Quantity = 89,
Address = "18 Boulevard Avenue, Boroughshire",
ShippingType = "Super Ultra Express",
PayType = "Bags of rye"
});
And saving it to a file could not be easier
var json = JsonSerializer.Serialize(ships);
File.WriteAllText("my-data.json", json);
var json = JsonSerializer.Serialize(ships);
File.WriteAllText("my-data.json", json);
Sam DZ
Sam DZ2y ago
Oh my god I going to jump from the mountain Thank you a lot ❤️ @Angius What do you think about save each list separately and then combine the files in one?
Angius
Angius2y ago
Makes no sense
Sam DZ
Sam DZ2y ago
It's called junior solutions hahahah
Angius
Angius2y ago
Do you keep track of your phone contacts with
names = [ "bob", "anna", "joel" ]
numbers = [ 12343245, 4567567, 4564564 ]
names = [ "bob", "anna", "joel" ]
numbers = [ 12343245, 4567567, 4564564 ]
or
people = [
{ name: "bob", number: 12343245 },
{ name: "anna", number: 4567567 },
{ name: "joel", number: 4564564 }
]
people = [
{ name: "bob", number: 12343245 },
{ name: "anna", number: 4567567 },
{ name: "joel", number: 4564564 }
]
?
Sam DZ
Sam DZ2y ago
Of course second one
Angius
Angius2y ago
Exactly
Sam DZ
Sam DZ2y ago
But the deadline tomorrow and i don't understand the first way Literally I'm going to cry
Angius
Angius2y ago
Whelp, happens I guess you can turn your weird bunch of lists into a list of objects
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...
});
}
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...
});
}
Sam DZ
Sam DZ2y ago
Ok And save this final one to text file instead of bunch of Lists
Angius
Angius2y ago
ye
Sam DZ
Sam DZ2y ago
I have an Idea if it possible instead of edit all code can i copy the bunch of Lists to this one ? @Angius
Angius
Angius2y ago
Yes, I even showed you how
Sam DZ
Sam DZ2y ago
What is the namespace for saving to text command @Angius I found it ! Put how i will put the path ?
MODiX
MODiX2y ago
Angius#1586
var json = JsonSerializer.Serialize(ships);
File.WriteAllText("my-data.json", json);
var json = JsonSerializer.Serialize(ships);
File.WriteAllText("my-data.json", json);
Quoted by
React with ❌ to remove this embed.
Angius
Angius2y ago
my-data.json is the path Adjust as you need
Sam DZ
Sam DZ2y ago
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 @Angius
Angius
Angius2y ago
I take it, that everything works now? Nice, glad to hear
Sam DZ
Sam DZ2y ago
Yes everything works 🙌🏻