Foros del Web » Programación para mayores de 30 ;) » .NET »

objetos a nivel formulario en c#

Estas en el tema de objetos a nivel formulario en c# en el foro de .NET en Foros del Web. hola, como se declara objetos a nivel en C# tengo un void que se llama cargardatos asi: ___________________________________ private void CargarDatos() { oledbconnection Cn=new oledbconnection(); ...
  #1 (permalink)  
Antiguo 06/07/2008, 10:20
Avatar de robertgustavo  
Fecha de Ingreso: marzo-2008
Ubicación: Camaná - Arequipa
Mensajes: 213
Antigüedad: 16 años, 1 mes
Puntos: 4
objetos a nivel formulario en c#

hola, como se declara objetos a nivel en C#
tengo un void que se llama cargardatos asi:
___________________________________

private void CargarDatos()
{
oledbconnection Cn=new oledbconnection();
oledbdataadapter Da=new oledbdataadapter();
dataset ds=new dataset();
try
{
da.fill(ds,"clientes");
datagridview.datasource=ds.tables["clientes"]
}
}

private void Nuevo()
{
//desde aca no puedo usar "Cn", la coneccion o el "Ds", dataset

}

como hago para usarlos en cualquier parte del formulario
  #2 (permalink)  
Antiguo 06/07/2008, 10:53
Avatar de Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 16 años, 8 meses
Puntos: 87
Respuesta: objetos a nivel formulario en c#

Cn y Ds y Da tendrian q estar nivel del inicio de tu class formulario

inmediatamnete despues. de

public Class Fomulario1:Form
{
---> tus propiedades o miembros
--->tus metoods
}
__________________
Curso WF4
http://cursos.gurudotnet.com/ DF
Aprende HTML5
  #3 (permalink)  
Antiguo 06/07/2008, 17:26
Avatar de robertgustavo  
Fecha de Ingreso: marzo-2008
Ubicación: Camaná - Arequipa
Mensajes: 213
Antigüedad: 16 años, 1 mes
Puntos: 4
Respuesta: objetos a nivel formulario en c#

no funciona
en esta parte:

OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);

Cn sale marcado dice:


Error 1 Un inicializador de campo no puede hacer referencia al campo no estático, método o propiedad 'Myprogram.FrmArticulos.Cn' C:\Documents and Settings\Roberth\Mis documentos\Visual Studio 2005\Projects\myprogram\myprogram\FrmArticulos.cs 17 79Myprogram

__________________________________________________ ______________
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace MyProgram
{
public partial class FrmArticulos : Form

{
OleDbConnection Cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\SismasDB.mdb");
OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);
DataSet Ds = new DataSet();
DataView Dv = new DataView();
BindingSource Bs = new BindingSource();
public int Pos;
public FrmArticulos()
{
InitializeComponent();
}
private void FrmArticulos_Load(object sender, EventArgs e)
{
LlenarDatos();
FormatearGrid();
}
void LlenarDatos()
{
try
{
Da.Fill(Ds, "Articulos");
this.DgArticulos.DataSource = Ds.Tables["Articulos"];
Bs.DataSource = Ds.Tables["Articulos"];
}
catch (OleDbException ex)
{
MessageBox.Show("No se pudo conectar al origen de datos,compruebe que exista o reinstale la aplicación", "Error Inesperado", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
finally
{
Ds.Clear();
Da.Fill(Ds, "Articulos");
}
}
private void FormatearGrid()
{
this.DgArticulos.Columns[0].Width = 60;
this.DgArticulos.Columns[1].Width = 120;
}
private void CerrarArticulosForm(object sender, EventArgs e)
{
this.Close();
}

private void CmdBuscarImagen_Click(object sender, EventArgs e)
{
if (OFDBuscaImagen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.ImagePreview.Image = Image.FromFile(this.OFDBuscaImagen.FileName);
}
}
private void habilitarControles()
{
this.TxtCodigo.Enabled = true;
this.TxtPrecio.Enabled = true;
this.TxtPrecioCompra.Enabled = true;
this.TxtPrecioVenta.Enabled = true;
this.TxtProducto.Enabled = true;
this.TxtStock.Enabled = true;
this.TxtStockMaximo.Enabled = true;
this.TxtStockMaximo.Enabled = true;
this.CboCategoria.Enabled = true;
this.CboMarca.Enabled = true;
this.CboMedida.Enabled = true;
this.CboProveedor.Enabled = true;
this.CmdSelect1.Enabled = true;
this.CmdSelect2.Enabled = true;
this.CmdSelect3.Enabled = true;
this.CmdSelect4.Enabled = true;
this.CmdBuscarImagen.Enabled = true;
}
private void DeshabilitarControles()
{
this.TxtCodigo.Enabled = false;
this.TxtPrecio.Enabled = false;
this.TxtPrecioCompra.Enabled = false;
this.TxtPrecioVenta.Enabled = false;
this.TxtProducto.Enabled = false;
this.TxtStock.Enabled = false;
this.TxtStockMaximo.Enabled = false;
this.TxtStockMaximo.Enabled = false;
this.CboCategoria.Enabled = false;
this.CboMarca.Enabled = false;
this.CboMedida.Enabled = false;
this.CboProveedor.Enabled = false;
this.CmdSelect1.Enabled = false;
this.CmdSelect2.Enabled = false;
this.CmdSelect3.Enabled = false;
this.CmdSelect4.Enabled = false;
this.CmdBuscarImagen.Enabled = false;

}

private void CmdNuevo_Click(object sender, EventArgs e)
{
habilitarControles();
CmdGuardar.Enabled = true;
CmdNuevo.Enabled = false;
}
private void Nuevo()
{

}

private void CmdGuardar_Click(object sender, EventArgs e)
{
DeshabilitarControles();
CmdNuevo.Enabled=true;
CmdGuardar.Enabled=false;
}

}
}
  #4 (permalink)  
Antiguo 07/07/2008, 07:28
Avatar de Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 16 años, 8 meses
Puntos: 87
Respuesta: objetos a nivel formulario en c#

Si pero puedes poner todo la inicializacion del connection dentro del form load.

si fuece asi tu variable tendria q ser estatica q no es el caso solo quieres q sea globlal.

public partial class FrmArticulos : Form

{
OleDbConnection Cn;
OleDbDataAdapter Da;
DataSet Ds;
DataView Dv;
BindingSource Bs;
public int Pos;
public FrmArticulos()
{
OleDbConnection Cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\SismasDB.mdb");
OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);
DataSet Ds = new DataSet();
DataView Dv = new DataView();
BindingSource Bs = new BindingSource();
public int Pos;

InitializeComponent();
}

con eso tus variables son de ambito global dentro de tu forma y las inicializas cuando se crea tu forma.
__________________
Curso WF4
http://cursos.gurudotnet.com/ DF
Aprende HTML5
  #5 (permalink)  
Antiguo 07/07/2008, 08:27
Avatar de robertgustavo  
Fecha de Ingreso: marzo-2008
Ubicación: Camaná - Arequipa
Mensajes: 213
Antigüedad: 16 años, 1 mes
Puntos: 4
Respuesta: objetos a nivel formulario en c#

yes, salio!! gracias Peterpay
Saludos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:31.