# Two Dimensional Array

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

###### Who is Participating?

Commented:
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

Commented:
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

Commented:
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 Commented:
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 Commented:
Hard to believe, but I know nothing about DataTables, their properties, or how they work.  lol.  Sorry.
0

Commented:
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

Author Commented:
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 Commented:
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

Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.