question

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 asked cooldadtx answered

passing a Property class to a new class

Hi

on my current form Class, i am passing a collection of values that are stored in another class property. Example:

 'shared with other classes to use the same info
 'populated from database
 Public class ProjectInfo
    Public Property Name As String
    Public Property Address As String
    Public Property Contact As String
 End Class

 Public Class Form1
    Public Sub ProcessInfo()
       Dim P As New Process(ProjectInfo.Name, ProjectInfo.Address, ProjectInfo.Contact)
       P.DoSomethingWithProperties()
    End sub
 End Class
    
 Public Class Process
    Private _ProjName As String = String.empty
    Private _ProjAddress As String = String.Empty
    Private _ProjContact As String = String.Empty
    
    Public Sub New(ByVal ProjName As String, ByVal ProjAddress As String, ByVal ProjContact As String)
    
       _ProjName = ProjName
       _ProjAddress = ProjAddress
       _ProjContact = ProjContact
    End Sub
    
    Friend Sub DoSomethingWithProperties()
       'do something here
    End Sub
 End Class

Obviously there is a lot of code here and i may not be doing this efficiently. My question is, can i pass the ProjectInfo class in its entirety to the Process Class as a single object without needing to set each property in the New() method individually so that i could then call any property from ProjectInfo from inside the Process Class?


Thanks

windows-formsdotnet-runtimedotnet-standard
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

Yes. There is a difference between a variable and the instance (of data) it stores. New creates an instance in memory that you then normally assign to a variable. The instance stores the data (the fields) and members (properties and methods) provide calling code access to it. The values in the fields are persisted as long as the instance remains in memory.

To use the instance it must be assigned to a variable (or perhaps stored in a temporary expression by the compiler). Variables can be assigned to other variables or passed as arguments to methods. This is just causing other variables/parameters to be referencing the same instance in memory. Any of them can access the members and interact with the instance as they see fit.

So you can create your ProcessInfo instance (via New) and then assign it to a variable. You can pass that variable's value (the instance) to another type and, as long as the other type keeps a variable to it, then it has access to the original instance and its data.

I recommend you read up on reference variables for more information on how this works.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.