2014-12-03 134 views
0

我試圖讓公共班級寵物穿着不同形式的連接,但我無法做到,我可以在哪裏將公共班級寵物與我創建的每個表單都是?如何從一種表格訪問某個表格到另一個表格

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

我儘量讓這種形式的連接,但我有一個mascotas錯誤< -

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

的完整代碼:

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) 
     { 

     } 
    } 
} 

另一種形式:

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) 
     { 

     } 


    } 
} 
+0

mascostas是嵌套在Program和Form1中的類。首先,我會刪除其中一個,然後使用全名來標識它(例如Form1.mascotas或Program.mascotas,根據哪一個您將保留以及哪一個您將放棄)。甚至更好(在你的情況下)放下其中一個,並將該類移出其包含的類以使其可直接訪問。 – 2014-12-03 20:33:42

回答

1

Right cli ck在你的項目上,「Add」和「Class ...」,輸入mascotas.cs作爲名字並定義你的班級。您將有這樣的事情:

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

您定義mascotas爲嵌套類的,所以你沒法從另一個類訪問它。

如果你的意思是這個!

+0

是的,這是這個!謝謝 !!! – 2014-12-03 20:46:52

+0

@MarcoMontoya:祝你好運;) – 2014-12-03 20:50:16

相關問題