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'

Irwene
  • 2,807
  • 23
  • 48
  • I doubt this fixes your problem but you should declare your class members to be public or private or something else. – Jacobr365 May 26 '16 at 15:55
  • I try it like this, private void metroButtonAdicionar_Click(object sender, EventArgs e, List 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()); } } – Chelmy Penhor May 26 '16 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. – Jeff Puckett May 26 '16 at 15:57
  • @Jacobr365 Don't forget class fields are `private` unless specified othrwise. Classes are `internal` unless specified otherwise – Irwene May 26 '16 at 15:58
  • @JeffPuckettII this is done during the object initialisation `List todos = new List();` – Irwene May 26 '16 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 – Irwene May 26 '16 at 15:59
  • Yes, that´s my mistake. But i don´t see where I have to assigne it. Can you help me? – Chelmy Penhor May 26 '16 at 16:00
  • Just have `List todos` as your field declaration. In your constructor, before the call to `InitializeComponent` assign the constructor parameter to your field `this.todos = todos` (you'll have to use this to access your field since your constructor parameter and the field have the exact same name). Won't fix your error though – Irwene May 26 '16 at 16:02
  • mybe you like to do sth. like `todos = todos`within your constructor – whymatter May 26 '16 at 16:02
  • Inside constructor `this.todos = todos` and wherever you are using it use it as `this.todos` instead of just `todos`. – Venkata Dorisala May 26 '16 at 16:04
  • I try do like you said, and nothing. :( – Chelmy Penhor May 26 '16 at 16:06
  • Are you sure you're showing all the relevant code? I think what you've shown would compile without giving that error. – hatchet - done with SOverflow May 26 '16 at 16:14
  • I think I solve the problem, no error for now. – Chelmy Penhor May 26 '16 at 16:19

1 Answers1

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

Community
  • 1
  • 1
Deepak
  • 126
  • 1
  • 11
  • 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 – Irwene May 26 '16 at 16:13
  • Thanks a lot every body. – Chelmy Penhor May 26 '16 at 16:14
  • How can I put the entire project here? I should that´s better. – Chelmy Penhor May 26 '16 at 16:21
  • Putting the entire project here would not be a good idea @ChelmyPenhor. Instead, focus your efforts on creating a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). 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. – Jeff B May 26 '16 at 16:56