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 KRank #1,514 · 1.1K reputationPosted Dec 13, 2010, 12:30 AM EST
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;
}
}
Madhu KRank #1,514 · 1.1K reputationPosted Dec 12, 2010, 9:25 PM EST
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);
}
}
Sharrangas123 reputationPosted Dec 12, 2010, 10:49 PM EST
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 KRank #1,514 · 1.1K reputationPosted Dec 12, 2010, 11:24 PM EST
if (ClassA.ProductId.Equals(string.Empty)) to
if (Class1.ProductId==null || Class1.ProductId=="")
or
if(string.IsNullOrEmpty(Class1.ProductId)) to check for null.
Sharrangas123 reputationPosted Dec 13, 2010, 12:18 AM EST
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....)
Sharrangas123 reputationPosted Dec 13, 2010, 1:04 AM EST
Is there any option to avoid same static property object shared between the request... ?
Madhu KRank #1,514 · 1.1K reputationPosted Dec 13, 2010, 1:10 AM EST
Sharrangas123 reputationPosted Dec 13, 2010, 1:53 AM EST
CrishRank #1,486 · 1.1K reputationPosted Dec 14, 2010, 12:33 AM EST
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.