Passing a value of a variable from the main page to a custom class
i am trying to pass a value from my main page to a customm class
i've read and read and read but nothing is sinking in
ok on the main webpage there is a dropdown list i want the selected index (int) to be passed to my custom class so it may determine which dataset to pass back to the main page.
any help is appreciated
Visual C# 2003
Re: Passing a value of a variable from the main page to a custom class
Main class:
contains variable : int x.
classOb a = new classOb(this);
Object class: classOb.cs
private Main m;
// constructor
public classOb(Main v)
{
m=v;
}
m.x // to use the variable x.
I don't know if I misunderstood your question but if you're trying to access a variable from another form, well that's how i do it.
Jennifer
Re: Passing a value of a variable from the main page to a custom class
yes i am trying to access varibles from different forms and classes
creating custom classes so all my code isnt on the main forms
thanx
Re: Passing a value of a variable from the main page to a custom class
ok im not sure i got it still.... im rusty frm not doing ANYTHING in a long time
i have this as my class im trying to use the index of a dropdownlist as the int which determines which data to pull up
Code:
using System;
namespace TCM
{
public class PInterface
{
public PInterface(Public_Page pp, int a, string m, int sr)
{
if(a.Equals(1))
{
m="1";
}
else
if(a.Equals(2))
{
m="2";
}
else
if(a.Equals(3))
{
m="3";
}
else
if(a.Equals(4))
{
m="4";
}
}
}
}
i guess i dont know how to make the variable visible to my other class(es) and vice versa
Re: Passing a value of a variable from the main page to a custom class
And wheres variable m came from?
Re: Passing a value of a variable from the main page to a custom class
sorry "L" was supposed to be changed to "m"
Re: Passing a value of a variable from the main page to a custom class
I dont know if this is what you want, but do you want other classes to access the variable a or m?
Code:
using System;
namespace TCM
{
public class PInterface
{
int a; string m; int str;
public PInterface(int a, string m, int sr)
{
if(a == 1))
{
m="1";
}
else
if(a.Equals(2))
{
m="2";
}
else
if(a.Equals(3))
{
m="3";
}
else
if(a.Equals(4))
{
m="4";
}
}
public int GetM() { return(a); } // method to get a
}
}
// other class
PInterface p = new PInterface();
// to access the variable a:
int x = p.GetM();
I prefer to use the set / get methods rather than the short hand that c# offers, but you could use them if you like.
I don't know if I misunderstood you post. Is this what you want?
Jennifer
Re: Passing a value of a variable from the main page to a custom class
no i want the PInterface to get a from the Main page
i want a the main page to get m from Pinterface
side question
would it be the same if m was a dataset?
Re: Passing a value of a variable from the main page to a custom class
public class PInterface
{
public PInterface(frmMain m)
{
this.m = m;
}
m.var // you access the variable var from object m with the dot operator
}
in the frmMain form:
public int var; // you must declare this variable as public or else PInterface will not be able to access it.
var = 9;
Hope this helps,
Jennifer.
Re: Passing a value of a variable from the main page to a custom class
what about a dataset?
say i wanted to populate a dropdown box
how would i pass the information from the ds in the custom class to a ds in the main class?
or could i access it like any other public variable?
could you give me an example?
Re: Passing a value of a variable from the main page to a custom class
ive done this code andthe only thing in my drop down list is "System.Data.DataRowView" but there is the same amount of rows in the ddl as there is in my table for it so im a step closer
heres my class
VB Code:
[COLOR=Blue]public class[/COLOR] MNamePop
{ SqlConnection sqlconn = new SqlConnection();
DataSet dsMNP = new DataSet();
public MNamePop(){}
// {ddlpop(MNPT(),dsMNP);}
[COLOR=Blue]public void[/COLOR] ddlpop()
{
sqlconn.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];//conn string
SqlCommand GetMItems = new //stored proc
SqlCommand("spGetRNames",sqlconn);
GetMItems.CommandType=CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(GetMItems);//da and ds
da.Fill(dsMNP,"tblRNames");
OConn();//open and closing of conn string and execution of stored proc
GetMItems.Connection=sqlconn;
GetMItems.ExecuteNonQuery();
CConn();
}
public void OConn(){sqlconn.Open();}
public void CConn(){sqlconn.Close();}
public DataSet Getds() { return dsMNP; }
public string MNPT() { return "tblRNames"; }
heres what is in the main class to populate the ddl
VB Code:
[COLOR=Blue]public class[/COLOR] Public_Page : System.Web.UI.Page
[+][[some windows crap]]
public string drDet;
// PInterface p= new PInterface();
MNamePop m=new MNamePop();
DataSet dsMNP;
[COLOR=Blue]private void[/COLOR] Page_Load(object sender, System.EventArgs e)
{if (!Page.IsPostBack)
{
this.m.ddlpop();
ddlURestaurants.DataSource=m.Getds();
ddlURestaurants.DataBind();
}
}
Re: Passing a value of a variable from the main page to a custom class
ok sorry folks
keep overlooking my dropdown list data text field