• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

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?
0
Matt Grofsky
Asked:
Matt Grofsky
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
Hi Michin;

You could use a structure to hold the IP address and the date. See sample code.

      struct UserInfo
      {
            public string IPAddress;
            public DateTime date;
      }
      
      Hashtable App = new Hashtable();
            
      private void button1_Click(object sender, System.EventArgs e)
      {
            UserInfo uInfo = new UserInfo();
            uInfo.IPAddress = "12.56.44.102";
            uInfo.date = DateTime.Now;
            App.Add("CurrentUser", uInfo);
      }


Fernando
0
 
Matt GrofskyAuthor Commented:
Fernando,

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

Thanks.
0
 
Fernando SotoCommented:
Glad I was able to help. :=)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Matt GrofskyAuthor Commented:
Fernando,

I forgot to ask, how would I grab the struct out of the hashtable value and parse it?
0
 
Fernando SotoCommented:
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
0
 
Matt GrofskyAuthor Commented:
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"]
0
 
Fernando SotoCommented:
What did you call this structure?

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

Because the code I sent you works for me.

Fernando
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now