Link to home
Start Free TrialLog in
Avatar of niceguy971
niceguy971

asked on

What are the minimal requirements for an object which can be placed into this type of HttpSessionState?

Instances of the class (written in C#)  must be placed into the session of an ASP.NET application deployed to a server cluster with distributed sessions.  What are the minimal requirements for an object which can be placed into this type of HttpSessionState?
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland 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 niceguy971
niceguy971

ASKER

Thank you very much!!!
I have completed the following exercise & would like to know your opinion. Is there a better solution???

Exercise:
Instances of the following class must be placed into the session of an ASP.NET application deployed to a server cluster with distributed sessions and used as a key for a Hashtable of other
objects. Make the necessary modifications to the UserKey so that it meets the minimal requirements for an object which can be placed into this type of HttpSessionState and ensure the test code following the class definition below produces the indicated results. Modify the class to
use properties and provide full documentation.

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 name;
}
public String GetUserID()
{
return userid;
}
}
}
Test Code (ensure the results are as indicated in the comments).
NOTE: Do NOT modify this test code. Ensure your code can return the indicated results without
any alteration to this test suite.
UserKey b1 = new UserKey(Bill Smith, BSMITH);
UserKey b2 = new UserKey(Bill Smith, BSMITH);
UserKey b3 = new UserKey(Susan Smith, SSMITH);

UserKey b4 = new UserKey(null,null);
System.Console.WriteLine( b1.Equals(b1) ); // prints true
System.Console.WriteLine( b1.Equals(b2) ); // prints true
System.Console.WriteLine( b1.Equals(b3) ); // prints false
System.Console.WriteLine( b1.Equals(null) ); // prints false
System.Console.WriteLine( b1.Equals(Some String) ); // prints false
System.Console.WriteLine( b4.Equals(b1) ); // prints false
Hashtable ht = new Hashtable();
ht.Add(b1,Some Data);
String s = (String) ht[b1];
System.Console.WriteLine( s.Equals(Some Data) ); // prints true
//My solution:
using System;
using System.Collections;
using System.Runtime.Serialization;


namespace com.landsafe.eval
{


[Serializable()]
public class UserKey
{
            public String name; // private readonly String name;
            public String userid; // private readonly String userid;

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

            
                /*
                public String GetName()
            {
                  return name;
            }
            public String GetUserID()
            {
                  return userid;
            }  */


                  public bool Equals(UserKey p)  // I added
            {
                  // If parameter is null return false:
                  if ((object)p == null)
                  {
                        return false;
                  }

                  // if p is not a UserKey reference ...return false

                        if (! ReferenceEquals( this.GetType(),p.GetType() ))
                        {
                            return false;
                        }


                  // Return true if the fields match:
                  return (this.name == p.name) && (this.userid == p.userid);
            }


}

class Test3
{

      public static void Main()
        {

            UserKey b1 = new UserKey("Bill Smith", "BSMITH");
            UserKey b2 = new UserKey("Bill Smith", "BSMITH");
            UserKey b3 = new UserKey("Susan Smith", "SSMITH");

            UserKey b4 = new UserKey(null,null);
           
           System.Console.WriteLine("They want --true");                   // I added
                  System.Console.WriteLine( b1.Equals(b1) ); // prints true
           System.Console.WriteLine(""); // I added

           System.Console.WriteLine("They want --true");                   // I added
                  System.Console.WriteLine( b1.Equals(b2) ); // prints true
           System.Console.WriteLine(""); // I added
                  
               System.Console.WriteLine("They want --false"); // I added
                  System.Console.WriteLine( b1.Equals(b3) ); // prints false
           System.Console.WriteLine(""); // I added

           System.Console.WriteLine("They want --false"); // I added
            System.Console.WriteLine( b1.Equals(null) ); // prints false
           System.Console.WriteLine(""); // I added


               System.Console.WriteLine("They want --false"); // I added
                  System.Console.WriteLine( b1.Equals("Some String") ); // prints false
           System.Console.WriteLine(""); // I added

           System.Console.WriteLine("They want --false"); // I added
            System.Console.WriteLine( b4.Equals(b1) ); // prints false
           System.Console.WriteLine(""); // I added
           
                  Hashtable ht = new Hashtable();
            ht.Add(b1,"Some Data");
            String s = (String) ht[b1];
           
                  
               System.Console.WriteLine("They want --true"); // I added
                  System.Console.WriteLine( s.Equals("Some Data") ); // prints true
           System.Console.WriteLine(""); // I added


      }

}

} //end namespace


Here you go mate. This code sample uses properties and produces the desired result. I have overidden the other necessaries from the object class and added a default constructor which is necessary for XML serialization (this may not be need however).
using System;
using System.Collections;
using System.Runtime.Serialization;
 
namespace com.landsafe.eval
{
	[Serializable()]
	public class UserKey
	{
		private String _name;	// private readonly String name; 
		private String _userId;	// private readonly String userid;
 
		public String Name
		{
			get { return _name; }
			set { _name = value; }
		}
 
		public String UserId
		{
			get { return _userId; }
			set { _userId = value; }
		}
 
		/// <summary>
		/// Default constructor necessary for 
		/// XML serialization
		/// </summary>
		public UserKey()
		{
			_name = String.Empty;
			_userId = String.Empty;
		}
 
		public UserKey(String name, String userId)
		{
			this._name = name;
			this._userId = userId;
		}
 
		public override bool Equals(object obj)
		{
			if ((object)obj == null)
				return false;
 
			if(!(obj is UserKey))
				return false;
 
			return (this._name == (obj as UserKey)._name) && (_userId == (obj as UserKey)._userId);
		}
 
		public override string ToString()
		{
			return String.Format("{0}: {1}", _userId, _name);
		}
 
		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}
	}
}

Open in new window

If I move from Net Framework v1.1 to v2.0 ...do I need to make changes in the code above?
Sorry for taking so long to respond. I didn't realise you posted a comment. Yes this code definitely works in 2.0, 3.0, 3.5 and I am reasonably certain in works in 1.1.
Thank You very much!!!  I would like to know your opinion--please see my question below:

Implement the Employee and Manager classes as showen on the diagram (the diagram.doc file). The classes should be in the com.landsafe.eval package, be public, and implement constructors that take the values to initialize all their attributes.  The classes should be serializable.
************
I wrote the code. Is there a better solution??

using System;
using System.Collections;
using System.Runtime.Serialization;


namespace com.landsafe.eval
{


[Serializable()]
public class Employee
{

    private String _name;
    private DateTime _dateHired;
    private int _id;

//===========+DateHired() : DateTime====
    public DateTime getDateHired()
    {
        return _dateHired;
    }

    public void SetDateHired(DateTime dateHired)
    {
      this._dateHired = dateHired;
    }

//========+ID() : int=======
      public int GetId()
      {
         return _id;
      }


      public void SetId(int id)
      {
         this._id=id;
      }

//========+Name() : String======
      public String GetName()
      {
         return _name;
      }

      public void SetName(String name)
      {
         this._name=name;
      }



    public Employee(String name, DateTime dateHired, int id) // should add default constructor?
    {
       this._name=name;
       this._dateHired=dateHired;
       this._id=id;      

    }


    public override bool Equals(object obj) // should I do it?-not shown on the diagram
    {
      if ((object)obj == null)
      return false;
 
      if(!(obj is Employee))
      return false;
 
      return (this._name == (obj as Employee)._name) && (this._id == (obj as Employee)._id) && (this._dateHired == (obj as Employee)._dateHired);
    }


    public override string ToString() // should I do it?-not shown on the diagram
    {
        return (this._name + " " +this._dateHired + " "+this._id );

    }


   public override int GetHashCode()
   {
      return this.ToString().GetHashCode();
   }
   

}  //End of class Employee



[Serializable()]
public class Manager : Employee
{
    private decimal _bonus;

      public decimal GetBonus()
      {
         return _bonus;
      }


      public void SetBonus(decimal bonus)
      {
         this._bonus=bonus;
      }



    public Manager(String name, DateTime dateHired, int id,decimal bonus):base(name, dateHired, id)
    {
       this._bonus=bonus;  

    }


} //End of class Manager


class Test5
{

           public static void Main()
        {


             Console.WriteLine("Inside of Main mathod");

             DateTime hiredDate,d2;
             hiredDate = new DateTime(2008, 1, 2, 3, 4, 5);    // Date = 2 Jan 2008 03:04:05
             d2= new DateTime(2008, 3, 2, 3, 4, 5);

             Employee bob= new Employee("Bob",hiredDate, 12345);
             Console.WriteLine("bob's _name: "+ bob.GetName());
             Console.WriteLine("bob's _id: "+ bob.GetId());

   
             decimal bon = 300.5m;

             Manager john=new Manager("John", d2, 98765, bon);
             Console.WriteLine("john's _name: "+ john.GetName());
            Console.WriteLine("john's _id: "+ john.GetId());

            Console.WriteLine("john's _bonus: "+ john.GetBonus());
             
           
        }



}




} // End Namespace
diagram.doc
Test5.txt
Here you go mate. To note that this is a lot of work for 125 points. Is this your assignment?
using System;
using System.Collections;
using System.Runtime.Serialization;
 
namespace com.landsafe.eval
{
	[Serializable]
	public class Employee
	{
		#region Fields & Properties
 
		private String _name;
		private DateTime _dateHired;
		private int _id;
 
		/// <summary>
		/// Gets or sets the date employee was hired
		/// </summary>
		public DateTime DateHired
		{
			get { return _dateHired; }
			set { _dateHired = value; }
		}
 
		/// <summary>
		/// Gets or sets the employee id
		/// </summary>
		public int Id
		{
			get { return _id; }
			set { _id = value; }
		}
 
		/// <summary>
		/// Gets or sets the name of the employee
		/// </summary>
		public String Name
		{
			get { return _name; }
			set { _name = value; }
		}
 
		#endregion
 
		#region Constructors
 
		/// <summary>
		/// Use a default constructor in this case
		/// only if XML Serialization is required - 
		/// remove otherwise asn question does not
		/// state that you need one.
		/// </summary>
		public Employee()
			: this("Unknown", DateTime.Now, -1)
		{
		}
 
		/// <summary>
		/// Construct employee object.
		/// </summary>
		public Employee(String name, DateTime dateHired, int id)
		{
			_name = name;
			_dateHired = dateHired;
			_id = id;
		}
 
		#endregion
 
		/// <summary>
		/// This is good practice. Its not shown on the diagram 
		/// however its a sensible addition to the class.
		/// </summary>
		public override bool Equals(object obj)
		{
			if ((object)obj == null)
				return false;
 
			if (!(obj is Employee))
				return false;
 
			return (this._name == (obj as Employee)._name) && 
				(this._id == (obj as Employee)._id) && 
				(this._dateHired == (obj as Employee)._dateHired);
		}
 
		/// <summary>
		/// This is good practice. Its not shown on the diagram 
		/// however its a sensible addition to the class.
		/// </summary>
		public override string ToString()
		{
			// Use String.Format / String.Concat to combat the
			// fact that .NET string are immutable.
			return String.Format("{0} {1} {2}", this._name, this._dateHired, this._id);
		}
 
		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}
	}
 
 
	[Serializable]
	public class Manager : Employee
	{
		#region Fields & Properties
 
		private decimal _bonus;
 
		public decimal Bonus
		{
			get { return _bonus; }
			set { _bonus = value; }
		}
 
		#endregion
 
		#region Constructors
 
		public Manager(String name, DateTime dateHired, int id, decimal bonus)
			: base(name, dateHired, id)
		{
			_bonus = bonus;
		}
 
		#endregion
	}
 
 
	class Test5
	{
		public static void Main()
		{
			Console.WriteLine("Inside of Main mathod");
 
			// Date = 2 Jan 2008 03:04:05
			Employee bob = new Employee("Bob", new DateTime(2008, 1, 2, 3, 4, 5), 12345);
			Console.WriteLine("bob's name: {0}", bob.Name);
			Console.WriteLine("bob's id: {0}\n",  bob.Id);
 
			Manager john = new Manager("John", new DateTime(2008, 3, 2, 3, 4, 5), 98765, 300.50M);
			Console.WriteLine("john's name: {0}", john.Name);
			Console.WriteLine("john's id: {0}", john.Id);
			Console.WriteLine("john's bonus: £{0}", john.Bonus);
 
			// Pause
			Console.ReadLine();
		}
	}
}

Open in new window

Thank You very much!!!  How can I add points???  It's not my assignment. I'm trying to learn C# deeper & was going thru some exersices.  I recently found this website.
I don't know how that is done, without opening a question.
OK...I'm ready to ask another question (and assign the max amount of points)...Could you please take a look? Thanks
Ask away
I submitted a new Question (Use try..catch...)- 500 points. Please take a look at open questions. Is it a good programming practice? What's your opinion? Thanks

Just in case- the same code is below:
using System;
using System.IO;

public class ExceptionTest
{

  public static void Main()  
  {
     int i=-5;

     try
     {
        if (i==-5)
          throw new Exception("My exception");

     }
     catch (Exception e)
     {
           Console.WriteLine("MESSAGE: " + e.Message);

     }
     finally
     {
       Console.WriteLine("Inside Finally.");
     }

     Console.WriteLine("End of application");
 
  }
}

I have left an answer, however the code examples provided were sufficient. Please feel free to ask any question easy or complicated.