0

I want to create a new user and save it in my list. Im new in C#, and I don´t know how I´m going to solve this. I´m using list.

using MetroFramework.Forms;
    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;


        namespace SGE
        {
            public partial class Registar_Utilizador : MetroForm
            {
                string username, password, tipo;
                List<Pessoa> todos = new List<Pessoa>();
                List<Pessoa> novaListaPessoa = new List<Pessoa>();

                public Registar_Utilizador(List<Pessoa> todos)
                {
                    InitializeComponent();
                }

                private void metroButtonAdicionar_Click(object sender, EventArgs e)
                {
                    int i = 0;
                    Pessoa p = new Pessoa();
                    do
                    {
                        p.setusername(metroTextBoxUsername.Text);
                    }while(i < todos.Count && p.getusername() == todos[i++].getusername());

                }
            }
        }

[Error] Error 1 Use of unassigned local variable 'todos'

13
  • I doubt this fixes your problem but you should declare your class members to be public or private or something else. Commented May 26, 2016 at 15:55
  • I try it like this, private void metroButtonAdicionar_Click(object sender, EventArgs e, List<Pessoa> todos) // the edit line { int i = 0; Pessoa p = new Pessoa(); do { p.setusername(metroTextBoxUsername.Text); }while(i < todos.Count && p.getusername() == todos[i++].getusername()); } } Commented May 26, 2016 at 15:56
  • you're trying to use while(i < todos.Count but I don't see any apparent method to actually populate todos so that's why you get the error because you haven't assigned anything to it yet you're trying to get a count from it. Commented May 26, 2016 at 15:57
  • @Jacobr365 Don't forget class fields are private unless specified othrwise. Classes are internal unless specified otherwise Commented May 26, 2016 at 15:58
  • 3
    What surprises me is that the constructor takes a list or Pessoa as a parameter and don't use it after that Commented May 26, 2016 at 15:59

1 Answer 1

1

In your Registar_Utilizador, you don't need to pass the list. You are doing nothing with it that I see.

public Registar_Utilizador(List<Pessoa> todos)
            {
                InitializeComponent();
            }

Now for this method: you will change it to below - I have shown using foreach:

 private void metroButtonAdicionar_Click(object sender, EventArgs e)
 {        
    foreach(Pessoa p in todos)
    {
        if (someCondition) //p.getusername() == todos[i++].getusername()
        {
          p.setusername(metroTextBoxUsername.Text);
          novaListaPessoa.Add(p)
        }
    }   
}

if you want to create a new instance of objects in todos, then you can use information from here to copy one object to another copy one object to another

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

4 Comments

I'd be careful about that constructor parameter, we don't know what was in the original list that was passed to it. It might just be that the OP forgot to assign the constructor's parameter to his class's field
Thanks a lot every body.
How can I put the entire project here? I should that´s better.
Putting the entire project here would not be a good idea @ChelmyPenhor. Instead, focus your efforts on creating a Minimal, Complete, and Verifiable example. Often, you'll find the answer to your question just by going through that process. Or at least get a better understanding of what question you need to be asking and locate an existing answer.

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.