Link to home
Start Free TrialLog in
Avatar of Matt Grofsky
Matt GrofskyFlag for United States of America

asked on

Hashtable Storing Users, but I have more than 2 things I need to store.

I'm currently doing something along the lines of

    public Hashtable MyAppUsers
    {
        get
        {
            Hashtable a = (Hashtable)Application["CurrentUsers"];
            if (a == null)
            {
                a = new Hashtable(2);
                Application.Lock();
                Application["CurrentUsers"] = a;
                Application.UnLock();
            }
            return a;
        }
    }

To store users in a C# website.   Basically just doing a MyAppUsers.Add(key,data) and so on.

The key is the user login and the data is a date for the last time the hashtable entry was updated.  I'm trying to figure out how I could also add an IP address in there as sometimes I have multiple people using the same login but not always fromt he same IP.

The only thing I can thing of off the top of my head is to make the key,value pair something like  MyAppUsers(Login|IPAddress,Data)  where Login|IPAddress is the key and the date is the value.

I would ideally want to have something like

JohnK,1.2.3.4,4/10/2006
JohnK,2.4.2.3,5/10/2005
Marryj,6.5.4.3,3/1/2006
and so on... with 3 values across but have no idea how to do it except for putting some delimiter in the Key and then parsing the key to get two values.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Matt Grofsky

ASKER

Fernando,

Looks like a good option to me, better than me putting in |pipes all over the place

Thanks.
Glad I was able to help. :=)
Fernando,

I forgot to ask, how would I grab the struct out of the hashtable value and parse it?
Hi Michin;

The following code will get the current user info from the Hashtable and cast that object to a UserInfo object and then displays the information in that structure.

      UserInfo uInfo = (UserInfo) App["CurrentUser"];
      MessageBox.Show("IPAddress = " + uInfo.IPAddress + " Date = " +
            uInfo.date.ToString());


Fernando
Fernando,

Hmm ya I tried that also but I get a  Inalid Cast Exception...Specified cast is not valid when I try to cast the (UserInfo)App["CurrentUser"]
What did you call this structure?

     struct UserInfo
     {
          public string IPAddress;
          public DateTime date;
     }

Because the code I sent you works for me.

Fernando