[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET Session Class instance problem in Server Cluster with distributed sessions

Posted on 2006-05-05
12
Medium Priority
?
314 Views
Last Modified: 2009-12-16
I need to add properties, but it is not working very well.  What is wrong with this class?

namespace com.landsafe.eval
{
   public class UserKey
   {
      private readonly String name;
      private readonly String userid;

      public UserKey(String name, String userid)
      {
         this.name = name;
         this.userid = userid;
      }

      public String GetName()
      {
         return userid;
      }
   }
}

Also, it has to be used as a key for a Hashtable.  What changes are required for to be a Hashtable key?
0
Comment
Question by:bfth1505
  • 7
  • 2
  • 2
  • +1
12 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16614655
You need to overload Equals() the == operator, and GetHashCode() to see if twpo UserKey objects are equal if you want to use it as a HashTable key.  objects by default use ReferenceEquals for the == comparison.

http://msdn2.microsoft.com/en-US/library/336aedhh.aspx
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16614661
sorry, that should be override, not overload.
0
 
LVL 3

Expert Comment

by:seazium
ID: 16614852
Hi,
read this, probably it will solve the problem

http://www.codeproject.com/useritems/Property_Acccesors.asp

regards,
dZ
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16615258
Well, one thing that is wrong with it is that you are trying to change the values of "name" and "userid" in your UserKey method, however, they are both declared as "ReadOnly".
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16615273
UsreKey() is the constructor.  You can set readonly field in the declaration or in a constructor (but nowhere else)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16615438
Doh, wasn't reading it properly ;)
0
 

Author Comment

by:bfth1505
ID: 16615903
Thanks for the input.  So what would it look like with all these changes?
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16615956
What defines "equality" for your class?  Just the user ID?  A combination of the User ID and Name?  

Assuming it's just the user ID, add the following two methods:

            public override bool Equals(object obj)
            {
                return userid.Equals(obj);
            }
            public override int GetHashCode()
            {
                return userid.GetHashCode();
            }

Also, I was wrong; you don't need to override the equality operator; just Equals() and GetHashCode() to use it in a hashtable.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16615992
Also, it seems a bit odd that GetName() returns the userid (instead of the name), and the name field is never used.

In additon, it's generally clearer to use get/set accessors instead of get___() :

      public String Name
      {
         get
            {
                return userid;
            }
      }
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16616024
Sorry, that should have been

            public override bool Equals(object obj)
            {
                UserKey userKey = obj as UserKey;
                    return userKey == null ? false : userid.Equals(userKey.userid);
            }
0
 

Author Comment

by:bfth1505
ID: 16641506
Put it all together and I'll award the points!
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 2000 total points
ID: 16641780
Okay, ASSUMING that:

You want to return the name value for the Name property, and the userid value from the Userid property,

AND ASSUMING

equality of two objects is defined by equal user IDs (not a combination of userid and name),

Here goes:


namespace com.landsafe.eval
{
   public class UserKey
   {
      private readonly String name;
      private readonly String userid;

      public UserKey(String name, String userid)
      {
         this.name = name;
         this.userid = userid;
      }

      public String Name
      {
            get
            {
               return name;
            }
      }
      public String Userid
      {
            get
            {
               return userid;
            }
      }

      // Two UserKeys are equal if and only if they have the same user ID
      public override bool Equals(object obj)
      {
          UserKey userKey = obj as UserKey;
          return userKey == null ? false : userid.Equals(userKey.userid);
      }
      // See equality condition above
      public override int GetHashCode()
      {
          return userid.GetHashCode();
      }
   }
}
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question