[英]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)
{
}
}
}
Right click on your project, "Add" and "Class...", type mascotas.cs
for name and define your class. 右键单击您的项目,“添加”和“类...”,键入mascotas.cs
作为名称并定义您的类。 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. 您将mascotas
定义为嵌套类,因此无法从另一个类访问它。
If you mean this! 如果你是这个意思!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.