1

I am using the module to declare the public class patient across all forms of my program. What do I have to do in order to input data into the array from three text boxes. One for Names, Heights, and Weights. Thanks

Public Module Module1
Public PatientCount As Integer = 0

Public Class Patient

    Public Property Name As String = String.Empty

    Public Property Height As Decimal = 0

    Public Property Weight As Decimal = 0

End Class
End Module


Dim patients As List(Of Patient) = New List(Of Patient)

For displaying patients in listbox

    For Each p As Module1.Patient In Patients
        lstPatients.Items.Add(p.Name)
    Next

CURRENT CODE: MODULE

Public Module Module1
Public PatientCount As Integer = 0
Public Patients As List(Of Patient) = New List(Of Patient)

Public Class Patient

    Public Property Name As String

    Public Property Height As Decimal

    Public Property Weight As Decimal

    Public Sub New(ByVal name As String, ByVal height As Decimal, ByVal weight As Decimal)
        name = _Name
        weight = _Weight
        height = _Height
    End Sub
End Class
End Module

FORM 2 (Entry of Data)

Public Class Form2


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
    Me.Close()
End Sub

Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click

    patients.Add(New Patient(txtName.Text, CDec(txtHeight.Text), CDec(txtWeight.Text)))

    PatientCount = PatientCount + 1
    Label1.Text = PatientCount

End Sub
End Class

FORM 3 (Listing of Data) Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    For Each p As Patient In Patients
        lstPatients.Items.Add("Name: " & p.Name)
        lstPatients.Items.Add("Weight: " & p.Weight)
        lstPatients.Items.Add("Height: " & p.Height)
        lstPatients.Items.Add("___________")
    Next

End Sub
End Class
4
  • 1
    I wouldn't store the textboxes on an array, how is the index of an array related to a name or height or weight at all? You could use a UserControl which includes all three TextBoxes. Then you can create one UC for every Patient in the list. If you provide meaningful (public) properties like Name there your code becomes much more redable and maintainable. This Name property would simply get or set the appropriate TextBox.Text. Commented Jan 28, 2014 at 23:11
  • It is for a school project, I need to allow the user to enter patient data through a textbox and later display it. So how do I go about storing the user entered data from the textbox into the array so I can display it. Commented Jan 28, 2014 at 23:16
  • create a Patient object - you have the class, store the form data into it, then add the new Patient to the List(Of Patient). The Pateient properties are going to work just like regular GUI ones Commented Jan 28, 2014 at 23:18
  • how do I actually add new Patients to the List(Of Patient)? In the click event handler of the enter patient button what code would go there to enter that data into the patient object Commented Jan 28, 2014 at 23:41

3 Answers 3

1

To store the data of an x number of patients, you could have this code in a button-clicked event handler.

Private Sub ButtonOK_Click()Handles BtnOk.Click()
    patients.Add(New Module1.Patient(txtName.Text, CDec(txtHeight.Text), CDec(txtWeight.text))
End Sub

And then add this to your Patient class:

Public Module Module1

Public Class Patient

    Public Property Name As String = String.Empty

    Public Property Height As Decimal = 0

    Public Property Weight As Decimal = 0

    Public Sub New(_name as String, _height as decimal, _weight as decimal)
        Name = _name
        Weight = _weight
        Height = _height
    End Sub
End Class
End Module


Dim patients As List(Of Patient) = New List(Of Patient)

With this, you will create a list of patients every time the btnOk is pressed, and you can easily access any patient and their data.

To put the data in a listbox:

Private Sub BTNLIST_Click()Handles BTNLIST.Click
    For Each p As Patient in patients
        lstbox.Items.Add("Name: " & p.Name)
        lstbox.Items.Add("Weight: " & p.Weight)
        lstbox.Items.Add("Height: " & p.Height)
        lstbox.Items.Add("___________")
    Next
End Sub

OPTIONAL

You could even create a PatientList Class to help you further (but remove the patients List). You could use the PatientList class to add helpful functions, such as finding a patient by name or height.:

Public Class PatientList

    Public List(Of Patient) Patients = New List(Of Patient)

    Public ReadOnly Property PatientCount() As Integer
        Get
            Return Patients.Count
        End Get
    End Property

    Public Sub AddPatientToList(name as String, height as decimal, weight as decimal)
        Patients.Add(New Patient(name, height, weight)
    End Sub
End Class

Dim patients as PatientList

Then add this in your button_click Handler:

Private Sub ButtonOK_Click()Handles BtnOk.Click()
        patients.AddPatientToList(New Module1.Patient(txtName.Text, CDec(txtHeight.Text), CDec(txtWeight.text))
    End Sub

HTH

Sign up to request clarification or add additional context in comments.

25 Comments

do you know why I could be receiving the "is not part of System.Array" error using the second answer?
Idk...it might have to do with the parenthesis...have you tried the other answers? Personally, I think having a New() subfunction, as suggested by me and tinstaafl, is much more effective because you only need one line of code to initialize the Patient object, as compared to 3 or more. (And there's much less clutter if you develop a big program)
Ok thankyou, I'll try your answer. Gandy's just looked the most simple
Ok, I understand. All I really added was the Initializer for the Patient object (Public Sub New()), and the code for the Button_Clicked Event. The bottom portion was just a suggestion (and me getting carried away thinking about designing a big interface :)), so you could just ignore that.
Personally, in the long run, I think mine or @tinstaafl 's would be better. You're initializing a class, and adding properties to it via the initialization. So if you create a big application that has a class with many properties (like I did when I created an animation class for a simulation software for a major corporation), you can initialize everything with the New() subfunction of the object, and won't have to write 10 lines just to set all the properties individually.
|
1

One option would be to have a New constructor that takes the 3 values for the properties.

Public Module Module1
Public PatientCount As Integer = 0

Public Class Patient

    Public Property Name As String = String.Empty

    Public Property Height As Decimal = 0

    Public Property Weight As Decimal = 0

    Public Sub New(_name As String, _height As Decimal, _weight As Decimal)
        Name = _name
        Height = _height
        Weight = _weight
    End Sub

End Class
End Module


Dim patients As List(Of Patient) = New List(Of Patient)

Assuming you've validated the input from the textboxes and converted them to variables(NewName, NewHeight, NewWeight), you would add a new patient something like this:

patients.Add(New Patient(NewName,NewHeight,NewWeight))

1 Comment

thanks for the answer I'll try it along with the other answers
0

If you want to add a patient then you need to create an instance of one and set it properties with the textboxes of whatever form your using. Then add the new patient to the list of patients, so something like this.

Dim patient as new Patient()
patient.Name = txtName.Text()
patient.Height = txtHeight.Text()
patient.Weight = txtWeight.Text()

patients.Add(patient)

1 Comment

using your code returns the error Name, Height, Weight, is not a member of System.Array? Thanks

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.