What would you do in console application to save a variable or object in this process to be accessible in every class like session variable in asp.net?
Something like below and I want the BAArray to be accessable in
every class libry without passing the arry.
How can I do that in console app? In asp.net I will just put
that in the session variable.
ArrayList BAArray = new ArrayList();
for
(int i = 0; i < 5; i++)
{
BAArray.Add(new BAObj());

Jaish MathewsRank #225 · 11K reputationPosted Apr 29, 2010, 8:36 AM EDT
Simple only. Use properties when ever need to expose values to out side.
Your Console Class With ArrayList
public class MyConsoleClass
{
//Property which will give ArrayList to any outside class
public ArrayList MyArrayList
{
get
{
ArrayList BAArray = new ArrayList();
for (int i = 0; i < 5; i++)
{
BAArray.Add(new object());
}
return BAArray;
}
}
}
Another Class Inside Console Application
class Program
{
static void Main(string[] args)
{
MyConsoleClass cls = new MyConsoleClass();
//Calling the property
ArrayList alist = cls.MyArrayList;
}
}
Amit ChoudharyRank #145 · 15.8K reputationPosted Apr 29, 2010, 8:36 AM EDT
if your class is in asp.net project or web application then you can use. System.Web.HttpContext.Session to save and retrieve the session.
Please mark "Do you like this post" if it helps you.
rich12 reputationPosted Apr 29, 2010, 9:18 AM EDT
Thanks for the example, however what I want was to have the alist avilabe to third class without passing the alist.
class Program
{
static void Main(string[] args)
{
MyConsoleClass cls = new MyConsoleClass();
ArrayList alist = cls.MyArrayList;
//Anotehr class need to access ArrayList
ThirdClass Tcls = new ThirdClass();
Tcls.runjob(ref alist); //here I need to pass the alist
}
}
//here is what need to be done
class ThirdClass
{
public void runjob(ArrayList alist)
{
//access alist
}
}
//Here is waht I want, without passing alist
//if it's in asp.net I can do HttpContext.Session like Amit mentioned but this is a console app.
class ThirdClass
{
public void runjob()
{
//somehow access the alist without passing in like below
if (alist !=null)
{
//do something
}
}
}
Jaish MathewsRank #225 · 11K reputationPosted Apr 29, 2010, 9:38 AM EDT
That I said create the belwo proerty in your 1st class. It can be accessible in other classes
//Property which will give ArrayList to any outside class
public ArrayList MyArrayList
{
get
{
ArrayList BAArray = new ArrayList();
for (int i = 0; i < 5; i++)
{
BAArray.Add(new object());
}
return BAArray;
}
}
rich12 reputationPosted Apr 29, 2010, 10:24 AM EDT
I did create the property in the 1st class and it's ok to use it in the main class. But if I don't pass it to the third class, I gett he error "The name 'cls' does not exist in the current context " message in below third class.
class ThirdClass
{
public void runjob()
{
ArrayList alist = cls.MyArrayList;
}
}
Jaish MathewsRank #225 · 11K reputationPosted Apr 29, 2010, 9:12 PM EDT
You declared the property in 1st class.
So you need to create objec of 1st class when ever you need to call this property. You missed this object creation. I think you declared the varibale "cls" in Main class only. Please declare the object and then only call the property. Send your full code, if still doubt there.