Solved

Handling Arrays

Posted on 2009-06-30
2
172 Views
Last Modified: 2012-05-07
I am having an issue with an array (actually two).  I am parsing a string from a database to display it on a website.  The string is like value;position,value;position

My code first splits on the comma which give me value;position in one array.  I then split that into another array on the semicolon.  This works great most of the time.  However, some instances the "position" in the string will be missing from the data so I get an out of index error.  I've tried to look for floor.length > 0 or see if this is null but that doesn't help.  below is the code snippet
Sample data: MasterBedroom;1,Den;,GreatRoom;,Bedroom;1,Bedroom;1,Bedroom;B,FullBath;B,FullBath;1,FullBath;1,DiningRoom;
in this example, MaterBedroom is the value, 1 is the position.  Notice that Den is the next value, but is missing position.  Desire is to put "Main Floor" where no position is provided.
 
Code:
 If Not objRdr("rooms") Is DBNull.Value Then
                        lblDiningAreas.Text = "<table>"
                        Dim rooms() As String
                        Dim floor() As String
                        rooms = objRdr("rooms").Split(",")
                        For j As Integer = 0 To rooms.Length - 1
                            lblDiningAreas.Text &= "<tr><td class='content'><b>"
                            floor = rooms(j).Split(";")
                            lblDiningAreas.Text &= floor(0) & "</b></td><td class='content'>&nbsp;&nbsp;&nbsp;"
                            If floor.Length > 0 Then
                                Select Case floor(1)
                                    Case "1"
                                        lblDiningAreas.Text &= "Main Level" & "</td></tr>"
                                    Case "2"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "3"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "4"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "5"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "6"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "7"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "8"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "9"
                                        lblDiningAreas.Text &= "Upper Level" & "</td></tr>"
                                    Case "B"
                                        lblDiningAreas.Text &= "Basement" & "</td></tr>"
                                End Select
                            Else
                                lblDiningAreas.Text &= "Main Level" & "</td></tr>"
                            End If
 
Out put looks like:
MasterBedroom      Main Level
etc..                        etc...

Open in new window

0
Comment
Question by:azyet24
2 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 250 total points
ID: 24746779
You should do the vaidation first and then the lblDiningAreas.Text &= floor(0) & "</b></td><td class='content'>&nbsp;&nbsp;&nbsp;"

Like:

 If floor.Length > 0 Then
    lblDiningAreas.Text &= floor(0) & "</b></td><td class='content'>&nbsp;&nbsp;&nbsp;"
    ...

Open in new window

0
 
LVL 4

Assisted Solution

by:vbgb
vbgb earned 250 total points
ID: 24747140
Although splitting a string like "Den;" with split(";") should always give an array length of 2 ("Den" and an Empty String), your code at 'If floor.length>0 is only testing if "Den" exists, not the second position element.

I assume that the Out of Index error occurs in the case statement so you need to check that  'floor.length>1', rather than 'floor.length>0'
- or you could check that the upper bound of the array is greater than 0 (If floor.getupperbound(0)>0)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with exporting to excel 4 35
MailAddress in vb 4 29
VB.NET String Settings and Temp Folder Question 3 51
Groupbox Control ? 2 17
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question