Hi,
Req: I want to pass the value from one class to another class file.
Example: ClassA.cs having one property like 'ProductId'. i require this value in my second class -> ClassB.cs. Using this value i am doing some calculation in my ClassB (May be ClassB update this property value too). Then again i am returning to my ClassA. My object life-cycle is like ClassA --> ClassB --> ClassA
Please share some ideas to retain this prperty value.
Loading
Madhu KPosted Dec 13, 2010, 5:30 AM
private string _ProductId;
public string ProductIdNEW
{
get
{ return _ProductId; }
set
{ _ProductId = value; }
}
class ClassB
{
public void CheckConstraint(string Userid)
{
ClassA objClassA = new ClassA();
if (objClassA.ProductId.Equals(string.Empty))
{
objClassA.ProductId = "APB12341";
ProductIdNEW = Class1.ProductId;
}
}
}
class ClassA
{
private string _ProductId;
public string ProductId
{
get
{ return _ProductId; }
set
{ _ProductId = value; }
}
public void GetDetails(string Userid)
{
if(Userid.Equals(string.Empty))
{ ProductId="APB12345"; }
ClassB objClassB = new ClassB();
objClassB.CheckConstraint(Userid);
// You get the productid value in id below
string id = objClassB.ProductIdNEW;
}
}
CrishPosted Dec 14, 2010, 5:33 AM
I have same problem .thanks for code now it will help me to solve my issue of o pass a value from one Class file to another Class.
SharrangasPosted Dec 13, 2010, 6:53 AM
Madhu KPosted Dec 13, 2010, 6:10 AM
SharrangasPosted Dec 13, 2010, 6:04 AM
Is there any option to avoid same static property object shared between the request... ?
SharrangasPosted Dec 13, 2010, 5:18 AM
If i declaring this ProductId as static variable the object exist. So i can able to update my productId as per my wish.
so same like this i want to retain the object without declaring property as static.(the reason for avoiding static variable - if i am declaring property as static, the same object shared with all request. So i want to avoid that....)
Madhu KPosted Dec 13, 2010, 4:24 AM
if (ClassA.ProductId.Equals(string.Empty)) to
if (Class1.ProductId==null || Class1.ProductId=="")
or
if(string.IsNullOrEmpty(Class1.ProductId)) to check for null.
SharrangasPosted Dec 13, 2010, 3:49 AM
My aspx:
string Userid = string.Empty;
ClassA objClassA = new ClassA();
objClassA.GetDetails(Userid);
---------------
class ClassA
{
private string _ProductId;
public string ProductId
{
get
{ return _ProductId; }
set
{ _ProductId = value; }
}
public void GetDetails(string Userid)
{
if (Userid.Equals(string.Empty))
{
_ProductId = "APB12345";
}
ClassB objClassB = new ClassB();
objClassB.CheckConstraint(Userid);
}
}
--------------------------
class ClassB
{
public void CheckConstraint(string Userid)
{
ClassA objClassA = new ClassA();
if (objClassA.ProductId.Equals(string.Empty))
{
objClassA.ProductId = "APB12341";
}
}
}------------
when my breakpoint reaches the ClassA.ProductId its returning with Null refer exeception. If i declare the ProductId as static it will work but i dont want that....
Please suggest me how to get the object without using static declartion?
Madhu KPosted Dec 13, 2010, 2:25 AM
check the sample
class Class1
{
private int ProductId = 20;
public int intPublicProperty
{
get
{return ProductId;}
set
{ ProductId = value;}
}
}
class class2
{
public void Checkvalue()
{
Class1 objClass1 = new Class1();
MessageBox.Show("Showing in Class Two : " + objClass1.intPublicProperty);
}
}