0

I was wondering how you create an array with multiple arrays inside as I already have the created arrays which you can see bellow in the code. But I want to put those array in side of an array called "Patients". while more so all the index of 1 in the following array Name, age, Etc can be seen under index 1 of Patients. Thank you

Public Class DataEntry

Dim Patients()

Dim Surname(200)
Dim Firstname(200)
Dim Age(200) As String
Dim HeightA(200) As String
Dim Weight(200)
6
  • 1
    You should really just make a Patient class, and have an arraylist of those. You can't really have a multidimensional array built with each of those you've already created, because it wouldn't really make sense to store 200 ages for one patient, would it? As it stands, you say you want Patient Index 1 to match across all arrays, well technically what you have written already achieves that. It's messy and inefficient, but you would just set each array(1) to patient-1's data, each array(2) to patient-2, etc...strongly consider using a list of class objects though, that's the right way to do it. Commented Dec 4, 2017 at 22:28
  • In addition to soohoonigan's comment: en.m.wikibooks.org/wiki/Visual_Basic_.NET/Classes Commented Dec 4, 2017 at 22:31
  • @soohoonigan : "You can't really have a multidimensional array built with each of those" - There's nested (aka jagged) arrays. ;) Commented Dec 4, 2017 at 22:33
  • @VisualVincent I meant it doesn't make sense to have a multidimensional array built with the Age(200), Height(200) etc that he's already created, because no person has 200 ages and 200 firstnames etc :-P Commented Dec 4, 2017 at 22:37
  • 1
    I should also mention that creating an array like that specifies the upper bound, i.e. the index of the last element, rather than the length. As .NET arrays are zero-based, i.e. the first element is at index 0, the code posted will create arrays with 201 elements. Commented Dec 5, 2017 at 0:12

2 Answers 2

2

You'd be best making a patient class similar to the following

Public Class Patient
    Private _surName As String
    Private _firstName As String
    Private _age As Integer

    Property SurName() As String
        Get
            Return _surName
        End Get
        Set(ByVal Value As String)
            _surName = Value
        End Set
    End Property

    Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal Value As String)
            _firstName = Value
        End Set
    End Property

    Property Age() As String
        Get
            Return _age
        End Get
        Set(ByVal Value As Integer)
            If Value >= 0 Then
                _age = Value
            End If
        End Set
    End Property
End Class

Then creating instances of that class

Dim p As Patient = New Patient() 
p.FirstName = "john"
p.LastName = "Smith"
p.Age = 50

Dim p1 As Patient = New Patient() 
p.FirstName = "james"
p.LastName = "bond"
p.Age = 47

'etc

and then adding the patients to a list or array:

Dim patients As List(Of Patient) = New List(Of Patient)
patients.Add(p)
patients.Add(p1)
Sign up to request clarification or add additional context in comments.

7 Comments

the part in the code that has Set(ByVal Value As String) what is "value" used for SEarle1986.
so this is what I have done in the code below. how could I do it so that I can have an infinite amount dim p as patient so I don't have to manually create it .
@jdavies : Create an array, like you're used to? Dim p(200) As Patient -- Note that you must initialize each item before using it, i.e. p(0) = New Patient p(1) = New Patient and so on...
@SEarle1986 : Your Age property is incorrect. Changing it changes the value of _firstName rather than _age.
@VisualVincent oops - a late night typo, well spotted
|
0
    BIM = FormatNumber(Val(txtWeight.Text) / Val((txtHeight.Text / 1000) ^ 2))

    If BIM < 18.5 Then
        Health = "Underweigth"
    ElseIf BIM > 18.5 And BIM < 24.9 Then
        Health = "Healthy Weight"
    ElseIf BIM > 24.9 And BIM < 29.9 Then
        Health = "Overweight"
    ElseIf BIM > 29.9 And BIM < 39.9 Then
        BIM = "Obese"
    ElseIf BIM > 40 Then
        Health = "Morbidly Obese"
    End If

    Dim p As Patient = New Patient()
    p.FirstName = txtFirst.Text
    p.SurName = txtSur.Text
    p.Age = txtAge.Text
    p.Height = txtHeight.Text
    p.Weight = txtWeight.Text

    Dim textAppend As String

    textAppend = "  " & p.SurName & ", " + p.FirstName & ", " + "Age: " & p.Age & ",  " + "Height: " & p.Height & "mm" & ",  " + "Weight: " & p.Weight & "kg" & "  ," + BIM & " = " & Health & "."

    Try
        File.AppendAllText(filepath, textAppend)
        MsgBox("Patient Added Successfully")
    Catch ex As Exception
        MsgBox("Error Adding Patient")
    End Try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.