Link to home
Create AccountLog in
Avatar of PeterFrb
PeterFrbFlag for United States of America

asked on

Indirect referencing of class variables in VB.Net

I have a couble of class modules that contain several variables, and I place these class modules into a couple of arrays.  I would like to write a function that extracts an array of strings, passing the variable name as a string variable.  Here's an example, performing the act explicitly.

Public Class Class_Sample
    Public strQuery As String
    Public strWorksheet As String
End Class

Sub ExtractVariable()
    Dim Array(3) as Class_Sample

    Array(0) = New Class_Sample
    With Array(0)
        .strQuery = "Query_Hot"
        .strWorksheet = "Wksht_Hot"
    End With

    Array(1) = New Class_Sample
    With Array(1)
        .strQuery = "Query_Cold"
        .strWorksheet = "Wksht_Cold"
    End With

    Array(2) = New Class_Sample
    With Array(2)
        .strQuery = "Query_Warm"
        .strWorksheet = "Wksht_Warm"
    End With

    Array(3) = New Class_Sample
    With Array(3)
        .strQuery = "Query_JustRight"
        .strWorksheet = "Wksht_JustRight"
    End With

    Dim strQueries() as string = Extract_Queries(Array)
    Dim strWorksheets() as string = Extract_Worksheets(Array)
End Sub

Function Extract_Queries(Array() as Object) as string()
    Dim Return_Queries(ubound(Array)) as string
    For icount as integer = 0 to ubound(Array)
        Return_Queries(icount) = Array(icount).strQuery
    Next icount
    Extract_Queries = Return_Queries
end Function

Function Extract_Worksheets(Array() as Object) as string()
    Dim Return_Worksheets(ubound(Array)) as string
    For icount as integer = 0 to ubound(Array)
        Return_Worksheets(icount) = Array(icount).strWorksheet
    Next icount
    Extract_Worksheets = Return_Worksheets
end Function

Open in new window

My ideal would be to create a generic function, with "strQuery" and "strWorksheet" as parameters, to achieve the same results as I've done here with two functions.  I'm guessing that I could use an array of Objects as a parameter, which would allow me to pass an array of any class (although having written "Array() as Object" here, I have not yet tested the functionality to make sure this works).  

The advantages of genericizing the functionality of variable extraction are plainly obvious.

Thanks, ~Peter Ferber
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of PeterFrb

ASKER

Superb!  I knew there was a way.  Many thanks.  ~Peter Ferber