passing a Property class to a new class

NachitoMax 411 Reputation points
2021-04-05T16:49:18.287+00:00

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 Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,817 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,328 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 47,471 Reputation points
    2021-04-05T19:40:18.467+00:00

    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.

    0 comments No comments