CallByName() function for variables and constants...

  • Thread starter Thread starter GypsyPrince2k
  • Start date Start date
G

GypsyPrince2k

Guest
Does there exist, or does any one know of, a function which works very similar to 'CallByName()' - but for selecting variables and/or constants based on a string value? We have many modules and classes, each containing roughly 100 or so constants. I can't post any factual sample code here of what I'm doing because I'm a military contractor and they sort of look down on me doing that. But, the gist is that sometimes in code, we don't know which variable or constant to reference or return in a function until we either get input from a user or another function tells us.

Here is a mock example of what we're trying to do...


Private Const m_ERR_1000_ENUS As System.String = "My error message in English (U.S.A.) for error number 1000."

Private Const m_ERR_1000_ESES As System.String = "My error message in Spanish (Spain) for error number 1000."

Private Const m_ERR_1001_ENUS As System.String = "My error message in English (U.S.A.) for error number 1001."

Private Const m_ERR_1001_ESES As System.String = "My error message in Spanish (Spain) for error number 1001."

Private Const m_ERR_1002_ENUS As System.String = "My error message in English (U.S.A.) for error number 1002."

Private Const m_ERR_1002_ESES As System.String = "My error message in Spanish (Spain) for error number 1002."



Public Sub Main()

MsgBox(GetErrMsg(1001, 1034))

End Sub



'Returns an error message string based on the supplied error number and system locale...

Friend Sub GetErrMsg(ByVal ErrNumber As System.Int32, ByVal Locale As System.Int32) As System.String

Dim strErrMbr As System.String 'Retains the name of the member (constant) to be selected...

Dim strLocale As System.String 'Retains the locale identifier...


Select Case Locale 'Determine the specified locale...

Case 1033 'English (U.S.A.)…

strLocale = "ENUS"

Case 1034 'Spanish (Spain)…

strLocale = "ESES"

End Select


strErrMbr = strErrMbr.Concat("m_ERR_", ErrNumber, "_", strLocale) 'Build the name of the constant to be selected...



'Choose which constant to reference based on the string's value...

Return MemberByName(strErrMbr) 'Reference the appropriate constant and return its literal value (error message)...

End Sub



If "strErrMbr" ends up as "m_ERR_1001_ESES", then the statement "Return MemberByName(strErrMbr)" would be identical to "Return m_ERR_1001_ESES", and so on. MsgBox(MemberByName(strErrMbr)) and MsgBox(m_ERR_1001_ESES) would be equal and an return the same message string of: "My error message in Spanish (Spain) for error number 1001."


Now... before anyone posts 100 different ways to handle localization - localization is NOT what we're trying to achieve. As I said, I can't post any sample of what we're actually trying to do. Also, what we currently have is a really HUGE function with a very long Select Case structure that works something similar to:


Select Case strErrMbr

Case "m_ERR_1000_ENUS"

Return m_ERR_1000_ENUS

Case "m_ERR_1000_ESES"

Return m_ERR_1000_ESES

Case "m_ERR_1001_ENUS"

Return m_ERR_1001_ENUS

Case "m_ERR_1001_ESES"

Return m_ERR_1001_ESES

End Select



It works very well, but is an extremely HUGE function. And, each time we add a new constant (along with its literal value) to our code, we have to also add it to inside the function as a possible return item.

So... to lessen our code, and therefore, the size of our source files, is there a function like "MemberByName()" in existence? If not, WHY NOT? Microsoft needs to create one, like now, that works for both variables and constants. Or, does any one have any ideas as to going about creating such a function?

We have some constraints. a. Because we use proprietary/military obfuscation software, using dictionaries or external XML files (or other non-encryptable external/support files) are NOT options, so don't even waste space suggesting either. Any solutions must be hard-coded. b. Creating dynamic members (constant and/or variables on the fly) is not an option either.


Anyone who has some ideas on how to create such a function, we'd love to hear from you. And... GO!

Continue reading...
 
Back
Top