Microsoft Visual Studio in C#: I have this code and need to loop through each element of the patient's array and add to a listbox (lbAccountDisplay) each property of the patient's object. This is the 2nd time to ask this question; it should have all the detail you need to help me. It is a piece of a project and I am trying not to give you the whole project. How do I use a foreach loop to loop through each element of the patient's array and add to a listbox (lbAccountDisplay) each property of the patient's object??
//Patients displayed in an array and displayed to
AccountDisplay
//Initialize Array of Patients
const int SIZE = 4;
patients = new Patient_Class[SIZE];
patients[0] = new Patient_Class("Ava Hal", 0);
patients[1] = new Patient_Class("Jen Jacobson", 0);
patients[2] = new Patient_Class("Falicia Singh", 0);
patients[3] = new Patient_Class("All Williams", 0);
NOTE :HERE Patient_Class IS CREATED FOR UNDERSTANDING PURPOSE ONLY
Application name :PatientsApplication
Language used :C#
Form1.cs :
//namespace
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//application namespace
namespace PatientsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//display button click
private void buttonAdd_Click(object sender, EventArgs e)
{
const int SIZE = 4;
//creating array of Patients
Patient_Class []patients = new Patient_Class[SIZE];
//Initialize Array of Patients
patients[0] = new Patient_Class("Ava Hal", 0);
patients[1] = new Patient_Class("Jen Jacobson", 0);
patients[2] = new Patient_Class("Falicia Singh", 0);
patients[3] = new Patient_Class("All Williams", 0);
//using for each loop
foreach (Patient_Class patient in patients)
{
//display patient name and number in the listbox
lbAccountDisplay.Items.Add(patient.patientName+"
"+patient.patientNumber);
}
}
}
class Patient_Class //C# class
{
public string patientName;
public int patientNumber;
//parameterized constructor
public Patient_Class(String name,int number)
{
this.patientName = name;
this.patientNumber = number;
}
}
}
=========================================='
Output :
Get Answers For Free
Most questions answered within 1 hours.