C
C#10mo ago
Jef

✅ 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
mtreit
mtreit10mo ago
Make a List?
Jef
JefOP10mo ago
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?
mtreit
mtreit10mo ago
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:
MODiX
MODiX10mo ago
mtreit
REPL Result: Success
using System;
using System.Collections.Generic;
using System.Linq;

var entries = new List<ServerEntry>();
foreach (var entryStr in GetCsvData().Skip(1))
{
var tokens = entryStr.Split(",");
var entry = new ServerEntry(DateTime.Parse(tokens[1]), new Server(tokens[0], tokens[2]));
entries.Add(entry);
}

foreach (var entry in entries)
{
Console.WriteLine(entry);
}

IEnumerable<string> GetCsvData()
{
yield return "Name,Date,Owner";
yield return "AppServer,2024-2-1,Pete";
yield return "DBServer,2023-1-24,Mike";
yield return "OtherServer,2023-1-21,Pete";
}

record Server(string Name, string Owner);
record ServerEntry(DateTime Timestamp, Server Server);
using System;
using System.Collections.Generic;
using System.Linq;

var entries = new List<ServerEntry>();
foreach (var entryStr in GetCsvData().Skip(1))
{
var tokens = entryStr.Split(",");
var entry = new ServerEntry(DateTime.Parse(tokens[1]), new Server(tokens[0], tokens[2]));
entries.Add(entry);
}

foreach (var entry in entries)
{
Console.WriteLine(entry);
}

IEnumerable<string> GetCsvData()
{
yield return "Name,Date,Owner";
yield return "AppServer,2024-2-1,Pete";
yield return "DBServer,2023-1-24,Mike";
yield return "OtherServer,2023-1-21,Pete";
}

record Server(string Name, string Owner);
record ServerEntry(DateTime Timestamp, Server Server);
Console Output
ServerEntry { Timestamp = 02/01/2024 00:00:00, Server = Server { Name = AppServer, Owner = Pete } }
ServerEntry { Timestamp = 01/24/2023 00:00:00, Server = Server { Name = DBServer, Owner = Mike } }
ServerEntry { Timestamp = 01/21/2023 00:00:00, Server = Server { Name = OtherServer, Owner = Pete } }
ServerEntry { Timestamp = 02/01/2024 00:00:00, Server = Server { Name = AppServer, Owner = Pete } }
ServerEntry { Timestamp = 01/24/2023 00:00:00, Server = Server { Name = DBServer, Owner = Mike } }
ServerEntry { Timestamp = 01/21/2023 00:00:00, Server = Server { Name = OtherServer, Owner = Pete } }
Compile: 575.095ms | Execution: 140.386ms | React with ❌ to remove this embed.
Jef
JefOP10mo ago
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
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server