Results 1 to 12 of 12

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

Hybrid View

  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

    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