10 Replies - 790 Views - Last Post: 01 November 2016 - 02:31 PM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 27-January 15

Saving reference to new object

Posted 01 November 2016 - 09:38 AM

Hi there!
I've got some difficulties with English terminology...

I have some Object.
And I have an event handler, which creates an instance of that Object on return.

How can I save reference to new Object?

Here's the code:
  private void Object_Create(object sender, ObjectEventArgs e)
        { 
   Object wb1 = e.Something;
        }


Thank you in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Saving reference to new object

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,221
  • Joined: 07-April 08

Re: Saving reference to new object

Posted 01 November 2016 - 10:07 AM

What exactly do you mean by save reference? Do you mean make it so that the Object is usable in somewhere other than Object_Create? If that is the case then declare your variable at a higher scope. I.E. create it at the class level. It seems the route you are going is prone to errors though. Why would you have a route that's sole purpose is to declare an object?
Was This Post Helpful? 1
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 27-January 15

Re: Saving reference to new object

Posted 01 November 2016 - 10:14 AM

View Postrgfirefly24, on 01 November 2016 - 10:07 AM, said:

What exactly do you mean by save reference? Do you mean make it so that the Object is usable in somewhere other than Object_Create? If that is the case then declare your variable at a higher scope. I.E. create it at the class level. It seems the route you are going is prone to errors though. Why would you have a route that's sole purpose is to declare an object?


actually, that's the problem - I am following an instruction and I don't know what stage "save reference to new Object" means...

I cannot declare it outside event handler because it is connected with event's args...

I can post it here if it will help. It is about GeckoFX Webbrowser control, which I named Object in my question.
Was This Post Helpful? 0
  • +
  • -

#4 Ratatoskr   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 223
  • Joined: 02-January 16

Re: Saving reference to new object

Posted 01 November 2016 - 10:28 AM

Perhaps you should give us context on what you are trying to do and what specifically is this object you are grabbing and why.

As the user above said, not quite sure what you mean by 'save reference'. Also, you can take it outside of the event if you declare an object in the scope of your class and assign it inside that event handler. I also suspect if at some point you are going to be needing to cast what ever this object is - an irrelevant curiosity.

This post has been edited by Ratatoskr: 01 November 2016 - 10:29 AM

Was This Post Helpful? 1
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 27-January 15

Re: Saving reference to new object

Posted 01 November 2016 - 11:29 AM

Ok. I am working with GeckoWebBrowser object.

Here is the original code:

    private void GeckoWebBrowser_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
        { 
   GeckoWebBrowser wb1 = e.WebBrowser;
   wb1.DocumentCompleted += Wb1_DocumentCompleted1;
        }

        private void Wb1_DocumentCompleted1(object sender, GeckoDocumentCompletedEventArgs e)
        {
            MessageBox.Show("Success.");
        }


It gives me an error "Object reference not set to an instance of an object."

And here's the instruction I tried to follow:

The GeckoCreateWindowEventArgs argument in webBrowser_CreateWindow contains a WebBrowser property that you can set to be a new instance of GeckoWebBrowser. After the event handler returns, the instance of GeckoWebBrowser that you provided to the GeckoCreateWindowEventArgs will have the popup document loaded in it. You should save a reference to the new GeckoWebBrowser, add a handler for it's DocumentCompleted event, and then check it for the element you want to click.

Thank you for your answers!

This post has been edited by Skydiver: 01 November 2016 - 01:08 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Ratatoskr   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 223
  • Joined: 02-January 16

Re: Saving reference to new object

Posted 01 November 2016 - 12:00 PM

Okay, so now we are getting somewhere. It would be helpful if you told me what specifically is giving you this error. Is it 'wb1' or 'e.WebBrowser' ?

Also, is the code you provided the code you are using? Or an example provided to you? If the latter, I'd want the code you are using. If the prior, ignore this portion of my post.

This post has been edited by Ratatoskr: 01 November 2016 - 12:01 PM

Was This Post Helpful? 1
  • +
  • -

#7 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 27-January 15

Re: Saving reference to new object

Posted 01 November 2016 - 12:53 PM

That's the code I'm using, got no example :(/>

Program runtime breaks at the following line and then I get the error:

wb1.DocumentCompleted += Wb1_DocumentCompleted1;

This post has been edited by Skydiver: 01 November 2016 - 01:07 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Saving reference to new object

Posted 01 November 2016 - 01:07 PM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#9 Ratatoskr   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 223
  • Joined: 02-January 16

Re: Saving reference to new object

Posted 01 November 2016 - 01:15 PM

Okay, so what seems to be happening is your 'wb1' isn't being assigned. It seems your e.WebBroswer needs to be declared, not retrieved based on re-reading the text.

"The GeckoCreateWindowEventArgs argument in webBrowser_CreateWindow contains a WebBrowser property that you can set to be a new instance of GeckoWebBrowser"


Try this.
 private void GeckoWebBrowser_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
     {
e.WebBrowser = new GeckoWebBrowser();
e.WebBrowser.DocumentCompleted += Wb1_DocumentCompleted1;
     }

     private void Wb1_DocumentCompleted1(object sender, GeckoDocumentCompletedEventArgs e)
     {
         MessageBox.Show("Success.");
     }



I hope to not be the blind leading the blind, just trying to think this through with you.
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Saving reference to new object

Posted 01 November 2016 - 01:27 PM

Based on the code for OpenGeckoSharp I found here, it looks like the instructions given to you in StackOverflow are wrong at worse, or incomplete at best.

The source code I linked to shows that the WebBrowser property is initially null when passed on to you. You have the option of creating your browser and putting it into the WebBrowser property, or if you leave it to be null, then one will be created for you when the event handler is done.

So basically you'll need to create a new instance of a browser like they do over in this other answer in StackOverflow.

The reason that the first answer was telling you that you need to retain a reference to the new browser that you create is because there is a pretty good chance that it can get garbage collected easily because there will be no other reference to it after the create window event handler is completed. The event handler object is a transient object, and if there is no other reference to the new browser, it'll be a prime candidate for GC.
Was This Post Helpful? 1
  • +
  • -

#11 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 27-January 15

Re: Saving reference to new object

Posted 01 November 2016 - 02:31 PM

Thank both of you so much!

Especially Skydiver for the complete explanation and example on Stack, wasn't able to find this one^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1