Solved

# Two Dimensional Array

Posted on 2005-04-30
189 Views
I am having difficulty wrapping my mind arround 2D arrays.  I need to set one up based on user input.  Example:  User gives me blood pressure reading and Cholesterol reading in 2 numeric updown boxes and I return an assigned rating to the user from looking up the value in an array when they hit the submit btn.

I have several questions... so please bear with me as I learn.
The real table is much bigger, but for example sake... let use a 3x3:
200     250
120/80     1.5      2.0
130/90     2.5      3.0

Q1: Do I include the Column and row headings in the array set up?
Dim strArray(3,3) as String
StrArray(0,1) = "200"
StrArray(0,2) = "250"
StrArray(1,0) = "120/80"
StrArray(1,1) ="1.5"
StrArray(1,2) = "2.0"
StrArray(2,0) = "130/90"
StrArray(2,1) = "2.5"
StrArray(2,2) = "3.0"

Q2: When I have the values from the user, how do I use that to look up the rating?
strRating=strArray(bp,cholest)  /I'm guessing here.  And i'm guessing I would need to declare and set these two variables up prior to using them....  lol

I have been quasi-successful modifying an array in the past. But have never had to set one up from scratch.
And, I feel so lost.   I'm trying to meet a deadline, that's why the points have been assisgned at 500.  As always, I appreciate any and all help!!   Many Thanks!  Michelle

0
Question by:mojeaux65

LVL 23

Expert Comment

Dim values(2,2) As String

1.In VB.NET when you create an array like value(3), it means for value(0 To 3)  which means 4 itemsin the array.
2. Thus values(2,2) means 3 rows by 3 columns
3. You'd have to populate the array accordingly.

'row/column

values(0,0) = string.Empty
values(0,1) = "200"
values(0,2) = "250"
values(1,0) = "120/80"
values(1,1) = "1.5"
values(1,2) = "3.0"
values(2,0) = "130/90"
values(2,1) = "2.5"
values(2,2) = "3.0"

0

LVL 23

Expert Comment

I realise you have a tight deadline and that you are new to this all. But you could seriously consider using DataTables to store your data accordingly and output them accordingly.

0

Author Comment

Your right about the Dimension (2,2)... I forget about the zero rule.  How do I use the array to look up the corresponding rating?  Do I pass the bp and cholesterol reading through a function and retreive the rating with a select case? Thx!

0

Author Comment

Hard to believe, but I know nothing about DataTables, their properties, or how they work.  lol.  Sorry.
0

LVL 23

Expert Comment

Private Function GetRating(ByVal bloodPressure As String, ByVal cholesterol As String) As String
Dim value As String = String.Empty
Dim count As Integer
For count = 0 To UpperBound(values,0)
If values(count,0) = bloodPressure Then
Select Case cholesterol
Case "200"
value = values(count,1)
Case "250"
value = values(count,2)
End Select
Exit For
End If
Next
Return value
End Function

0

LVL 23

Accepted Solution

amendment
=======
For count = 0 To UBound(values)

Next

Private Function GetRating(ByVal bloodPressure As String, ByVal cholesterol As String) As String
Dim value As String = String.Empty
Dim count As Integer
For count = 0 To UBound(values)
If values(count,0) = bloodPressure Then
Select Case cholesterol
Case "200"
value = values(count,1)
Case "250"
value = values(count,2)
End Select
Exit For
End If
Next
Return value
End Function
0

Author Comment

Thank you for the solution.  If you have a sec, I have a couple questions about the 'why' behind the 'how'.
Q1: Is Ubound an intrinsic function? And does it represent the max # of cells/elements in the array?
Q2: Does the declaration statement:' Dim value as string = string.empty' declare the array as empty or just the value element as empty?

I never would have known to use a counter variable for the look up.  But it's all starting to make more sense now.  Thx again for your help!  Michelle
0

Author Comment

Oh! one more question.... I've heard of ByVal and ByRef, but not seen them used in an example.  Does this return the value of the cell/element instead of its location?
0

LVL 23

Expert Comment

1. UBound is a VB function that got carried over to VB.NET
It represents the maximum item number of the particular rank.
You see with multi-dimensional arrays like String(2,2,3)
Dim values(2,4,6) As String,
UBound(values) would yield 2 and is the same as UBound(values,1)
UBound(values,2) would yield 4
UBound(values,3) would yield 6

String.Empty is a shared (VB.NET) / static (C#) member which essentially is a zero-lengthed string.
You can set variables in VB.NET straight off the bat like so:
Dim connection As New SqlConnection()

0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!