0

So I have the folowing code for a form.

Public Class tab
     Public personas, formaciones, avisos, cursos As List(Of Object)
     [Lots of Code]
End class

On another form I want the formaciones List, so I can just:

ListBox1.DataSource = tab.formaciones

And it works, perfectly.

But.. How?

tab is a class, not an instance of it, but vb is able to understand that I want the instance of that class.

What happen if there are mor than one tab open? How does it work internally?

Aimnox
  • 895
  • 7
  • 20
  • 2
    You could [read this](http://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c). VB create an instance of each form for you, that's why it works. But you would have to specify which instance you want if you have multiple forms. – the_lotus Jun 29 '16 at 11:49
  • @ThorstenDittmar actually, vb does create instance "on the fly" for each forms. – the_lotus Jun 29 '16 at 11:51
  • @the_lotus One more reason to stay away from VB as far as possible and learn a proper language in the first place... Anyway: In this example, `tab` is not a form? – Thorsten Dittmar Jun 29 '16 at 11:51
  • @ThorstenDittmar, yes poor naming. Even more when you consider that `tab` is my main form. So yes, it have an instance. But how does visual know that Im referring to that instance when I'm just using the class name? – Aimnox Jun 29 '16 at 11:52
  • @ThorstenDittmar I prefer C#, but work inforces vb use so... – Aimnox Jun 29 '16 at 11:53
  • @the_lotus And if I had multiple instances and I wrote the same line, which one would it access? – Aimnox Jun 29 '16 at 11:54
  • @Aimnox an instance with the same name as the class is generated. If you had multiple instance, you would have a variable for each so it would all depend on which variable you chose to use. – the_lotus Jun 29 '16 at 12:03
  • @the_lotus but it doesn't generate a new instance, it takes the existing one – Aimnox Jun 29 '16 at 12:50

1 Answers1

1

This is part of a default application framework that is enabled when creating VB.NET WinForms applications.

Its intent was to help the migration from VB6 so it creates singletons of each form.

If you want different instances, you can disable/ignore the framework and write your own start up methods.

More details:

Singleton forms: https://msdn.microsoft.com/en-us/library/ms233839.aspx

Enable/disable: https://msdn.microsoft.com/en-us/library/17k74w0c(v=vs.100).aspx

Full article: https://visualstudiomagazine.com/articles/2007/10/01/enable-the-application-framework-in-vb.aspx

Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45