Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form, Modules, Scope, and Recursion 3

Status
Not open for further replies.

Genimuse

Programmer
May 15, 2003
1,797
0
0
US
Versions of my question come up in my searching here, but they don't quite seem to address my problem.

Simple app, one form (called, cleverly, MainForm) and several code modules, modules that have almost nothing to do with the form (other than being called by it).

The form has a browser control and a timer. Every tick one a function in one of the modules is called (depending on the tick and other things).

In one case in one of the modules the Url of the browser control (cleverly called AppBrowser) has to be set. Because the control is not public by default, I just refer to the form to make a change, like this:
Code:
MainForm.AppBrowser.Url = StartupURI
This seems like the logical way to refer to things. Because of the error I'll list below, I also tried making AppBrowser public and taking off the "MainForm." part (which VS2005 is perfectly happy with, too), but it didn't solve the problem.

Here's the error I get: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'

This confuses me. I'm not calling this in the form, I'm calling it in a separate module. How is the form referring to itself? The message seems to indicate that this module code ended up in the instance... huh?

Referring to Me.AppBrowser doesn't work, of course, because it's not the module's control.

What am I missing?
 
I would need to see some more code to be sure.

But you have to realize that the compiler does some "weird" things. For example if you do this.

Code:
private sub callfunction()
  dim intval as integer = otherfunction()
end sub

private function otherfunction() as integer
  return  1 + 1
end function

will probably end up like this

Code:
private sub callfunction()
  dim intval as integer = 1+1
end sub

The compiler will rearrange your code. If a method is less then 100lines then it will probably end up in the calling method. Wich means a lot more IL but faster excecution of the code.

On the other hand, what you are describing seems odd. And I would never make a variable public. I would use a property to set the Url from within the form.

Something like

Code:
Public property BrowserURL() as URI'I suppose
  get
    return appbrowser.url
  end get
  set(byval value as uri)
    appbrowser.url = value
  end set

And now you can change the url by doing this.

Code:
MainForm.BrowserUrl = StartupUrl

If you want you can send me the app and I will look into it. You know where to find my address.



Christiaan Baes
Belgium

"My new site" - Me
 
The problem is that you have (i think) set as the startup object the MainForm. To have access to that form from other classes, you have to use reflection.. bla bla bla, where i cannot help you with. I have posted a rouine called DefInstance that will allow you to call the main form by its name, from anywhere.

Yes, if you are in the MainForm class, you can and must use the 'me' instead of the 'MainForm'. To solve this, add a module and write a 'sub main'. The Public Dim fm As MainForm, and last Application.Run (New fm).

And of cource Chris's way by writing some public properties.
 
Set your startup object to be a module.

In that module declare a public variable of the type MainForm.

In the startup method in that module instantiate your public MainForm object (myMainForm). Use either application.run(myMainForm) or myMainForm.showmodal to display the application.

Any code in myMainForm that references that form should use "me."

Any code outside of myMainForm that references that form should use the public variable from your module (modMain.myMainForm).

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks to everyone.

I first went with Rick's suggestion (which is an application of TipGiver's suggestion), and it worked.

Then I realized that I could instead put a reference to the form in the function call in MainForm and refer to it in the function in the module. This also worked.

Then finally I realized that there was only one function in my the module that ever referred to MainForm's properties, it was pretty small, and it made logical sense for it to be in MainForm's code, so I moved it there. LOL, et voila.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top