Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I'm working in a personnel control with WinForms. Each person has its proper username and a code of three digits(a random number between 000 and 999), when the user enters its code to a textbox, the software should look for the matching username.

I'm storing the usernames and codes in a simple text file like this:
ChrisHD22 123
ChrisCreateBoss 846
Unknown 067

Now, as said before, if a user enters, for example, 846, the application should automatically show ChrisCreateBoss entered!. The problem is: I don't know how to manage the two data values(username and code) ,so they will match.

Maybe with a string[]Users and a string[]Codes, but I don't know how to divide the values into each string array. I could use a List<string> Users and one for Codes too, but the same thing, I can't organize the data.

Thanks in Advance - CCB
Posted

Assuming, you have a unique code for each username.

Use C# Dictionary<TKey,TValue>, and add you codes as keys and usernames as values into the dictionary.

Your dictionary will look something like:
C#
Key             Value
123             ChrisHD22
846             ChrisCreateBoss
067             Unknown


By doing this, you can search for key entered in the textbox and fetch the its corresponding value from the dictionary.

Check the links below to see how it works:
https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^]
http://www.dotnetperls.com/dictionary[^]
 
Share this answer
 
v3
Comments
Krunal Rohit 16-Jun-15 2:00am    
I'd rather suggest HashTable.

-KR
George Jonsson 16-Jun-15 2:15am    
The Hashtable class should in most cases not be used anymore as it is a bit slow and not typed.
George Jonsson 16-Jun-15 2:17am    
In this case it might be better to set the random number as the key and the user name as the value as the user seems to mostly enter the number for searching.
Abhipal Singh 16-Jun-15 3:06am    
Thanks for pointing out George!
Actully I meant to do that only, please see the answer description. However, I wrote the column headers wrong.. (copy paste mess) :p

Will update the answer
George Jonsson 16-Jun-15 3:08am    
I mostly looked at the table and missed the text. I guess most ppl might do the same.
Hi You can use enumerator (enum) which matches name-number which you require.

ex,
C#
public enum usernames
   {
       ChrisHD22= 123,
       ChrisCreateBoss= 846,
       Unknown= 067
   }


some links for help:

http://stackoverflow.com/questions/309333/enum-string-name-from-value[^]

http://www.csharp-station.com/Tutorial/CSharp/lesson17[^]
 
Share this answer
 
v2
Comments
George Jonsson 16-Jun-15 2:13am    
With this solution, what happens if the OP wants to add more user names?
Well, he needs to update the enum, recompile the application and distribute a new version. Not a very elegant solution.
Enums are good in the right context, but maybe not in this case.
Abhipal Singh 16-Jun-15 3:10am    
Also, the question is to read a text file on runtime and search the key from text box. I am afraid that this solution will not work as enum is static and text file data will not be converted to enum on runtime.
Member 11414035 18-Jun-15 5:58am    
Abhipal have u ever use enum, the answer is quit related to query as I have used the same. do you have some atic wit but I think you don't have that.
Abhipal Singh 18-Jun-15 7:48am    
Yes, I have used enums, a lot :)

There are somethings called best practices. Enums are used to define static things and not dynamic data loaded from file, It does not make any sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900