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 MathewsPosted Apr 30, 2010, 1:12 AM
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.
richPosted Apr 29, 2010, 2:24 PM
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 MathewsPosted Apr 29, 2010, 1:38 PM
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;
}
}
richPosted Apr 29, 2010, 1:18 PM
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
}
}
}
Amit ChoudharyPosted Apr 29, 2010, 12:36 PM
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.
Jaish MathewsPosted Apr 29, 2010, 12:36 PM
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;
}
}