Register/Display List C#

Asked

Viewed 428 times

0

The function of this code is to read the information : Discipline name,4 notes and the average

The problem : I cannot carry the data in a List in the Form Cadastrar and display the information in the Form Exibir.

Forms : inserir a descrição da imagem aqui

Form Code Menu :

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 SimuladorFMM
{
    public partial class Menu : Form
    {
        public Menu()
        {
             InitializeComponent();
        }

        private void cadastrarMenu_Click(object sender, EventArgs e)
        {
             Cadastro abrirCadastro = new Cadastro();
             abrirCadastro.ShowDialog();
        }

        private void botaoExibir_Click(object sender, EventArgs e)
        {
           Exibir abrirExibir = new Exibir();
           abrirExibir.ShowDialog();
        }
    }
}

Code of the Form of Cadastro :

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 SimuladorFMM
{
    public partial class Cadastro : Form
    {
        public Cadastro()
        {
            InitializeComponent();
        }

        private void finalizar_Click(object sender, EventArgs e)
        {
            List<Disciplina> dados = new List<Disciplina>();
            Disciplina entrada = new Disciplina();
            entrada.nome = nomeCadastro.Text;
            entrada.nota[0] = Convert.ToDouble(nota1.Text);
            entrada.nota[1] = Convert.ToDouble(nota2.Text);
            entrada.nota[2] = Convert.ToDouble(nota3.Text);
            entrada.nota[3] = Convert.ToDouble(nota4.Text);
            entrada.media = (entrada.nota[0] + entrada.nota[1] + entrada.nota[2] + entrada.nota[3]) / 4;
            dados.Add(entrada);
        }
    }
}

Form Code Exibir :

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 SimuladorFMM
{
    public partial class Exibir : Form
    {
        int i = 0;
        public Exibir()
        {
            InitializeComponent();
        }

        private void Exibir_Load(object sender, EventArgs e)
        {

        }

        private void proximo_Click(object sender, EventArgs e)
        {
            if (i < dados.Count)
            {
                i++;
                exibirNome.Text = dados[i].nome;
                exibirNota1.Text = dados[i].nota[0].ToString();
                exibirNota2.Text = dados[i].nota[1].ToString();
                exibirNota3.Text = dados[i].nota[2].ToString();
                exibirNota4.Text = dados[i].nota[3].ToString();
                exibirMedia.Text = dados[i].media.ToString();
            }
        }

        private void anterior_Click(object sender, EventArgs e)
        {
            if (i > 0)
            {
                i--;
                exibirNome.Text = dados[i].nome;
                exibirNota1.Text = dados[i].nota[0].ToString();
                exibirNota2.Text = dados[i].nota[1].ToString();
                exibirNota3.Text = dados[i].nota[2].ToString();
                exibirNota4.Text = dados[i].nota[3].ToString();
                exibirMedia.Text = dados[i].media.ToString();
            }
        }
    }
}
  • where you create the array data in the class Display?

  • It was not created, precisely because I wanted to access the list of the other form

1 answer

0


In your class Cadastro, make the list estática:

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 SimuladorFMM
{
public partial class Cadastro : Form
{
    public Cadastro()
    {
        InitializeComponent();
    }
    static public List<Disciplina> dados = new List<Disciplina>();//Cria a lista estática
    private void finalizar_Click(object sender, EventArgs e)
    {

        Disciplina entrada = new Disciplina();
        entrada.nome = nomeCadastro.Text;
        entrada.nota[0] = Convert.ToDouble(nota1.Text);
        entrada.nota[1] = Convert.ToDouble(nota2.Text);
        entrada.nota[2] = Convert.ToDouble(nota3.Text);
        entrada.nota[3] = Convert.ToDouble(nota4.Text);
        entrada.media = (entrada.nota[0] + entrada.nota[1] + entrada.nota[2] + entrada.nota[3]) / 4;
        dados.Add(entrada);
    }
}
}

And as the list belongs to the class Cadastro to access the list have to change from dados[index] for Cadastro .dados[index]:

    private void proximo_Click(object sender, EventArgs e)
    {
        if (i < Cadastro.dados.Count)
        {
            i++;
            exibirNome.Text = Cadastro.dados[i].nome;
            exibirNota1.Text = Cadastro.dados[i].nota[0].ToString();
            exibirNota2.Text = Cadastro.dados[i].nota[1].ToString();
            exibirNota3.Text = Cadastro.dados[i].nota[2].ToString();
            exibirNota4.Text = Cadastro.dados[i].nota[3].ToString();
            exibirMedia.Text = Cadastro.dados[i].media.ToString();
        }
    }

private void anterior_Click(object sender, EventArgs e)
    {
        if (i > 0)
        {
            i--;
            exibirNome.Text = Cadastro.dados[i].nome;
            exibirNota1.Text = Cadastro.dados[i].nota[0].ToString();
            exibirNota2.Text = Cadastro.dados[i].nota[1].ToString();
            exibirNota3.Text = Cadastro.dados[i].nota[2].ToString();
            exibirNota4.Text = Cadastro.dados[i].nota[3].ToString();
            exibirMedia.Text = Cadastro.dados[i].media.ToString();
        }
    }

In order to access variables and methods from other classes, this method or variable must be public(public) and static(static).
From there to access in other classes, use nomedaclasse.metodoouvariavel.

  • 1

    Error CS0052 Inconsistent Accessibility: field type 'List<Disciplina>' is Less accessible than field 'Cadastro.dados' ?

  • You made the public after static?

  • Yes, Static public List<Discipline> data = new List<Discipline>();

  • Of course, you have to put the Register.data in the next void. See the answer, which I edited.

  • I did it, but the mistake continues

  • The list is being created inside or outside the finalir_Click void?

  • was raised outside

  • Look, in mine worked, worked, check if the code is equal to the answer. In which line, of which class is giving error?

  • The problem was the class that was not public, now it works

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.