Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys i Have a web based application that contains a class called myList that create my collection list and handles adding of data to the collection list. Is it possible to declare a global instance of myList class tht will be created when the web application starts and will be accessible by any class that need to access the list in The myList Class. Thanks
Posted

1 solution

Assuming your class can be serialzed you can add it to the Application which will make it available to all sessions.

C#
System.Web.HttpContext.Current.Application["SomeClass"] = value; 
 
Share this answer
 
Comments
IamBlessed 4-Apr-12 10:43am    
Thanks.. My Class is serialized, does the code you provided above go into the Application_Start event or session_start, also what does the value represent and also how do i call the class and access the method in it.. thanks
ZurdoDev 4-Apr-12 11:00am    
1. You can put the code anywhere you want but since it is for the Application, Application_Start would be the likely place. It just depends on what you are doing.
2. value in my example would be an instance of your class.
3. If you want to call methods in it, it really sounds like you want a static class. Are you wanting to store values or call methods? To store values do the way I suggested but if all you want to do is call methods then use a static class.
IamBlessed 4-Apr-12 11:22am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JustMe
{
[Serializable]
public class FITS
{

public List<fitness> fitness { get; set; }

public FITS()
{
fitness = new List<fitness>();
}

public void AddFitness(Fitness addF)
{

for (int i = 0; i < fitness.Count; i++)
{

if (fitness[i].Id.Equals(addF.Id) == true)
{
ExceptionHandler error = new ExceptionHandler(addF.Id);
throw (error);
}

}

fitness.Add(addF);

}

public void DeleteFitness(Fitness deleteF)
{
fitness.Remove(deleteF);
}


public void SelectSort()
{

int smallest;

for (int i = 0; i < fitness.Count - 1; i++)
{
smallest = i;

for (int index = i + 1; index < fitness.Count; index++)
{

if (fitness[index].Id.CompareTo(fitness[smallest].Id) < 0)
{
smallest = index;
}
}

var temp = fitness[i];
fitness[i] = fitness[smallest];
fitness[smallest] = temp;

}

}

public void SelectSort1()
{

int smallest1;

for (int i = 0; i < fitness.Count - 1; i++)
{
smallest1 = i;

for (int index1 = i + 1; index1 < fitness.Count; index1++)
{

if (fitness[smallest1].Id.CompareTo(fitness[index1].Id) < 0)
{
smallest1 = index1;
}
}

var temp1 = fitness[i];
fitness[i] = fitness[smallest1];
fitness[smallest1] = temp1;

}

}



}
}

Above is the smaple of myList Class, the AddFitness method get the values submitted by the user and add them to the list, the problem i am having is that every time a new data is submitted by the user a new list is created, that why i want a single global instance of the class that will be instantiated when the web application starts, and i want to also be able to access the AddFitness method so user can be able to use that one instance of the class that is created when the application starts.
ZurdoDev 4-Apr-12 11:44am    
It might be easier to just store the list in the application so you can always use that list and then make your class static.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900