Home > Net >  How to access public class property and preserve/modify an instance of class property value in sever
How to access public class property and preserve/modify an instance of class property value in sever

Time:03-23

Class LoanApplicant

Private mstrPropertyName As String

Property Get Name() As String
    Name = mstrPropertyName
End Property

Property Let Name(rData As String)
    mstrPropertyName = rData 
End Property

Sub to initialise the class and assign a value to the property

Public ApplicantName As String
Sub Initilise()
    Dim Applicant1 As LoanApplicant
    
    ApplicantName = "Steve"
    
    Set Applicant1 = New LoanApplicant
    Applicant1.Name = "Frank"

End Sub

Sub to print, first message box works as it is a public variable but second does not.

Sub CreateClass(Applicant1 As Object)

    Call Initilise
    MsgBox (ApplicantName)
    MsgBox (Applicant1.Name)

End Sub

Create a class with a property

Then, I have two modules. let's say in the first module, I initilise the class and create an instance of a class, assigned value to the class property etc.

Question is, how do I access the same instance property value in another module? I have tested in my example that an instance of the class would be destroyed when exit a sub. the value of the class property is not preserved.

Thanks

Rational for the question is that in real world, when create an applicant class, it can have multiple applicant instance, applicant1, applicant2 etc. At various modules, the program would have to modify the same applicant's attributes(properties).

CodePudding user response:

In your Initilise procedure, once you create an instance of LoanApplicant and assign the Name property a name, you need to call your CreateClass procedure and pass it your newly created object.

Here's an example for you to look at. Notice, though, I changed the name of your procedure from CreateClass to DisplayMessage, in order to reflect its true purpose.

Public ApplicantName As String

Sub Initilise()

    ApplicantName = "Steve"
    
    Dim Applicant1 As LoanApplicant
    Set Applicant1 = New LoanApplicant
    Applicant1.Name = "Frank"
    
    DisplayMessage Applicant1

End Sub

Sub DisplayMessage(Applicant As Object)

    MsgBox ApplicantName
    
    MsgBox Applicant.Name
    
End Sub

CodePudding user response:

So I got a new version of what I want.

Overall module and subs

Sub Main()
    Dim Applicant1 As LoanApplicant

    Set Applicant1 = New LoanApplicant      
    ApplicantCreate Applicant1
    IncomeEntry Applicant1
    ExpenseEntry Applicant1

    MsgBox (Applicant1.name)
    MsgBox (Applicant1.Income)
    MsgBox (Applicant1.Expense)     
End Sub

Sub ApplicantCreate(Applicant As Object)
   Applicant.name = "Frank"  
End Sub

Sub IncomeEntry(Applicant As Object)
    Applicant.Income = 100000
End Sub

Sub ExpenseEntry(Applicant As Object)
    Applicant.Expense = 5000
End Sub

Each sub can be placed into different module that does not change the results of what I wanted.

This is just a simplified version of the business logic it would apply in each sub. In reality, it can be much more complex. So, I got that.

Next question in line is whether it is possible to have an alternative approach? I mean, this works fine when I know there is only ONE applicant. What if I do not know how many applicants would be generated in Main? Also, do I have to create all the instant of different object in Main? If I use this approach, I would think I have to but I just like to know if there are other options. For example, if I can create dynamic object variable in any of the sub?

  •  Tags:  
  • vba
  • Related