PDA

Click to See Complete Forum and Search --> : Passing a value of a variable from the main page to a custom class


KingSatan
Feb 2nd, 2007, 05:52 PM
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

JenniferBabe
Feb 4th, 2007, 08:49 AM
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

KingSatan
Feb 5th, 2007, 11:00 AM
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

KingSatan
Feb 5th, 2007, 05:34 PM
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


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

popskie
Feb 5th, 2007, 08:57 PM
And wheres variable m came from?

KingSatan
Feb 7th, 2007, 11:03 AM
sorry "L" was supposed to be changed to "m"

JenniferBabe
Feb 8th, 2007, 06:55 AM
I dont know if this is what you want, but do you want other classes to access the variable a or m?



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

KingSatan
Feb 8th, 2007, 10:54 AM
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?

JenniferBabe
Feb 8th, 2007, 02:03 PM
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.

KingSatan
Feb 20th, 2007, 01:43 PM
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?

KingSatan
Feb 20th, 2007, 02:10 PM
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
public class MNamePop
{ SqlConnection sqlconn = new SqlConnection();
DataSet dsMNP = new DataSet();
public MNamePop(){}
// {ddlpop(MNPT(),dsMNP);}

public void 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

public class Public_Page : System.Web.UI.Page
[+][[some windows crap]]
public string drDet;
// PInterface p= new PInterface();
MNamePop m=new MNamePop();
DataSet dsMNP;
private void Page_Load(object sender, System.EventArgs e)

{if (!Page.IsPostBack)
{
this.m.ddlpop();
ddlURestaurants.DataSource=m.Getds();
ddlURestaurants.DataBind();
}
}

KingSatan
Feb 21st, 2007, 01:22 PM
ok sorry folks
keep overlooking my dropdown list data text field