C
C#

help

✅ i want to save several list to text file

SDSam DZ12/26/2022
I have many List<t> and want to save their values to one text file how can i do it
AAngius12/26/2022
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?
SDSam DZ12/26/2022
@Angius Sorry but i didn't know how to do it
SDSam DZ12/26/2022
@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
AAngius12/26/2022
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
SDSam DZ12/26/2022
So how to copy list items to this?
FFirehawk12/26/2022
There's plenty of ways you can go about serializing that data - could do it csv-style, or even json depends on your needs
SDSam DZ12/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
AAngius12/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"
},
};
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);
SDSam DZ12/26/2022
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?
AAngius12/26/2022
Makes no sense
SDSam DZ12/26/2022
It's called junior solutions hahahah
AAngius12/26/2022
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 }
]
?
SDSam DZ12/26/2022
Of course second one
AAngius12/26/2022
Exactly
SDSam DZ12/26/2022
But the deadline tomorrow and i don't understand the first way Literally I'm going to cry
AAngius12/26/2022
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...
});
}
SDSam DZ12/26/2022
Ok And save this final one to text file instead of bunch of Lists
AAngius12/26/2022
ye
SDSam DZ12/26/2022
I have an Idea if it possible instead of edit all code can i copy the bunch of Lists to this one ? @Angius
AAngius12/26/2022
Yes, I even showed you how
SDSam DZ12/26/2022
What is the namespace for saving to text command @Angius I found it ! Put how i will put the path ?
AAngius12/26/2022
my-data.json is the path Adjust as you need
SDSam DZ12/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 @Angius
AAngius12/26/2022
I take it, that everything works now? Nice, glad to hear
SDSam DZ12/26/2022
Yes everything works 🙌🏻

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
✅ how to move it to abstract classI want to move admin user name & adminpass to abstract class or method The point is how to compare ❔ ✅ Applying math to programmingHi, this is not necessarily a C# question and more question towards math and programming. I am using❔ Blazor webassembly app hosted ASP.NET Core 7 test with WebApplicationFactoryI have a Blazor webassembly app hosted in ASP.NET Core 7 that I want to run some tests on. I'm tryi❔ Output informationI have two constructors that take a surname or surname and a collection with salaries . How can I ge❔ 2 Errors in image downloadingMy code: ```cs private void button11_Click(object sender, EventArgs e) { saveFil✅ write multiple Lists to fileI want to write several items from lists that have same index to text file, what is the easiest way❔ Poor DataGrid performance when using DataTableI'm writing a program that can take any number of different tab delimited csv files, load them into ❔ CSharp Socket Stream StacksThe stream buffer stacks data, instead of receiving, processing, repeating loop. ```csharp ❔ xmlhow to make to my program reads only elements inside "Officer I"?❔ hey i need guidance from a .net expertplease❔ reverse of RemoveAt[index]hi, so my problem is that im itterating through a list of 10 questions, and each time the user has cSome namespace and class questions..```css namespace Cool { class VeryCool { static void main(string[] args) { ✅ Texas holdem pokerHi, I need help with my project to school. My task is to create Texas holdem poker. But I dont know ❔ Sockets - TCPI'm going to implement a TCP listener for RF-V48 (4G GPS Tracking Bracelet) for elders. The device i❔ Problems with colliders...Context: when a game object with tag "Player" touch other object, print in console "Func". But that ❔ .NET6 Selenium 4. it’s possible to use Navigate.GoToUrl(url) using http Post?Hi, Im was searching around a solution or approach for use a POST method instead GET when using GoTo❔ Multi-tenant == Roles ?I need clarification whether the RoleManager in IdentityDbContext that manages roles for user author❔ Custom template not showing upI'm trying to create a new custom .csproj template for .NET 7, but I can not seem to get it to show ❔ Is it possible to put a method into a variable and call the variable?like this:❔ Getting error 500 on form post (using fetch)I'm trying to do a form post (posting a model), but my controller is not being called. When I check