Register / Display List C #

0

The function of this code is to read the information: Name of the course, 4 grades and the average

The problem: I can not transport the data in a List on Form Cadastrar and display information on Form Exibir .

Forms:

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();
        }
    }
}

Form Code 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();
            }
        }
    }
}
    
asked by anonymous 19.09.2017 / 23:28

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 since the list belongs to class Cadastro to access the list, change from dados[index] to 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 other classes, use nomedaclasse.metodoouvariavel .

    
20.09.2017 / 00:15