referencing class in multiple forms

NachitoMax 411 Reputation points
2021-04-15T19:25:57.25+00:00

Hi

thought it would be easy enough, maybe im missing something logically...

As i select a product, i load that specific product form. in the form, a reference a class instance (Info) that has already been instantiated on startup in ApplicationEvents-

Dim Inf As New Info

as i load my product form, i have set it like that to access the class without creating a new instance of it

Private _Inf as Info

In my New class, i do this

_Inf = My.Application.Inf

this seems to work but i dont think its the best method. however, when i do the exact same in a sub UserControl on the product form, i get an exception error on a control EditEvent. I understand that this is because the _Inf has completely been qualified yet as the form isnt 100% instantiated.

So

what is a good method for multiple forms to access the same class containing data?

i could use My.Application.Inf for all access but is there a better way?

Thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,264 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. NachitoMax 411 Reputation points
    2021-04-15T21:34:22.52+00:00

    Thanks for being thorough. I did prefer the method of passing parameters to the Constructor but i can also see the benefits of the delegates. Its probably too time consuming now to revert back but we'll see..

    There arent any business reasons withheld.

    • there is 100% guarantee that only 1 product will be open at a time, its the way we need to operate.
    • The application is a plugin to a 3rd party software
    • each product is a simple left to right process to each ChildForm with a function at the end to process the data to a dataset

    i have had various versions, all working in their own way. At one point, i did have all subs containing their own data properties but as the complexity grew, it became more difficult passing those property values between subforms without creating a bunch of extra handling code (as the process moves left to right, some property values are required in the next subforms etc). In my mind, if

    • each subform only had to look at 1 dataclass to get / set property values
    • then invoke a ControlChangeEvent in the ParentForm to update
    • ParentForm collects all properties from DataClass and makes the graphical update.

    I dont want to get into bad coding habits so I might revert to something like this-

    • ParentForm contains all ProductProperties (no Shared class, members or new instance)
    • ChildForms contain all relevant variable members specific to that ChildForm
    • on a control update, invoke a data collector from the ParentForm to update the Properties
    • ParentForm graphically updates what it needs to
    • On a ChildForm NavigationEvent, collect the required data and pass to the req. ChildForm
    • ParentForm processes final data to a dataset

    The graphical update on the parent form is a realtime result of the updated data. Is mainly materials & sizes in a representation of the product.

    Is this something that would more inline with a typical construction?

    Thanks for your help :)


  2. NachitoMax 411 Reputation points
    2021-04-15T22:12:07.59+00:00

    Hey

    to fully qualify and correct my earlier comments, the Delegate advice i had was related to my method of passing the Form to the constructor as a form which is different in this case like this-

    Dim UC1 As New UserControl1 = UserControl1(me)
    

    then in my usercontrol

    Private _UC1 as UserControl1
    Public Sub New(ByVal UC As UserControl1)
       _UC1 = UC
    End Sub
    

    I have since added in the delegate classes required and its better.

    I will look at this method which is similar to your approach by separating the properties into a single class-

    In ParentForm

    Private Inf As New Info = InfoClass
    
    Friend Sub LoadChilds()
       Dim C1 As New Child1 = Child1(Inf)
       Dim C2 As New Child2 = Child2(Inf)
    End Sub
    

    In ChildForm

    Private _Inf As InfoClass
    Public Sub New(ByRef Inf As InfoClass)
       _Inf = Inf
    End Sub
    
    Private Sub Dosomething()
       Dim GetVal1 As String = Inf.GetSomething
    End Sub
    
    0 comments No comments