0

Snippet From JSON

 "stats": [
    {
      "stat": 32,
      "amount": 651,
      "reforgedAmount": -434
    },
    {
      "stat": 5,
      "amount": 2001
    },
    {
      "stat": 36,
      "amount": 1544
    },
    {
      "stat": 7,
      "amount": 3362
    },
    {
      "stat": 49,
      "amount": 434,
      "reforged": true
    }
  ],
  "armor": 2244
},

my current code

stat0lbl.Text = If(jResults("items")(itemtypelbl.Text) Is Nothing, "", jResults("items")(itemtypelbl.Text)("stats")(0)("stat").ToString()) + Environment.NewLine + If(jResults("items")(itemtypelbl.Text) Is Nothing, "", jResults("items")(itemtypelbl.Text)("stats")(1)("stat").ToString()) + Environment.NewLine + If(jResults("items")(itemtypelbl.Text) Is Nothing, "", jResults("items")(itemtypelbl.Text)("stats")(2)("stat").ToString()) + Environment.NewLine + If(jResults("items")(itemtypelbl.Text) Is Nothing, "", jResults("items")(itemtypelbl.Text)("stats")(3)("stat").ToString()) + Environment.NewLine + If(jResults("items")(itemtypelbl.Text) Is Nothing, "", jResults("items")(itemtypelbl.Text)("stats")(4)("stat").ToString()) + Environment.NewLine + If(jResults("items")(itemtypelbl.Text)("stats")(5) Is Nothing, "", jResults("items")(itemtypelbl.Text)("stats")(5)("stat").ToString())

this code works perfectly until it comes to stat 5 in the array, im not sure how to handle the Is Nothing in an array

ids
  • 33
  • 1
  • 9

1 Answers1

0

I think pushing to do this logic in single line doesn't bring any advantage but far less readable code (and redundant checking jResults("items")(itemtypelbl.Text) Is Nothing). I'd suggest to do it in standard If block (if possible) :

If jResults("items")(itemtypelbl.Text) Is Not Nothing Then
    Dim newStat = _
        jResults("items")(itemtypelbl.Text)("stats")(0)("stat").ToString() & Environment.NewLine & _
        jResults("items")(itemtypelbl.Text)("stats")(1)("stat").ToString() & Environment.NewLine & _
        jResults("items")(itemtypelbl.Text)("stats")(2)("stat").ToString() & Environment.NewLine & _
        jResults("items")(itemtypelbl.Text)("stats")(3)("stat").ToString() & Environment.NewLine & _
        jResults("items")(itemtypelbl.Text)("stats")(4)("stat").ToString() & Environment.NewLine

    If jResults("items")(itemtypelbl.Text)("stats").Count() > 5 Then
        newStat &= jResults("items")(itemtypelbl.Text)("stats")(5)("stat").ToString()
    End If

    stat0lbl.Text = newStat
End If

I assume that the main confusion is on how to check if stat at index 5 exists. One possible way, as demonstrated above is, using .Count() to check how many data (stat) in collection (stats). If .Count() return more than 5, means there is data at index 5.

har07
  • 83,990
  • 12
  • 70
  • 116
  • when i use the count function i seem to be getting an exception `{"Object reference not set to an instance of an object."}` – ids Mar 14 '14 at 19:34
  • it is my (itemtypelbl.Text) that is causing the issue, is it possible to define a value using the text from itemtypelbl and use it in the string? – ids Mar 15 '14 at 00:39
  • yes, it is possible. if the issue was null reference as in your 1st comment, first step is to find out which variable is null. [for reference on how to avoid NullReferenceException](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – har07 Mar 15 '14 at 01:43
  • i already know itemtypelbl.Text was causing the null to return what im not sure how to do is `Dim stat0 As String stat0 = stat0lbl.text` then use that in a way like `If jResults("items")(stat0)("stats").Count() > 5 Then .....` i have tried it this way and it still throws the exception without physically typing it like this `If jResults("items")("head")("stats").Count() > 5 Then ...` when i format it like that it seems to work fine but i dont want to type it for every item which is why i was using `itemtypelbl.text` as this is set by the parent form for each item – ids Mar 15 '14 at 12:17
  • Which one was null, Text property of `stat0lbl` or `stat0lbl` it self? how about adding null checking? if it is null don't proceed with reading `jResults` – har07 Mar 15 '14 at 13:28
  • im not sure i understand when i use the `jResults("items")("head")("stats")(0)("stat")` on its own WITHOUT count it works fine so nothing is null i just cant use .count like `If jResults("items")(itemtypelbl.Text)("stats").Count() > 5 Then` so when i ad count its changing how it is being read some how and im unable to determine what and unable to see a null you refer to – ids Mar 15 '14 at 14:18
  • that (the 1st snippet) was WITHOUT count and WITHOUT itemtypelbl.Text. so there are 2 suspects, you need to find out which one causing the problem. Try this way and see if exception still thrown : `jResults("items")("head")("stats").Count() > 5` – har07 Mar 15 '14 at 14:41
  • i think we may be getting confused here, in an earlier post i have said `If jResults("items")("head")("stats").Count() > 5 Then` -- Works Fine But Not Ideal | `mylbl.text = jResults("items")(itemtypelbl.Text)("stats")(0)("stat")` - Works Fine | But `If jResults("items")(itemtypelbl.Text)("stats").Count() > 5 Then` Throws An Exception so it has somthing to do with the either .count or using the label text and count together :) – ids Mar 15 '14 at 16:07