0

I have an enum which is array list ( its optional you can add items up to 5 times)

Public DiseaseTreatment() As TreatmentsetDiseaseTreatment

Public Enum TreatmentsetDiseaseTreatment
'''<remarks/>
<System.Xml.Serialization.XmlEnumAttribute("01")>  _
Item01

''<remarks/>
Antibiotics
''<remarks/>
<System.Xml.Serialization.XmlEnumAttribute("02")>  _
Item02

'''<remarks/>
<System.Xml.Serialization.XmlEnumAttribute("Pain Killers)")>  _
PainKillers

break

Dim temptreatment As List(Of TreatmentsetDiseaseTreatment)= New List(Of TreatmentsetDiseaseTreatment)()
temptreatment.Add("Painkiller")
For i As Integer = 0 To temptreatment(i) - 1
   test.TreatmentTypes(i) = temptreatment(i)
Next

When I hover over tempTreatment(i) it has the right string "painkiller" so I do not understand how is it throwing NullReferenceException?

Igor
  • 60,821
  • 10
  • 100
  • 175
Sam
  • 61
  • 10
  • Enums are not arrays, lists or arraylists. They are just Enums - groups of constants with names. What is `TreatmentsetDiseaseTreatment` ? – Ňɏssa Pøngjǣrdenlarp Jun 23 '17 at 14:59
  • Sorry I updated the code. That is the Enum name, I've copied the correct line.. I tried loads of stuff and I don't get it at all.. I managed to do all the ones without the () the above DiseaseTreatment() is list. so how shall I deal with it ? – Sam Jun 23 '17 at 15:04
  • No, its not duplicate. My one when I hover over it show me data but then I get error?!! – Sam Jun 23 '17 at 15:07
  • 1
    It is a duplicate and it's probably because `test` is `null` so calling `test.TreatmentTypes(i)` would throw an NRE. – Igor Jun 23 '17 at 15:11
  • If you follow the top answer outlined in the marked duplicate you will be able to better trouble shoot what variable is `null`. – Igor Jun 23 '17 at 15:13
  • 1
    Conceptually, it is a duplicate. There is only one cause for a NRE, but this seems to be an edge case. `test` is not initialized anywhere. I think you are way off base in what you are doing. If some value can be a combination of treatments, it needs to be a flag. Why you are doing any of this when you can get the Enum values ot names very easily is a mystery – Ňɏssa Pøngjǣrdenlarp Jun 23 '17 at 15:17
  • Thiis is the line of code I used on my previous values where Enum was not a list - ([Enum].Parse(GetType(Bloodtype), getData.code) and It worked perfectly however the fact DiseaseTreatment() its causing issues so I am trying to find a way to solve it – Sam Jun 23 '17 at 15:21
  • 1
    Yea, an Enum is never a list. – Ňɏssa Pøngjǣrdenlarp Jun 23 '17 at 15:29

1 Answers1

4

You want to stop at the end of the array, not at the end of the x item in the array.

For i As Integer = 0 To temptreatment.Count - 1

Not

For i As Integer = 0 To temptreatment(i) - 1
Igor
  • 60,821
  • 10
  • 100
  • 175
  • "length is not a member of type list" I think I am doing something completely wrong. I've never used list and new to vb.net. I will try to read more about lists and how to go about it with Enums. the other enums where straightforward hopefully this one too! Cheers – Sam Jun 23 '17 at 15:38
  • @Sam - sorry, that should have been `Count` not `Length`. The latter is the property on an array. – Igor Jun 23 '17 at 15:38
  • Yeah, that's right but I still have error. Can you recommend a link to learn about the enums which have list type ( the only different with this one from the previous ones I did with enums, you can have multiple treatments) If you could direct me to a good place to start reading about I will very much appreciate it. – Sam Jun 23 '17 at 15:45
  • @Sam - Most of the content out there is for c# but I think its not too complex to easily translate to vb.net. What you are searching for is called [Enum Flags](https://msdn.microsoft.com/en-us/library/system.flagsattribute%28v=vs.110%29.aspx). This offers the ability to combine multiple states into a single value. Keyword browser search: [.net enum flags](https://www.google.com/search?q=.net+enum+flags&oq=.net+enum+flags) – Igor Jun 23 '17 at 15:48
  • Thanks! but my Enum class, I am not meant to change it and they didnt use flag.. I've looked at it quick but thanks very much for giving me some reading material for this problem I have. – Sam Jun 23 '17 at 15:53