1

For example if I have code like this

Private Function getCompleteAddress(ByVal sCountryCode as String, ByVal sStateCode as 
String, ByVal sCityCode as String) as String

    If sCountryCode ="US" 
          If StateCode = "AL"
             If CitiCode = "BHM"
                   getCompleteAddress=AddressBook.USALBHM
             End If
           ElseIf StateCode = "WA"
             If CitiCode = "SEA"
                   getCompleteAddress=AddressBook.USWASEA
             End If
           ElseIf StateCode = "ME"
             If CitiCode = "PWM"
                   getCompleteAddress=AddressBook.USMEPWM
             End If
           ElseIf StateCode = "PA"
             If CitiCode = "PHL"
                   getCompleteAddress=AddressBook.USPAPHL
             End If
           ElseIf
                ......
           ElseIf
                ......
End Function

I want the code to looks like this

Private Function getCompleteAddress(ByVal sCountryCode as String, ByVal sStateCode as 
String, ByVal sCityCode as String)
 
Dim sCombinedCode as String = sCountryCode + sStateCode + CityCode
getCompleteAddress = AddressBook.sCominedCode
    
End Function

Is it possible? Please note sCombinedCode is not a member of AddressBook it is just a way I imagined of to produce short cleaner code.

shadow_wxh
  • 366
  • 3
  • 17
  • is AddressBook an enum? – TYeung Aug 28 '21 at 14:36
  • no, consider it a ordinary customized class with list of String Member Variables. – shadow_wxh Aug 28 '21 at 14:56
  • @shadow_wxh Could `AddressBook` be changed to a `Dictionary(Of String, Something)` where the first `String` is the combined code? It might help if you could edit the question to show the current definition of `AddressBook` - there could be a better data structure for it. – Andrew Morton Aug 28 '21 at 15:12
  • AddressBook is just a list of Strings these is no other structure. Theoretically yes I could change it to Dictionary and get Complete String by searching the letter code but the Dictionary is exceptionally large. And also I am trying to explore some alternative way to program. – shadow_wxh Aug 29 '21 at 05:32

1 Answers1

1

Read Access property using its name in vb.net

You can use

Private Function getCompleteAddress(ByVal sCountryCode As String, ByVal sStateCode As 
String, ByVal sCityCode as String)
 
Dim sCombinedCode As String = sCountryCode + sStateCode + CityCode
    getCompleteAddress = CallByName(AddressBook, sCountryCode & sStateCode & CityCode, Microsoft.VisualBasic.CallType.Get, Nothing)
End Function

Or

Module Module1
    Public Class AddressBookClass
        Public Property USALBHM As String = "USALBHM"
        Public Property USWASEA As String = "USWASEA"
        Public Function GetPropertyValue(ByVal obj As Object, ByVal PropName As String) As Object
            Dim objType As Type = obj.GetType()
            Dim pInfo As System.Reflection.PropertyInfo = objType.GetProperty(PropName)
            Dim PropValue As Object = pInfo.GetValue(obj, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
            Return PropValue
        End Function
    End Class

    Sub Main()
        Dim AddressBook As New AddressBookClass
        Console.WriteLine(AddressBook.GetPropertyValue(AddressBook, "USALBHM"))
        Console.ReadLine()
    End Sub
End Module

I leave any consequences to you :)

TYeung
  • 2,579
  • 2
  • 15
  • 30