C
C#7mo ago
Algo

Class Objects Aren't Working Correctly?

Doesn't seem like anything is appending to the arrays. When i call it in the mainform. it doesn't pull anything. am i missing anything here? My Auth Class Code Snippet from its Constructor
VPNInfo[] avpns = new VPNInfo[] { };
this.raw_vpns = new string[] { };
int i = 0;

foreach (string line in vpns)
{
if (line.Contains("db.VPN{"))
{
string name = vpns[i + 1].Replace("name:", "").Replace("'", "").Trim();
string ip = vpns[i + 2].Replace("ip:", "").Replace("'", "").Trim();
string config = vpns[i + 3].Replace("config:", "").Replace("'", "").Trim();
string access_lvl = vpns[i + 4].Replace("access_level:", "").Replace("'", "").Trim();
VPNInfo vpn = new VPNInfo(name, ip, config, access_lvl);
avpns.Append(vpn);
this.raw_vpns.Append(name);
MessageBox.Show(name); // DEBUGGING CODE
}
i++;
}
VPNInfo[] avpns = new VPNInfo[] { };
this.raw_vpns = new string[] { };
int i = 0;

foreach (string line in vpns)
{
if (line.Contains("db.VPN{"))
{
string name = vpns[i + 1].Replace("name:", "").Replace("'", "").Trim();
string ip = vpns[i + 2].Replace("ip:", "").Replace("'", "").Trim();
string config = vpns[i + 3].Replace("config:", "").Replace("'", "").Trim();
string access_lvl = vpns[i + 4].Replace("access_level:", "").Replace("'", "").Trim();
VPNInfo vpn = new VPNInfo(name, ip, config, access_lvl);
avpns.Append(vpn);
this.raw_vpns.Append(name);
MessageBox.Show(name); // DEBUGGING CODE
}
i++;
}
Main Form code snippet
foreach(VPNInfo vpn in this.CS.User.available_vpns)
{
listBox1.Items.Add($"{vpn.name} | {vpn.ip}");
}
foreach(VPNInfo vpn in this.CS.User.available_vpns)
{
listBox1.Items.Add($"{vpn.name} | {vpn.ip}");
}
8 Replies
Pobiega
Pobiega7mo ago
you cant append to an array arrays are fixed size
Algo
AlgoOP7mo ago
O_o
Pobiega
Pobiega7mo ago
fairly sure your "append" is actually a concat and returns a new array, that you are promptly dropping
Algo
AlgoOP7mo ago
hmmm
Pobiega
Pobiega7mo ago
the better solution is to use a List<T> and list.Add(...)
Algo
AlgoOP7mo ago
ok 1sec let me take a look at an old project i did this way in
Pobiega
Pobiega7mo ago
just doublechecked, array.Append is indeed Enumerable.Append and returns the new sequence public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element)
Algo
AlgoOP7mo ago
i used this instead
Array.Resize(ref this.User.available_vpns, this.User.available_vpns.Length + 1);
this.User.available_vpns[this.User.available_vpns.Length - 1] = vpn;
Array.Resize(ref this.User.available_vpns, this.User.available_vpns.Length + 1);
this.User.available_vpns[this.User.available_vpns.Length - 1] = vpn;
this approach should be a method built-in :picard_facepalm: ty for the help effort tho thread closing
Want results from more Discord servers?
Add your server