Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hii ,

I am from my main page passing object using parameterized constructor to set .

Now in other class .. I got the vale for the first time .. howerver within the same class when any object of that gets created default constructor gets invoked and lost the value set using parameterized constrcuror ..

Please suggest

public abc()
{

}

public abc(classexp obj)
{
connection=obj.connection;
year=obj.year;
}

Method1()
{
// here i can use the value
}

method 2()
{
Abc objabc=new abc();
abc.getdetails();
here i am loosing the value.. set by my parameterized constructor
}
Posted
Updated 3-Nov-14 19:14pm
v2

1 solution

in Method2 why are you creating a new object i.e.
C#
method 2()
 {
 Abc objabc=new abc(); //here you are creating new object.
 abc.getdetails();
 here i am loosing the value.. set by my parameterized constructor 
} 
.

The basic rule for the parameterised constuctor is that the no. of objects of class gets created that many no. of copy of variables present inside the class gets initialised.

Here you cann't get the values of variables(using parameterised constructor) using default constructors.

for further visit this link[^]

this link[^]
 
Share this answer
 
Comments
Torakami 4-Nov-14 1:35am    
Please suggest the other way for this then ,...
Pravuprasad 4-Nov-14 2:49am    
Create a object of the class using parameterised constructor (with required value) globally. And use that object where-ever need comes.
Torakami 4-Nov-14 3:51am    
ok .. cool .. thanks for the suggestion ...
Torakami 4-Nov-14 5:08am    
no .. this is not working ..i filled the object on parameterized constructior , but like above when default constructor is called , value gets lost


public ObjectivesPdfReport()
{

}

public ObjectivesPdfReport(ExportToPdfDocument objExp)
{
objExport = new ExportToPdfDocument()
{
Connectionstring=objExp.Connectionstring,
DataForYear=objExp.DataForYear
};
}

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