0
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. 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.

How do I overcome it? Can anyone provide me with a solution for this problem?

0

2 Answers 2

3

the rfobj array is being created fresh on each button click, so each time it only has the last entry in it

You need to move it to the form - like the 'blick' counter.

Sign up to request clarification or add additional context in comments.

2 Comments

if button1_ click one time i want to create a objects with some properties and associated values which user enters dynamically.I want to bind those dynamically values to that object created .Then i will return to my user screen if the same button1 clicked again that is second time another new object want to create with same properties and new values like that it continues until that button will not clicked. Then on another preview-click i want to access all the objects with their data which created on button1-click .
I know what you want to do, and I explained why your code isnt working. YOu need to declare rjobj in the page load function not in the click function
0

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...

Best regards

8 Comments

can i get the objects data in another button-click for showing to the user.
you must declare ReferenceDetails[] rfobj in the form, and then fill the array after recreate it, like I showed you...
see my update... you dont need the J too, and the bclick holds the desired (when new) array length
I am getting object reference nit set to instance of an object
In rfobj(bclick-1)showing object not set to Instance of an object
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.