2013-03-25 21 views
3

我是C#的新手,並且我正在嘗試編寫一段代碼。C#在一個字典中使用類作爲對象

我不得不做一個簡單的程序,存儲一些信息的學生和以後,讓我得到信息打印。

我的想法是這樣的:我創建了一個類,命名爲「Aluno」,並實施了一系列的方法來設置和獲得我所需要的信息,然後我通過這個類作爲參數,以這樣的字典的東西:

class Aluno 
    { 
     private int matricula; 
     private string nomeCompleto; 

     public int getMatricula() { return matricula; } 
     public string getNomeCompleto() { return nomeCompleto; 

     public void setMatricula(int m) 
     { 
      matricula = m; 
     } 
     public void setNomeCompleto(string nome) 
     { 
      nomeCompleto = nome; 
     } 

然後我把它用在一個dictionaty

 Dictionary<int,object> principal = new Dictionary<int,object>(); 
     Aluno a = new Aluno(); 

     Console.WriteLine("Por favor informe a matricula:"); 
     a.setMatricula(int.Parse(Console.ReadLine())); 
     Console.WriteLine("Por favor informe o nome completo do aluno:"); 
     a.setNomeCompleto(Console.ReadLine().ToUpper()); 

     principal.Add(a.getMatricula(), a); 

在此之後,我實現了一個類打印存儲在字典中的信息:

class ImprimeListagem 
    { 
     public static void listagemSimples(Dictionary<int,object> origem) 
     { 
      int contador = 0; 

      foreach (KeyValuePair<int, object> chave in origem) 
      { 
       Console.WriteLine(chave.Key+"|"+chave.Value); 

       Console.ReadKey(); 
      } 

     } 

當我使用Visual Studio來查看chave.value時,所有存儲的信息都在那裏,但我不能訪問它。有人能幫我嗎? 我會打印完整的代碼。

class Program 
{ 
    static void Main(string[] args) 
    { 
     const int QTDE_ALUNO_PRINCIPAL = 1; 
     const int QTDE_ALUNO_RESERVA = 1;    
     int contadorAlunosPrincial = 0; 
     int contadorAlunoReserva = 0; 


     Dictionary<int,object> principal = new Dictionary<int,object>(); 
     Queue<object> espera = new Queue<object>(); 

     Aluno a = new Aluno(); 

     int opcao = 0; 



     do 
     { 
      ImprimeMenu.imprimirMenuPrincipal(); 
      opcao = int.Parse(Console.ReadLine()); 

      switch (opcao) 
      { 
       case 1: 
        if (contadorAlunosPrincial < QTDE_ALUNO_PRINCIPAL) 
        { 
         Console.Clear(); 
         Console.WriteLine("Por favor informe a matricula:"); 
         a.setMatricula(int.Parse(Console.ReadLine())); 
         Console.WriteLine("Por favor informe o nome completo do aluno:"); 
         a.setNomeCompleto(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe o nome da mãe:"); 
         a.setNomeDaMae(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe o nome do pai:"); 
         a.setNomeDoPai(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe o país"); 
         a.setEnderecoPais(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe o estado:"); 
         a.setEnderecoEstado(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe a rua e o numero"); 
         a.setEnderecoRuaNumero(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe o cep"); 
         a.setEnderecoCep(Console.ReadLine().ToUpper()); 
         Console.WriteLine("Por favor informe o telefone para contato:"); 
         a.setTelefoneContato(Console.ReadLine().ToUpper()); 

         principal.Add(a.getMatricula(), a); 

         contadorAlunosPrincial++; 

        } 
        else 
         if (contadorAlunoReserva < QTDE_ALUNO_RESERVA) 
         { 
          Console.Clear(); 
          Console.WriteLine("Por favor informe a matricula:"); 
          a.setMatricula(int.Parse(Console.ReadLine())); 
          Console.WriteLine("Por favor informe o nome completo do aluno:"); 
          a.setNomeCompleto(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o nome da mãe:"); 
          a.setNomeDaMae(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o nome do pai:"); 
          a.setNomeDoPai(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o país"); 
          a.setEnderecoPais(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o estado:"); 
          a.setEnderecoEstado(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe a cidade:"); 
          a.setEnderecoCidade(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o bairro:"); 
          a.setEnderecoBairro(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe a rua e o numero"); 
          a.setEnderecoRuaNumero(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o cep"); 
          a.setEnderecoCep(Console.ReadLine().ToUpper()); 
          Console.WriteLine("Por favor informe o telefone para contato:"); 
          a.setTelefoneContato(Console.ReadLine().ToUpper()); 

          espera.Enqueue(a); 

          contadorAlunoReserva++; 
         } 
         else 
          Console.WriteLine("Não há mais vagas para cadastro de Alunos!\nTente novamente mais tarde."); 

        break; 

       case 2: 
        ImprimeMenu.imprimirMenuSecundário(); 
        opcao = int.Parse(Console.ReadLine()); 
        switch (opcao) 
        { 
         case 1: 
          ImprimeListagem.listagemSimples(principal); 
          break; 
         case 2: 
          break; 
        } 
        break; 

      } 
     } while (opcao != 7); 

    } 

    class ImprimeMenu 
    { 
     public static void imprimirMenuPrincipal() 
     { 
      Console.Clear(); 
      Console.WriteLine("Programa de cadastro de alunos! Escolha a opção desejada.\n\n" + 
       "1 – Cadastrar aluno\n" + 
       "2 – Imprimir lista de alunos\n" + 
       "3 – Imprimir lista de espera\n" + 
       "4 – Pesquisar aluno\n" + 
       "5 – Desistência\n" + 
       "6 – Sorteio\n" + 
       "7 – Sair"); 
     } 
     public static void imprimirMenuSecundário() 
     { 
      Console.Clear(); 
      Console.WriteLine("2 – Imprimir lista de alunos\n\n" + 
       "\t1 – Listagem simples\n" + 
       "\t2 – Listagem completa"); 
     } 
    } 

    class Aluno 
    { 
     public Aluno() 
     { 
      matricula = 0; 
      nomeCompleto = null; 
      nomeDaMae = null; 
      nomeDoPai = null; 
      enderecoPais = null; 
      enderecoEstado = null; 
      enderecoCidade = null; 
      enderecoBairro = null; 
      enderecoRuaNumero = null; 
      enderecoCep = null; 
      telefoneContato = null; 
     }    

     private int matricula; 
     private string nomeCompleto; 
     private string nomeDaMae; 
     private string nomeDoPai; 
     private string enderecoPais; 
     private string enderecoEstado; 
     private string enderecoCidade; 
     private string enderecoBairro; 
     private string enderecoRuaNumero; 
     private string enderecoCep; 
     private string telefoneContato; 

     public void setMatricula(int m) 
     { 
      matricula = m; 
     } 

     public void setNomeCompleto(string nome) 
     { 
      nomeCompleto = nome; 
     } 

     public void setNomeDaMae(string nomeMae) 
     { 
      nomeDaMae = nomeMae; 
     } 

     public void setNomeDoPai(string nomePai) 
     { 
      nomeDoPai = nomePai; 
     } 

     public void setEnderecoPais(string pais) 
     { 
      enderecoPais = pais; 
     } 

     public void setEnderecoEstado(string estado) 
     { 
      enderecoEstado = estado; 
     } 

     public void setEnderecoCidade(string cidade) 
     { 
      enderecoCidade = cidade; 
     } 

     public void setEnderecoBairro(string bairro) 
     { 
      enderecoBairro = bairro; 
     } 

     public void setEnderecoRuaNumero(string ruaNumero) 
     { 
      enderecoRuaNumero = ruaNumero; 
     } 

     public void setEnderecoCep(string cep) 
     { 
      enderecoCep = cep; 
     } 

     public void setTelefoneContato(string telefone) 
     { 
      telefoneContato = telefone; 
     } 

     public int getMatricula() { return matricula; } 
     public string getNomeCompleto() { return nomeCompleto; } 
     public string getNomeDaMae() { return nomeDaMae; } 
     public string getEnderecoPais() { return enderecoPais; } 
     public string getEnderecoEstado() { return enderecoEstado; } 
     public string getEnderecoCidade() { return enderecoCidade; } 
     public string getEnderecoBairro() { return enderecoBairro; } 
     public string getEnderecoRuaNumero() { return enderecoRuaNumero; } 
     public string getEnderecoCep() { return enderecoCep; } 
     public string getTelefoneContato() { return telefoneContato; }   

    } 

    class ImprimeListagem 
    { 
     public static void listagemSimples(Dictionary<int,object> origem) 
     { 

      foreach (KeyValuePair<int, object> chave in origem) 
      { 
       Console.WriteLine(chave.Key+"|"+chave.Value); 

       Console.ReadKey(); 
      } 

     }    
    } 

在此先感謝和抱歉我的英語不好,這不是我的母語。 :d

+1

題外話了Object,但你知道,你可以使用'Matriculo'和'NomeCompleto' [屬性](HTTP ://msdn.microsoft.com/en-gb/library/x9fsa0sw.aspx),而不是單獨的getter和setter方法。 C#不是Java。 – LukeH 2013-03-25 01:32:48

+0

我不是,爲小費thnks。 :) – Dib 2013-03-26 16:01:33

回答

5

看起來像你想:

Dictionary<int,Aluno> principal = new Dictionary<int,Aluno>(); 

一般來說,當一個需要使用object和泛型類型的參數極爲罕見。在大多數情況下,你應該使用具體類型(或接口)。

如果你真的發現自己的object value;而不是具體的類型 - 你可以投它來鍵入你期望的「價值」爲:MyType = (MyType)value;

+0

謝謝,是的,這是我想要的,對新手問題抱歉。 :D – Dib 2013-03-26 16:00:14

2

你需要顯式轉換,你從字典到你的Aluno類這樣((Aluno)chave.Value)

+0

謝謝,幫了很多! :) – Dib 2013-03-26 15:59:28

相關問題