Hack Forums
[C#] Need to store an array of two strings - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Need to store an array of two strings (/showthread.php?tid=5427304)



Need to store an array of two strings - .Bux - Oct 8, 2016

Hey

I'm making my first real tool in C# (which isn't my main language), and at some part I need to store an undefined number of usernames and passwords to get those values later, in another function.

At the moment I found this on google:

PHP Code

<?php 
List<KeyValuePair<string, string>> _items = new List<KeyValuePair<string, string>>(); 

A list of two strings, in which I add "working accounts" in a loop

PHP Code

<?php 
_items
.Add(new KeyValuePair<string, string>(username, passwords)); 

It works like a charm, but now that I want to display what's in that list (to check if I filled it earlier), I don't really know how to manipulate this "two dimensional list".

Maybe I picked the hard way, what do you think?

Basically my program works like this:

The user enters a list of usernames/password
Then I check if they're working or not one by one in a loop, and I'd like to store the working accounts in that loop
Final step is to save those working accounts in a txt file


* - BahNahNah - Oct 8, 2016

Use a dictionary or use a list of a a custom object that has a username and password property.


* - Ninja - Oct 8, 2016

What BahNahNah said, a dictionary would be better for this.
But if you insist on doing it this way; to get an item from the list, grab the KeyValuePair at the index you want and then interact with that.


* - .Bux - Oct 9, 2016

Thanks for your anwser, I'll learn how to use Dictionnaries then


* - hsotaka - Oct 10, 2016

Like BahNahNah said, use a dictionary!
BUT: a dictionary is composed of a key and a value. The key is your identifier in the dictionary, so be sure you don't add 2 keys with the same value.
If you have multiple entries with the same value, its not recommended to it as a disctionary key.
In this case, like BahBahBah said too, create a object like "Account" and a list which contains multiple "Accounts".
This could be a simple class with two string properties, for your example.

Sorry for bad english, hope it helps.


* - 0xDEAD10CC - Oct 10, 2016

(Oct 10, 2016 01:50 AM)hsotaka Wrote:  Like BahNahNah said, use a dictionary!
BUT: a dictionary is composed of a key and a value. The key is your identifier in the dictionary, so be sure you don't add 2 keys with the same value.
If you have multiple entries with the same value, its not recommended to it as a disctionary key.
In this case, like BahBahBah said too, create a object like "Account" and a list which contains multiple "Accounts".
This could be a simple class with two string properties, for your example.

Sorry for bad english, hope it helps.

There's no "BUT" here, what he is trying to do with a list is essentially what a Dictionary does best already. It's a list of key-value pairs, the only difference is that a Dictionary will only store one unique key per value and indexing by key will access the value associated with it.


* - krumpez - Oct 10, 2016

Edit : sry, If you do not mind the items being imutable you can use the Tuple class added to .net 4

ar list = new List<Tuple<string,string>>();
list.Add(new Tuple<string,string>("hello", "world"));

list[0].Item1 //Hello
list[0].Item2 //world

or as previously suggested use a dictionary

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("string", "string2");


* - Ninja - Oct 10, 2016

(Oct 10, 2016 10:44 AM)krumpez Wrote:  u can use an arraylist also

...