✅ C# Data structure selection
Hello! I am currently trying to write a program and am having difficulty selecting the best way of storing information collected from a CSV file. The CSV contains server related information such as “server name”, “owner”, “date”.
EX/
Name,date,owner,…
AppServer,date,owner,…
DBServer,date2,owner2,…
….(n) for a list of unknown length
I was investigating creating a server object and creating an instance for each server where:
Server server1 = new Server;
Server server2 = new Server;
But I was unable to find a way to make this work as I do not know how many servers will be in the CSV
I attempting looking for a way to put a variable in the name of the object as so:
Server server_name_variable = new Server;
Where I iterate through server_name_variable, assigning the servers actual name as the instance name, but it appears that variable/object names must be known before compile time, so this would not work in c#
I looked into a dictionary class of keys/values, but each CSV row will have a significant number of independent values corresponding to the key, and from my understanding, dictionary is 1 key per 1 pair. So this would not work.
I would like to be able to call methods on particular values in the data structure eg, Server1.Reboot, and it would kick off a reboot
I was really trying to find the best class structure to do this, but I have yet to find a way to make this work using classes. If anyone could guide me in some direction or some examples of a similar type of structure I would much appreciate it. Let me know if I need to elaborate on anything to make the question more clear.
Thanks!
7 Replies
Make a List?
Would I be able to perform the same functionality on list items like calling methods as I would be able to do on objects? I had felt like this was the sort of thing an object was created to do?
A list is just a collection of items. The items in the collection can be your class that represents servers, has methods, etc.
It's how you handle not knowing how many you will have before you run the program.
Here is a trivial little example:
mtreit
REPL Result: Success
Console Output
Compile: 575.095ms | Execution: 140.386ms | React with ❌ to remove this embed.
Yes…. Trivial….😰
Ok so after combing through for a bit and testing, now I see I can access individual values as well from those objects by doing var temp = entries[0].Server.Name
Thats exactly what I was looking for, thanks for the help
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Use the /close command to mark a forum thread as answered