0

What I'm trying to do with this is save a list of reference keys for everything that is populating a listbox. There will be an unknown quantity of lines(users/"patients"). After saving the list, I want to be able to use the listbox index to find the corresponding key, and use that to move on to the next part.

public partial class PatientList : Window
{
    HumanResources ListAllPatients;
    List<PatientInformation> AllPatients;
    string[] Usernames;

    public PatientList()
    {
        InitializeComponent();
        int i = 0;
        string[] Usernames = new string[i];
        ListAllPatients = new HumanResources();
        AllPatients = ListAllPatients.PopPatientList();

        foreach (PatientInformation PatientChoice in AllPatients)
        {
            Usernames[i] = PatientChoice.Username;
            lboxPatients.Items.Add(string.Format("{0} {1}; {2}", PatientChoice.FirstName, PatientChoice.LastName, PatientChoice.Gender));
            i += 1;
        }
    }

Here is the code for the button that moves you on to the next section

public void btnSelectPatient_Click(object sender, RoutedEventArgs e)
    {
        PatientInfo PatientInfoWindow = new PatientInfo();
        PatientInfoWindow.Show();
        //i = lboxPatients.SelectedIndex;
        //UserFactory.CurrentUser = Usernames2[i];
        this.Close();
    }

My issue is this: I believe I've initialized a string array to do this, but VS keeps telling me it isn't assigned to, and will always remain null.

My question: How and where do I properly initialize a string array (or something better) in order to accomplish sending the key from PatientList() to btnSelectPatient?

Erick
  • 5
  • 2

2 Answers2

0

You are not initializing the string[] field but a local variable in the constructor here:

string[] Usernames;

public PatientList()
{
    InitializeComponent();
    int i = 0;
    string[] Usernames = new string[i];

You have to assign this array to the field:

public PatientList()
{
    InitializeComponent();
    int i = 0;
    this.Usernames = new string[i];

But that doesn't make much sense currently because it's length is always 0.

Maybe you want this instead:

public PatientList()
{
    InitializeComponent();
    ListAllPatients = new HumanResources();
    AllPatients = ListAllPatients.PopPatientList();
    this.Usernames = new string[AllPatients.Count];

    int i = 0;
    foreach (PatientInformation PatientChoice in AllPatients)
    {
        Usernames[i] = PatientChoice.Username;
        lboxPatients.Items.Add(string.Format("{0} {1}; {2}", PatientChoice.FirstName, PatientChoice.LastName, PatientChoice.Gender));
        i += 1;
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You have a few mistakes:

  • Your initializing a new local variable instead of the class-level variable because you declare it again (string[] userNames = ...
  • Your initializing an array of length 0. This will fail when you try to add items to position 1 and above.

My advice would be to use a List, as it's better suited for dynamic length lists and you don't need to keep track of the index yourself, it will automatically be added at the right index:

HumanResources ListAllPatients;
List<PatientInformation> AllPatients;
List<string> Usernames = new List<string>();

public PatientList()
{
    InitializeComponent();
    ListAllPatients = new HumanResources();
    AllPatients = ListAllPatients.PopPatientList();

    foreach (PatientInformation PatientChoice in AllPatients)
    {
        Usernames.Add(PatientChoice.Username);
        lboxPatients.Items.Add(string.Format("{0} {1}; {2}", PatientChoice.FirstName, PatientChoice.LastName, PatientChoice.Gender));
    }
}
Kenneth
  • 28,294
  • 6
  • 61
  • 84