3

私は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;
        } 

それから私はそれを辞書で使用しました

        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

4

2 に答える 2

5

あなたが望むように見えます:

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

object一般に、ジェネリック型のパラメータを使用する必要がある場合は非常にまれです。ほとんどの場合、具象型(またはインターフェース)を使用する必要があります。

具体的な型の代わりに実際に自分自身を見つけた場合はobject value;、「値」が次のようになると予想される型にキャストできますMyType = (MyType)value;

于 2013-03-25T01:32:45.583 に答える
2

Object辞書から取得したものを次のAlunoようにクラスに明示的にキャストする必要があります((Aluno)chave.Value)

于 2013-03-25T01:35:28.143 に答える