Hi,

I'm not just new to this forum, but I'm also very new to programming and C#... so be warned! I am using a dictionary list to hold my usernames for an application I'm making. It's never actually going to be used, but it's purely to develop my knowledge. Firstly, am I correct in using this to initiate(?) my list?

Dictionary<string, User> users= new Dictionary<string, User>();

As far as I know this creates a list, with items stored with 2 elements; their key, and the object (which can contain lots more data, created using the class User).

So as far as I'm aware, I then need to create the object:

User joebloggs = new User(username, name, password, age);

and then add this to the list:

users.add("user1", joebloggs);

Is this the correct way of doing things? I am also having difficulty when using lists in a GUI (with a form). Where abouts should I be declaring the list, in the static void Main(), or in the Form.cs itself?

Apologies if my terminology is a little off, please don't hesitate to correct me! Thank you in advance for any help you can provide :)

Recommended Answers

All 4 Replies

Your code is good. I don't see any problems with it.

For what do you want to use lists in GUI? Be more specific, please.

Thanks

Your code is good. I don't see any problems with it.

For what do you want to use lists in GUI? Be more specific, please.

Thanks

Thanks for your quick reply farooqaaa. In my GUI I have a "search for username" option. I have a field to enter the username (search_data) and then a submit button. On this submit button I've added the following code:

if (users.ContainsKey(search_data.Text.ToString()))
{
  found = true;
}

However, I get an error saying "The name 'users' does not exist in the current context. I understand this is due to my declaration of the list, which I still don't understand where to place. The error still remains if I place it in the static void Main(), and if I place it just above the coding above I'll be declaring it everytime the button is clicked, which surely I should only be doing once? Also, it'd mean it wouldn't be accessible by other methods which I'll shortly be adding.

I'm very confused!

Declare it above public Form1().... :

public partial class Form1 : Form
    {

       [B]Dictionary<string, User> users= new Dictionary<string, User>();
[/B]
        public Form1()
        {
            InitializeComponent();
        }
.....

Now you can access it everywhere (inside this class).

Thanks

Works perfectly, I can't believe I couldn't work that out. Thank you so much for all your help Farooq!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.