[英]how to create multiple objects with same properties for a class but values are different to each object
protected void page load()
{
blick=1;
j=0;
}
protected button1_click()
{
bclick=blick+1;
j=j+1;
ReferenceDetails[]rfobj=new ReferenceDetails[bclick];
if(j<=bclick)
{
rfobj[j]=new ReferenceDetails();
rfobj[j].name=txtrfname.text;
rfobj[j].lastname=txtrflastname.text;
}
}
protected preview button_click()
{
Response.write(rfobj[1].name); // HOW i should achieve this
Response.write(rfobj[2].name);
//object reference is not set to instance of an object
}
I want to create a new object for the same class with same properties but values are different. 我想为具有相同属性但值不同的相同类创建一个新对象。 On Button first click one object want to create with some properties and bind with values according to user enters.
在“按钮”上,首先单击要创建的具有某些属性的对象,并根据用户输入的值绑定它们。
If the button clicked once again then again a new object wants to be created with same properties but different values what user enters at that time and those values want to bind with that corresponding object. 如果再次单击该按钮,则要再次创建一个具有相同属性但用户输入的值不同的新对象,而这些值要与该对应对象绑定。
Then I want to get the each newly generated object with their associated values in another button click to show it to on Gridview. 然后,我想在另一个按钮中单击以将其显示在Gridview上,以获取每个新生成的对象及其关联的值。 But object is generating newly in the above code as
rfobj[1], rfobj[2]
, but when I go to next button and access that values their will be null. 但是对象在上面的代码中以
rfobj[1], rfobj[2]
,但是当我转到下一个按钮并访问该值时,它们将为null。
How do I overcome it? 我该如何克服? Can anyone provide me with a solution for this problem?
谁能为我提供解决此问题的方法?
the rfobj array is being created fresh on each button click, so each time it only has the last entry in it 每次单击按钮都会重新创建rfobj数组,因此每次它只有最后一个条目时
You need to move it to the form - like the 'blick' counter. 您需要将其移至表格-类似于“ blick”计数器。
This is far away from good programming technique, and may be you are having memory leaks... 这与良好的编程技术相去甚远,可能是您内存泄漏。
but to solve your question, the problem is: 但是要解决您的问题,问题是:
when you click the button second time, the array is recreated, with the number of buttons, but you fill only the last button, the prior buttons remaisn null, because is anew object. 当您第二次单击该按钮时,将重新创建带有按钮数量的数组,但是您只填充了最后一个按钮,因此先前的按钮将保留为空,因为它是一个新对象。
you must backup your reference before recreate, and then fill the previous array slots with previous created objects... 您必须先备份参考,然后再重新创建,然后用先前创建的对象填充先前的阵列插槽...
something like this: 像这样的东西:
in the form: 形式:
ReferenceDetails[] rfobj;
int bclick = 0;
in the button: 在按钮中:
int old_count = bclick;
bclick=blick+1;
//j=j+1;
ReferenceDetails[] old_rfobj = rfobj;
rfobj = new ReferenceDetails[bclick];
for ( int i = 0; i < old_count; i++ ){
rfobj[i] = old_rfobj[i];
}
if (j<=bclick) {
//rfobj[j]=new ReferenceDetails(); you dont need this
rfobj[bclick-1].name=txtrfname.text;
rfobj[bclick-1].lastname=txtrflastname.text;
}
This shold work for you, but you may try understand what happens in background, when create and release objects... the new
word requests memory from system... and this memory must be released anywhen... 此句柄为您工作,但是您可以尝试了解在后台发生的情况,创建和释放对象时...
new
单词向系统请求内存...并且任何时候都必须释放该内存...
Best regards 最好的祝福
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.