简体   繁体   中英

How to get string parts using the variable name

I have a program written in vb.net that contains a lot of variables very similar to each others. For example, one of them is

Public Shared iPhone4S_Firmware_8_0_Key = key

As you can see, my variables are composed by the model of the iPhone (iPhone 4S/5, etc.) and the firmware version (8.0/8.0.1, etc.) Now, when the user selects a combo of 'device' + 'firmware', my tool has to export the right variable (depending from the user's choice) in another string called 'active'.

I could do this manually with an if/then combination, but it would be a mess. So I'd like to proceed in this way:

Dim active as String = iPhoneModel + "_Firmware" + version + "_Key"

As you can guess, 'iPhoneModel' and 'version' are two other strings. So in this way I can always compose the right variable dynamically, but the problem is that if I write

Dim active as String = iPhoneModel + "_Firmware" + version + "_Key"

It thinks that 'iPhoneModel + "_Firmware" + version + "_Key"' is the content of the new variable. So how can I say that the program has to take the content from the already existing variable with that name; and NOT to take that name as variable's content?

Even though you know the string is comprised of sub parts, the compiler has no way of knowing how to map some parts to "Model" and some to "version" etc.

If you have different parts of a thing which independently have meaning, you can use a very simple class to store the parts:

Public Class PhoneItem
    Public Property Brand As String
    Public Property Model As String

    ' Note that there is an actual Version Type in NET
    Public Property FirmwareVer As String

    Public Property ActivationKey As String
    Public Property IsActive As Boolean

    Public Sub New(b As String, md As String)
        Brand = b
        Model = md

        FirmwareVer = ""
        ActivationKey = ""
        IsActive = False
    End Sub

    ...
    Public Overrides Function ToString() As String
        Return String.Format("{0}.{1}", Brand, Model)
    End Function
End Class

To create a new PhoneItem :

Dim pi As New PhoneItem("Nokia", "whizbang")
pi.FirmwareVer = "1.6.666"
pi.IsActive = SomeTest()
pi.ActivationKey = GetPhoneKey()

' create the string depending on what they choose,
' or perhaps create methods in the class to do it  
Dim thisPhone = pi.Brand & pi.Model & pi.FirmwareVer & pi.ActivationKey

The ToString() method on the class provides a default string representation of that phone item. In this case, it would return or print "Nokia.whizbang" as a sort of unique identifier. It might not be something the user sees, but the code may have a use for it.

If there are many possible phone make/models, you might store them in a dictionary and use the Make.Model as the key (we have no idea of what the data really looks like, but it seems that should be unique).

' collection of PhoneItems:
Private mcolP As Dictionary(Of String, PhoneItem)
...
mcolP = New Dictionary(Of String, PhoneItem)
mcolP.Add(pi.ToString, pi)

Any number of PhoneItem objects can be stored there as long as you use a unique key (the first param). To get one out the collection to work with:

Dim active As PhoneItem = mcolP("Samsung.FooBar")

Note also that the collection could be used to provide things like the contents for Make and Model ComboBoxes , so you aren't duplicating data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM