I have two forums,Login and Game. Login is used for logging in and it calls a dictionary from class "Clsdictionary" So I use this on Form Login.
Code
clsDictionary UserSetCLS = new clsDictionary();
Then I add some Values and Keys to that Dictionary in the class,I'm using a class so this dictionary can be global/Shared between all my forms in that application.
Now I have 3 entries. 1 for UserSetINT 2 for UserSetSTRING
At the end of all my code it switchs to form game. I try to call back the entries that SHOULD be in the Dictionary
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespaceGame
{
publicpartialclassfrmCapitalCity : Form
{
clsDictionary UserSetCLS = new clsDictionary();
string CapitalName;
publicfrmCapitalCity()
{
InitializeComponent();
}
privatevoidCapitalCity_Load(object sender, EventArgs e)
{
UserSetCLS.UserSetSTRING.TryGetValue("CapitalName", out CapitalName);
}
}
}
So that should take the dictionary entry "CapitalCity" and change the label I have to the string value of that. But,When i call it back its Null. I'm wondering how could that be? I put it all in another Class so it can be used in every form in my application.
Basicly All i want to do is Get one Value from one forum to another. On Login the Dictionary count is 3 with those three values and there keys. When i call it back on Game the count is zero with no Entries!
Can anyone correct me in what i did? Or Point me to a tutorial that goes over this Specific detail of what i want to do?
If you need me to clear anything up please post! Thanks, Aaron
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
PJ MartinsRank #2,548 · 548 reputationPosted Jun 1, 2010, 11:27 AM EDT
✓ Accepted answer0
The problem is when you create a new instance of clsDictionary in your frmCapitalCity, it will not contain the values you set in your login form, it will be a whole new seperate UserSetCLS. In order to access the one you created in your login form, you'll have to create a static instance somewhere and use it instead. Add the following code to your ClsDictionary class:
public static ClsDictionary Instance = new ClsDictionary();
now you can access the instance throughout your project, so in your login form you'd use: ClsDictionary.Instance.UserSetINT.Add("UserID", _UserID); ClsDictionary.Instance.UserSetSTRING.Add("UserName", UserName); ClsDictionary.Instance.UserSetSTRING.Add("CapitalName", CapitalName);
and in frmCapital you'd do this instead: ClsDictionary.Instance.UserSetSTRING.TryGetValue("CapitalName", out CapitalName);
Thanks! That got the job done. I'm kind of confused on how to view the count of the dictionary now from the "Locals" Listing. But,Then again when it reaches the other side i can see if its correct or not. Everything works now and i took out
clsDictionary UserSetCLS = new clsDictionary(); on both the login and the Capitalcity due to after your code its useless to instance it at the top!
No problem, you can follow the count of the dictionary through the watch window by using the same instance. Eg
ClsDictionary.Instance.UserSetINT.Count;
PJ MartinsRank #2,548 · 548 reputationPosted Jun 1, 2010, 11:27 AM EDT
public static ClsDictionary Instance = new ClsDictionary();
now you can access the instance throughout your project, so in your login form you'd use:
ClsDictionary.Instance.UserSetINT.Add("UserID", _UserID);
ClsDictionary.Instance.UserSetSTRING.Add("UserName", UserName);
ClsDictionary.Instance.UserSetSTRING.Add("CapitalName", CapitalName);
and in frmCapital you'd do this instead:
ClsDictionary.Instance.UserSetSTRING.TryGetValue("CapitalName", out CapitalName);
Aaron21 reputationPosted Jun 1, 2010, 1:10 PM EDT
That got the job done.
I'm kind of confused on how to view the count of the dictionary now from the "Locals" Listing.
But,Then again when it reaches the other side i can see if its correct or not.
Everything works now and i took out
clsDictionary UserSetCLS = new clsDictionary();
on both the login and the Capitalcity due to after your code its useless to instance it at the top!
Thanks again!
PJ MartinsRank #2,548 · 548 reputationPosted Jun 1, 2010, 1:46 PM EDT
ClsDictionary.Instance.UserSetINT.Count;