using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Linq;using System.Text;namespace Clases_propias{ public class ClasePersona { public int Edad; public string Nombre; private string Apellido; public ClasePersona() //Constructor personalizado { Nombre = ""; Apellido = ""; }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ClasesPropias;namespace UsoDeClasePropia{ // instanciar y declarar objeto de mi clase ClasePersona Persona1; Persona1= new ClasePersona(); public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } }}
E:\Proyectos_carpeta_seguridad\Visual C#\Mis cosas\UsoDeClasePropia\UsoDeClasePropia\Form1.cs(14,5): error CS0116: A namespace does not directly contain members such as fields or methods
E:\Proyectos_carpeta_seguridad\Visual C#\Mis cosas\UsoDeClasePropia\UsoDeClasePropia\Form1.cs(15,19): error CS1518: Expected class, delegate, enum, interface, or struct
A namespace does not directly contain members such as fields or methods
Un namespace no contiene directamente miembros como campos (las variables miembro se consideran campos) o metodos
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ClasesPropias;namespace UsoDeClasePropia{ // instanciar y declarar objeto de mi clase public partial class Form1 : Form { // Definicion de tu variable miembro Persona1 // dentro de la clase Form1 ClasePersona Persona1; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Asignacion de tu variable Miembro // dentro del metodo Form1_Load Persona1 = new ClasePersona(); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LibreriaPersonas{ public class Persona string nombre = ""; string apellido = ""; string direccion = ""; public Persona()// el constructor de momento sera "nulo", //ya que la persona la definira el usuario { /*this.nombre = nombre; this.apellido = apellido; this.direccion = direccion;*/ } //public void DatosPersona (string nombre,string apellido, string direccion) public void DatosPersona(string nombre) { } } ////* Clases hija que heredan de la padre class Persona *//// public class Alumno : Persona { string asignatura = ""; public Alumno() { //this.asignatura = asignatura; } }}
Form1.cs
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using LibreriaPersonas;namespace UsoDeLibreriaPersonas{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void comboBoxDinamico_SelectedIndexChanged(object sender, EventArgs e) { string nombreAux,apellidoAux,direccionAux; switch (comboBoxDinamico.Text) { case "Nombre": //MessageBox.Show("Teclea nombre"); VentanaDatosPersona.Visible = true; VentanaDatosPersona.Focus(); nombreAux = comboBoxDinamico.Text; VentanaPersona ventanaEscribeDatosPersona = new VentanaPersona(); ventanaEscribeDatosPersona.EscribeDatosPersona(nombreAux); break; case "Apellido": VentanaDatosPersona.Visible = true; VentanaDatosPersona.Focus(); apellidoAux = comboBoxDinamico.Text; break; case "Edad": VentanaDatosPersona.Visible = true; VentanaDatosPersona.Focus(); direccionAux = comboBoxDinamico.Text; break; default: MessageBox.Show("por defecto"); break; } } private void NuevaPersona_Click(object sender, EventArgs e) { persona1= new Persona(); short incrementaPersona; ComboBox comboBoxDinamico = new ComboBox(); this.Controls.Add(this.comboBoxDinamico); this.comboBoxDinamico.SelectedIndexChanged += new System.EventHandler(this.comboBoxDinamico_SelectedIndexChanged); } }}
VentanaPersona.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using System.ComponentModel;using System.Data;using System.Drawing;using LibreriaPersonas;namespace UsoDeLibreriaPersonas{ class VentanaPersona { private System.Windows.Forms.TextBox CajaTextoMuestraPersona; private void InitializeComponent() { TextBox CajaTextoMuestraPersona = new TextBox(); // // CajaTextoMuestraPersona // this.CajaTextoMuestraPersona.Location = new System.Drawing.Point(200, 61); this.CajaTextoMuestraPersona.Name = "CajaTextoMuestraPersona"; this.CajaTextoMuestraPersona.Size = new System.Drawing.Size(100, 20); this.CajaTextoMuestraPersona.TabIndex = 4; this.CajaTextoMuestraPersona.Visible = true; } public VentanaPersona() { } public void EscribeDatosPersona(string Nombre, string Apellido, string Edad) { string vpNombre; string vpApellido; string vpEdad; vpNombre = Nombre; vpApellido = Apellido; vpEdad= Edad; } public void EscribeDatosPersona(string Nombre) { string vpNombre; vpNombre = Nombre; }
Form1.Designer.cs
namespace UsoDeLibreriaPersonas{ partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.toolStripMenuItemNuevo = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemNuevaPersona = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemNuevoAlumno = new System.Windows.Forms.ToolStripMenuItem(); this.comboBoxDinamico = new System.Windows.Forms.ComboBox(); this.VentanaDatosPersona = new System.Windows.Forms.TextBox(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItemNuevo}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(284, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // // toolStripMenuItemNuevo // this.toolStripMenuItemNuevo.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItemNuevaPersona, this.toolStripMenuItemNuevoAlumno}); this.toolStripMenuItemNuevo.Name = "toolStripMenuItemNuevo"; this.toolStripMenuItemNuevo.Size = new System.Drawing.Size(54, 20); this.toolStripMenuItemNuevo.Text = "Nuevo"; // // toolStripMenuItemNuevaPersona // this.toolStripMenuItemNuevaPersona.Name = "toolStripMenuItemNuevaPersona"; this.toolStripMenuItemNuevaPersona.Size = new System.Drawing.Size(155, 22); this.toolStripMenuItemNuevaPersona.Text = "Nueva Persona"; this.toolStripMenuItemNuevaPersona.Click += new System.EventHandler(this.NuevaPersona_Click); // // toolStripMenuItemNuevoAlumno // this.toolStripMenuItemNuevoAlumno.Name = "toolStripMenuItemNuevoAlumno"; this.toolStripMenuItemNuevoAlumno.Size = new System.Drawing.Size(155, 22); this.toolStripMenuItemNuevoAlumno.Text = "Nuevo Alumno"; // // comboBoxDinamico // this.comboBoxDinamico.FormattingEnabled = true; this.comboBoxDinamico.Items.AddRange(new object[] { "Nombre", "Apellido", "Edad"}); this.comboBoxDinamico.Location = new System.Drawing.Point(20, 61); this.comboBoxDinamico.Name = "comboBoxDinamico"; this.comboBoxDinamico.Size = new System.Drawing.Size(121, 21); this.comboBoxDinamico.TabIndex = 3; // // VentanaDatosPersona // this.VentanaDatosPersona.Location = new System.Drawing.Point(154, 61); this.VentanaDatosPersona.Name = "VentanaDatosPersona"; this.VentanaDatosPersona.Size = new System.Drawing.Size(100, 20); this.VentanaDatosPersona.TabIndex = 4; this.VentanaDatosPersona.Visible = false; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.VentanaDatosPersona); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.Text = "Uso de dll para nombres"; this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemNuevo; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemNuevaPersona; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemNuevoAlumno; private LibreriaPersonas.Persona persona1; private LibreriaPersonas.Alumno alumno1; private System.Windows.Forms.ComboBox comboBoxDinamico; private System.Windows.Forms.TextBox VentanaDatosPersona; //private System.Windows.Forms.TextBox CajaTextoMuestraPersona; }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using LibreriaPersonas;namespace UsoDeLibreriaPersonas{ public partial class Form1 : Form { int indice=0; int i=0; int j=0; int[,] IndiceMatriz ; string[,] GuardaPersonas; public Form1() { InitializeComponent(); VentanaDatosPersona.KeyDown += new System.Windows.Forms.KeyEventHandler(VentanaDatosEscritos); GuardaPersonas = new string[5, 3]; // personas y sus 3 campos IndiceMatriz = new int[i, j]; } private void VentanaDatosEscritos(object sender, KeyEventArgs e) { bool nom = false; bool apell = false; bool ed = false; if (e.KeyValue == Convert.ToChar('\r')) { switch (this.comboBoxDinamico.Text) { case "Nombre": this.comboBoxDinamico.Items.Remove("Nombre"); break; case "Apellido": this.comboBoxDinamico.Items.Remove("Apellido"); break; case "Edad": this.comboBoxDinamico.Items.Remove("Edad"); break; } switch (indice) { case 0 : while (!nom ) { textBox1.Text += "Nombre:" + VentanaDatosPersona.Text + "\r" + "\n"; textBox1.Visible = true; GuardaPersonas[i, j] = VentanaDatosPersona.Text; VentanaDatosPersona.Text = ""; j++; indice++; nom = true; } break; case 1: while(!apell) { textBox1.Text += "Apellido:" + VentanaDatosPersona.Text + "\r" + "\n"; GuardaPersonas[i, j] = VentanaDatosPersona.Text; VentanaDatosPersona.Text=""; indice++; j++; apell = true; } break; case 2: while (!ed) { textBox1.Text += "Edad:" + VentanaDatosPersona.Text + "\r" + "\n"; GuardaPersonas[i, j] = VentanaDatosPersona.Text; VentanaDatosPersona.Text = ""; indice++; j = 0; ed = true; } MessageBox.Show(GuardaPersonas[0, 0]); MessageBox.Show(GuardaPersonas[0, 1]); MessageBox.Show(GuardaPersonas[0, 2]); this.botonOK.Visible = true; break; } } } private void comboBoxDinamico_SelectedIndexChanged(object sender, EventArgs e) { string apellidoAux,direccionAux; VentanaDatosPersona.Visible = true; switch (comboBoxDinamico.Text) { case "Nombre": //MessageBox.Show("Teclea nombre"); VentanaDatosPersona.Visible = true; VentanaDatosPersona.Focus(); nombreAux = comboBoxDinamico.Text; break; case "Apellido": VentanaDatosPersona.Visible = true; VentanaDatosPersona.Focus(); apellidoAux = comboBoxDinamico.Text; break; case "Edad": VentanaDatosPersona.Visible = true; VentanaDatosPersona.Focus(); direccionAux = comboBoxDinamico.Text; break; default: MessageBox.Show("por defecto"); break; } } private void RehacerComponentes() { // // comboBoxDinamico // this.comboBoxDinamico.FormattingEnabled = true; this.comboBoxDinamico.Items.AddRange(new object[] { "Nombre", "Apellido", "Edad"}); this.comboBoxDinamico.Location = new System.Drawing.Point(20, 61); this.comboBoxDinamico.Name = "comboBoxDinamico"; this.comboBoxDinamico.Size = new System.Drawing.Size(121, 21); this.comboBoxDinamico.TabIndex = 3; // // VentanaDatosPersona // this.VentanaDatosPersona.Location = new System.Drawing.Point(154, 61); this.VentanaDatosPersona.Name = "VentanaDatosPersona"; this.VentanaDatosPersona.Size = new System.Drawing.Size(100, 20); this.VentanaDatosPersona.TabIndex = 4; this.VentanaDatosPersona.Visible = false; // // textBoxVentanaDatosEscritos // this.textBoxVentanaDatosEscritos.Location = new System.Drawing.Point(200, 61); this.textBoxVentanaDatosEscritos.Name = "textBoxVentanaDatosEscritos"; this.textBoxVentanaDatosEscritos.Size = new System.Drawing.Size(100, 20); this.textBoxVentanaDatosEscritos.TabIndex = 4; this.textBoxVentanaDatosEscritos.Visible = false; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(52, 169); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(172, 63); this.textBox1.TabIndex = 5; this.textBox1.Visible = false; // // botonOK // this.botonOK.Location = new System.Drawing.Point(522, 95); this.botonOK.Name = "botonOK"; this.botonOK.Size = new System.Drawing.Size(75, 23); this.botonOK.TabIndex = 6; this.botonOK.Text = "OK"; this.botonOK.UseVisualStyleBackColor = true; this.botonOK.Visible = false; this.botonOK.Click += new System.EventHandler(this.botonOK_Click); } private void NuevaPersona_Click(object sender, EventArgs e) { persona1= new Persona(); ComboBox comboBoxDinamico = new ComboBox(); this.Controls.Add(this.comboBoxDinamico); this.comboBoxDinamico.SelectedIndexChanged += new System.EventHandler(this.comboBoxDinamico_SelectedIndexChanged); } private void botonOK_Click(object sender, EventArgs e) { VentanaDatosPersona.Clear(); VentanaDatosPersona.Visible = false; comboBoxDinamico.Hide(); comboBoxDinamico.Visible = false; textBox1.Clear(); this.textBox1.Visible = false; RehacerComponentes(); botonOK.Visible = false; } }}
namespace UsoDeLibreriaPersonas{ partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.toolStripMenuItemNuevo = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemNuevaPersona = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemNuevoAlumno = new System.Windows.Forms.ToolStripMenuItem(); this.comboBoxDinamico = new System.Windows.Forms.ComboBox(); this.VentanaDatosPersona = new System.Windows.Forms.TextBox(); this.textBoxVentanaDatosEscritos = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.botonOK = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItemNuevo}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(762, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // // toolStripMenuItemNuevo // this.toolStripMenuItemNuevo.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItemNuevaPersona, this.toolStripMenuItemNuevoAlumno}); this.toolStripMenuItemNuevo.Name = "toolStripMenuItemNuevo"; this.toolStripMenuItemNuevo.Size = new System.Drawing.Size(54, 20); this.toolStripMenuItemNuevo.Text = "Nuevo"; // // toolStripMenuItemNuevaPersona // this.toolStripMenuItemNuevaPersona.Name = "toolStripMenuItemNuevaPersona"; this.toolStripMenuItemNuevaPersona.Size = new System.Drawing.Size(155, 22); this.toolStripMenuItemNuevaPersona.Text = "Nueva Persona"; this.toolStripMenuItemNuevaPersona.Click += new System.EventHandler(this.NuevaPersona_Click); // // toolStripMenuItemNuevoAlumno // this.toolStripMenuItemNuevoAlumno.Name = "toolStripMenuItemNuevoAlumno"; this.toolStripMenuItemNuevoAlumno.Size = new System.Drawing.Size(155, 22); this.toolStripMenuItemNuevoAlumno.Text = "Nuevo Alumno"; // // comboBoxDinamico // this.comboBoxDinamico.FormattingEnabled = true; this.comboBoxDinamico.Items.AddRange(new object[] { "Nombre", "Apellido", "Edad"}); this.comboBoxDinamico.Location = new System.Drawing.Point(20, 61); this.comboBoxDinamico.Name = "comboBoxDinamico"; this.comboBoxDinamico.Size = new System.Drawing.Size(121, 21); this.comboBoxDinamico.TabIndex = 3; // // VentanaDatosPersona // this.VentanaDatosPersona.Location = new System.Drawing.Point(154, 61); this.VentanaDatosPersona.Name = "VentanaDatosPersona"; this.VentanaDatosPersona.Size = new System.Drawing.Size(100, 20); this.VentanaDatosPersona.TabIndex = 4; this.VentanaDatosPersona.Visible = false; // // textBoxVentanaDatosEscritos // this.textBoxVentanaDatosEscritos.Location = new System.Drawing.Point(200, 61); this.textBoxVentanaDatosEscritos.Name = "textBoxVentanaDatosEscritos"; this.textBoxVentanaDatosEscritos.Size = new System.Drawing.Size(100, 20); this.textBoxVentanaDatosEscritos.TabIndex = 4; this.textBoxVentanaDatosEscritos.Visible = false; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(52, 169); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(172, 63); this.textBox1.TabIndex = 5; this.textBox1.Visible = false; // // botonOK // this.botonOK.Location = new System.Drawing.Point(522, 95); this.botonOK.Name = "botonOK"; this.botonOK.Size = new System.Drawing.Size(75, 23); this.botonOK.TabIndex = 6; this.botonOK.Text = "OK"; this.botonOK.UseVisualStyleBackColor = true; this.botonOK.Visible = false; this.botonOK.Click += new System.EventHandler(this.botonOK_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(762, 319); this.Controls.Add(this.botonOK); this.Controls.Add(this.textBox1); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.VentanaDatosPersona); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.Text = "Uso de dll para nombres"; this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemNuevo; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemNuevaPersona; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemNuevoAlumno; private LibreriaPersonas.Persona persona1; private LibreriaPersonas.Alumno alumno1; private System.Windows.Forms.ComboBox comboBoxDinamico; private System.Windows.Forms.TextBox VentanaDatosPersona; private System.Windows.Forms.TextBox textBoxVentanaDatosEscritos; string nombreAux; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button botonOK; }}