Results 1 to 12 of 12

Thread: Passing a value of a variable from the main page to a custom class

  1. #1

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Angry 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
    Last edited by KingSatan; Feb 2nd, 2007 at 07:09 PM.

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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

  3. #3

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    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

  4. #4

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    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
    Last edited by KingSatan; Feb 7th, 2007 at 12:04 PM. Reason: miss3ed something

  5. #5
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: Passing a value of a variable from the main page to a custom class

    And wheres variable m came from?

  6. #6

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: Passing a value of a variable from the main page to a custom class

    sorry "L" was supposed to be changed to "m"

  7. #7
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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

  8. #8

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    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?

  9. #9
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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.

  10. #10

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    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?

  11. #11

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    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:
    1. [COLOR=Blue]public class[/COLOR] MNamePop
    2.     {   SqlConnection sqlconn = new SqlConnection();
    3.         DataSet dsMNP = new DataSet();
    4.         public MNamePop(){}
    5. //      {ddlpop(MNPT(),dsMNP);}
    6.        
    7.         [COLOR=Blue]public void[/COLOR] ddlpop()
    8.     {  
    9.             sqlconn.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];//conn string
    10.             SqlCommand GetMItems = new //stored proc
    11.             SqlCommand("spGetRNames",sqlconn);
    12.             GetMItems.CommandType=CommandType.StoredProcedure;
    13.             SqlDataAdapter da = new SqlDataAdapter(GetMItems);//da and ds
    14.             da.Fill(dsMNP,"tblRNames");
    15.             OConn();//open and closing of conn string and execution of stored proc
    16.             GetMItems.Connection=sqlconn;
    17.             GetMItems.ExecuteNonQuery();
    18.             CConn();
    19.         }
    20.         public void OConn(){sqlconn.Open();}
    21.         public void CConn(){sqlconn.Close();}
    22.         public DataSet Getds() { return dsMNP; }
    23.         public string MNPT() { return "tblRNames"; }

    heres what is in the main class to populate the ddl
    VB Code:
    1. [COLOR=Blue]public class[/COLOR] Public_Page : System.Web.UI.Page
    2. [+][[some windows crap]]
    3.         public string drDet;
    4. //      PInterface p= new PInterface();
    5.         MNamePop m=new MNamePop();
    6.         DataSet dsMNP;
    7.         [COLOR=Blue]private void[/COLOR] Page_Load(object sender, System.EventArgs e)
    8.        
    9.         {if (!Page.IsPostBack)
    10.          {
    11.             this.m.ddlpop();
    12.             ddlURestaurants.DataSource=m.Getds();
    13.             ddlURestaurants.DataBind();
    14.              }
    15.         }

  12. #12

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width