AnubisVolga
AnubisVolga

Reputation: 311

How to access a Class from one form to another form

I'm trying to get the public class pet to wear in different forms as a connection but I can not do, where can I put the public class pet to make connection with each forms that I create ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace prueba
{
   static class Program
   {
    /// <summary>
    /// Punto de entrada principal para la aplicación.
    /// </summary>  

        public class mascotas
        {
            public string nombremascota, nombredueño, sexo, especie, estado, respuesta;
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
     }
  }

I try to make connection with this form but i have a error in mascotas <--

this.listnombresmas.Items.Add(((mascotas)capturar[i]).);

the complete code:

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;
using System.Collections;

namespace prueba
{
    public partial class verdatos : Form
    {
        ArrayList capturar = new ArrayList();

        public verdatos(ArrayList mascotaguardar)
        {
            capturar = mascotaguardar;
            InitializeComponent();
            cargadatos();
        }
       void cargadatos()
        {
            for (int i = 0; i < capturar.Count; i++)
            {
                this.listnombresmas.Items.Add(((mascotas)capturar[i]).);
            }
        }
        private void label5_Click(object sender, EventArgs e)
        {

        }
    }
}

the other form:

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;
using System.Collections;

namespace prueba
{

    public partial class Form1 : Form
    {
        public class mascotas
        {
            public string nombremascota, nombredueño, sexo, especie, estado, respuesta;
        }

        string especie = "Pajaro", sexo = "Hembra", respuesta = "Si";
        ArrayList nuevamascota = new ArrayList();

        //----------------No Modificar NADA-------------------
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonguardar_Click(object sender, EventArgs e)
        {
            if(compruebadatos()==true)
            {
                MessageBox.Show("Guardado con éxito");
                almacenadatos();
                resetcontrols();
            }
            else
            {
                MessageBox.Show("Faltan datos");
            }
        }
        void resetcontrols()
        {
            this.tnomdueño.Text = "";
            this.tnommascota.Text = "";
            this.radiopajaro.Checked = true;
            this.radiohembra.Checked = true;
            this.radiosi.Checked = true;
            this.listvivienda.SelectedIndex = 0;
        }
        void almacenadatos()
        {
            mascotas almacenar = new mascotas();
            almacenar.nombremascota = this.tnommascota.Text;
            almacenar.nombredueño = this.tnomdueño.Text;
            almacenar.estado = this.listvivienda.Text;
            almacenar.especie = especie;
            almacenar.respuesta = respuesta;
            almacenar.sexo = sexo;

            nuevamascota.Add(almacenar);
        }

        #region botones

        private void radiopajaro_CheckedChanged(object sender, EventArgs e)
        {
            if(radiopajaro.Checked)
            {
                especie = "Pajaro";
            }
        }

        private void radioperro_CheckedChanged(object sender, EventArgs e)
        {
            if(radioperro.Checked)
            {
                especie = "Perro";
            }
        }

        private void radiogato_CheckedChanged(object sender, EventArgs e)
        {
            if(radiogato.Checked)
            {
                especie = "Gato";
            }
        }

        private void radioconejo_CheckedChanged(object sender, EventArgs e)
        {
            if(radioconejo.Checked)
            {
                especie = "Conejo";
            }
        }

        private void radioerizo_CheckedChanged(object sender, EventArgs e)
        {
            if(radioerizo.Checked)
            {
                especie = "Erizo";
            }
        }

        #endregion

        bool compruebadatos()
        {
            if(tnommascota.Text == "" || tnomdueño.Text == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void buttonverdatos2_Click(object sender, EventArgs e)
        {

        }


    }
}

Upvotes: 0

Views: 1029

Answers (1)

Ali Sepehri.Kh
Ali Sepehri.Kh

Reputation: 2508

Right click on your project, "Add" and "Class...", type mascotas.cs for name and define your class. You will have something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YourNamespace
{
    public class mascotas
    {
        public string nombremascota, nombredueño, sexo, especie, estado, respuesta;
    }
}

You defined mascotas as a nested class an thus you can not access to it from an another class.

If you mean this!

Upvotes: 1

Related Questions