.net - How to get string parts using the variable name -
i have program written in vb.net contains lot of variables similar each others. example, 1 of them is
public shared iphone4s_firmware_8_0_key = key
as can see, variables composed model of iphone (iphone 4s/5, etc.) , firmware version (8.0/8.0.1, etc.) now, when user selects combo of 'device' + 'firmware', tool has export right variable (depending user's choice) in string called 'active'.
i manually if/then combination, mess. i'd proceed in way:
dim active string = iphonemodel + "_firmware" + version + "_key"
as can guess, 'iphonemodel'
, 'version'
2 other strings. in way can compose right variable dynamically, problem if write
dim active string = iphonemodel + "_firmware" + version + "_key"
it thinks 'iphonemodel + "_firmware" + version + "_key"'
content of new variable. how can program has take content existing variable name; , not take name variable's content?
even though know string comprised of sub parts, compiler has no way of knowing how map parts "model" , "version" etc.
if have different parts of thing independently have meaning, can use simple class store parts:
public class phoneitem public property brand string public property model string ' note there actual version type in net public property firmwarever string public property activationkey string public property isactive boolean public sub new(b string, md string) brand = b model = md firmwarever = "" activationkey = "" isactive = false end sub ... public overrides function tostring() string return string.format("{0}.{1}", brand, model) end function end class
to create new phoneitem
:
dim pi new phoneitem("nokia", "whizbang") pi.firmwarever = "1.6.666" pi.isactive = sometest() pi.activationkey = getphonekey() ' create string depending on choose, ' or perhaps create methods in class dim thisphone = pi.brand & pi.model & pi.firmwarever & pi.activationkey
the tostring()
method on class provides default string representation of phone item. in case, return or print "nokia.whizbang" sort of unique identifier. might not user sees, code may have use it.
if there many possible phone make/models, might store them in dictionary , use make.model
key (we have no idea of data looks like, seems should unique).
' collection of phoneitems: private mcolp dictionary(of string, phoneitem) ... mcolp = new dictionary(of string, phoneitem) mcolp.add(pi.tostring, pi)
any number of phoneitem
objects can stored there long use unique key (the first param). 1 out collection work with:
dim active phoneitem = mcolp("samsung.foobar")
note collection used provide things contents make , model comboboxes
, aren't duplicating data.
Comments
Post a Comment