• 0

[C#/ASP] - Session Object Help


Question

My session object appears to be working although I am having trouble with the logic in my loop. All I want to do is test the object returned by key i, and determine if its type is that of another class type, i.e., Client class or Location class.

If it is, toss them into different array I have setup.

Is what I wrote correct in testing the Type of the object stored at index i in Session?

Any help appreciate.

 protected void Page_Load(object sender, EventArgs e)
	{
		if(!IsPostBack)
		{
			if(sessionTest())
			{
				for (int i = 0; i < Session.Count; i++)
				{
					if (Session[i].GetType().Equals(typeof(Client)))
					{
						_clients.Add((Client)Session[i]);
					}
					if (Session[i].GetType().Equals(typeof(Location)))
					{
						_locations.Add((Location)Session[i]);
					}
					Session.RemoveAt(i);
				}

				lbl_status_message.Text = "Session Count: " + Session.Count.ToString() +
																		" - " + "Clients: " + _clients.Count + " - " + 
																		"Locations: " + _locations.Count;
			}
		}
	}
	public bool sessionTest()
	{
		return Session.Count > 0 ? true : false;
	}

lbl_status_message will currently display, Session Count: 2 Clients: 0 Locations: 0. So it's clearly finding my variables in session but not passing the logic in the loop.

Edited by AaronMT
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Testing with this revision.

 if ((Session[i]).GetType().FullName.Equals(typeof(WeddingPlanner.Client).FullName))
					{
						_clients.Add((WeddingPlanner.Client)Session[i]);
					}
					if ((Session[i]).GetType().FullName.Equals(typeof(WeddingPlanner.Location).FullName))
					{
						_locations.Add((WeddingPlanner.Location)Session[i]);
					}

Link to comment
Share on other sites

  • 0

Do they need to be that exact type, or could you use the is statement, which will evaluate to true if the type is the exact type, or castable:

object item = Session[i];
if (item is WeddingPlanner.Client)
{
	_clients.Add(item as WeddingPlanner.Client);
} 
else if (item is WeddingPlanner.Location)
{
	_locations.Add(item as WeddingPlanner.Location);
}

Link to comment
Share on other sites

  • 0
Do they need to be that exact type, or could you use the is statement, which will evaluate to true if the type is the exact type, or castable:

object item = Session[i];
if (item is WeddingPlanner.Client)
{
	_clients.Add(item as WeddingPlanner.Client);
} 
else if (item is WeddingPlanner.Location)
{
	_locations.Add(item as WeddingPlanner.Location);
}

Late reply, but that works beautifully. I forgot about C#'s is/as :)

Link to comment
Share on other sites

  • 0

It really isn't the best looking solution in the world. What are you trying to accomplish? If we understood your problem better, we may be able to give you a more elegant solution.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.