0

I have a vb or c# question (I'm doing it in vb but either way is fine) so I am going through this big case statement that is like this........

Select Case item("itemLabelType")
    Case "Shelf1"
        oItemLabel.Shelf1 = item("value")
    Case "Exp1"
        oItemLabel.Exp1 = item("Exp1")
    Case "CureDt1"
        oItemLabel.CureDt1 = item("CureDt1")
End Select

is there a way I can just do it (like in javascript) where I would say oItemLabel[foo] = bar

djv
  • 15,168
  • 7
  • 48
  • 72
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
  • A little more context is required, such as the type of `oItemLabel`. You could do this using reflection, but you generally don't want to. – CodeCaster May 01 '17 at 13:31
  • oItem label is an instance of a class. so I am trying to say oItemLabel.someVariableHere = somevaluehere – Bryan Dellinger May 01 '17 at 13:32
  • Yes, I understand that. What you want to do using the syntax you show can be done if that class provides a string indexer. – CodeCaster May 01 '17 at 13:33
  • not that I am familiar with it but I thought there was something called reflection that would allow this type of thing. – Bryan Dellinger May 01 '17 at 13:34
  • 3
    Yes, I mentioned that in my former comment. See for example [C# setting/getting the class properties by string name](http://stackoverflow.com/questions/10283206/c-sharp-setting-getting-the-class-properties-by-string-name) and [Can I set a property value with Reflection?](http://stackoverflow.com/questions/7718792/can-i-set-a-property-value-with-reflection). – CodeCaster May 01 '17 at 13:35
  • Should this `oItemLabel.Shelf1 = item("value")` be `oItemLabel.Shelf1 = item("Shelf1")` ? – djv May 01 '17 at 13:49
  • thanks codecaster your link worked well. I went with Dim [property] = GetType(ItemLabel).GetProperty(item("itemLabelType")) [property].SetValue(oItemLabel, item("value"), Nothing). Should I close this question as duplicate question since that link answered my question? – Bryan Dellinger May 01 '17 at 13:51

0 Answers0