4

I have a Module of Public Properties (named Constants for what it's worth), none of which are associated to any specific object. I need to invoke those properties based on the value in a string variable. That variable's value will be the name of the property I need to use.

The best candidate I know of for something like this would be CallByName, but that requires an object to get the property from, which as I mentioned, doesn't exist in this case.

As an example, the first value that the variable should take is "Master". When it comes time I want to be able to use that variable to call up the property Master() (a string array declared as a public property).

Thanks in advance!

7
  • 3
    You could use reflection to do this. However, it's worth mentioning that, in most cases, using a Dictionary object to store these named values would be a better design. Commented Dec 2, 2013 at 21:50
  • On further thought the example I gave might not have been the best. Most of the properties I'll need to reference are lists of objects. They get populated as I compare data from one source (my company's db) against data that should be the same from our customer's db. I then intend to use the lists to output the problem records where we have conflicting data, or one or both of us has bad data. Would it be better if I simply declared a dummy object class to store the properties so I could use reflection or callbyname? Commented Dec 2, 2013 at 22:49
  • 2
    Now it definitely sounds like a job for a dictionary. You can make a dictionary which contains a list of objects as it's value for each key. For instance, you could create a Dictionary(Of String, List(Of Object)). Commented Dec 2, 2013 at 23:03
  • I've setup the Dictionaries I needed and they seem to be working. For the future though what would be the best way to architect a set of common storage properties so that they can be used by multiple reports? The idea would be that I'd interate through a string array of the report names, set a variable to the name of the report type I'm trying to generate, and then use that variable to look up multiple properties of varying types. Commented Dec 3, 2013 at 16:38
  • 1
    That would be difficult to answer without more details. Commented Dec 3, 2013 at 16:40

2 Answers 2

1

It sounds to me like your trying to use strings in the same way Enum values work. There are a few ways to do this, but here is a simple example that might help you think about ways to resolve your strings to constants programming issue.

Namespace Constants

Public Enum Priorities
    Unknown = 0
    Low = 1
    Medium = 2
    High = 3
    VeryHigh = 4
End Enum

Public Class ConverterTo
    Public Function Priority(ByVal value As String) As Constants.Priorities
        Select Case value.ToLower
            Case "low"
                Return Priorities.Low
            Case "medium"
                Return Priorities.Medium
            Case "high"
                Return Priorities.High
            Case "veryhigh"
                Return Priorities.VeryHigh
            Case Else
                Return Priorities.Unknown
        End Select
    End Function
End Class
End Namespace
Sign up to request clarification or add additional context in comments.

3 Comments

I'd change some names, but the basic idea (use a Select Case) is sound... with the addition that, if you have a lot of these, you should turn it into a dictionary lookup instead.
Sorry, but not so much. The intent is the properties are used to store counts and objects (the items being counted including all their details so I can push out whatever values from them I'd like). The dictionaries idea Steven mentioned got me a lot of a the way. I'm having some trouble, but I'm sure it's just a question of tweaking my implementation.
Perhaps you could adjust the question to include that information and the code you have so far, so we can all contribute to a good solution.
0

So what I ended up having to do is declaring an instance of the ConsObj (there was a rename since initial posting) and that object now gets passed to the function that outputs the values from the properties.

Thanks to everyone for the suggestions, some gave me ideas on how to do some other things better elsewhere in the script. Sorry I never posted code, just a bit of a hassle as the code is on a network w/o internet access.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.