简体   繁体   English

如何为一个类创建具有相同属性但值与每个对象不同的多个对象

[英]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.

相关问题 如何在C#中比较两个不同类对象的相同属性? - How to compare the same properties of two different class objects in C#? 如何创建与另一个 class 的多个关系作为不同的属性? - How to create multiple relations to another class as different properties? 在每个类中使用不同的格式序列化同一类中的多个DateTime属性 - Serializing multiple DateTime properties in the same class using different formats for each one 如何将具有多个字符串属性的对象序列化为每个具有一个字符串属性的对象的 json 数组? - How to serialize an object with multiple string properties to a json array of objects with one string property each? 如何让一个 function 创建多个不同类型的相同 class - How to have one function create multiple different types of the same class LINQ to Objects:如何在同一个对象列表中连接不同的属性 - LINQ to Objects: How to join different properties in the same List of objects 如何创建类的(多态)集合/列表/数组,其中每个类可能具有一些不同的属性 - How to create a (Polymorphism) collection/list/array of a class where each one may have some different properties 如何将多个对象映射到具有源对象作为属性的单个对象? - How to map multiple objects to a single object that has source objects as properties in it? 具有多个相同类型但具有不同限制的列表属性的类 - Class with multiple List Properties of same type but with different restrictions 在 C# 中创建每次都可以具有不同属性的类或对象 - Create an class or an object that can have different properties everytime in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM