Passing variables between functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Passing variables between functions

    Hi

    First of all, my terminology may be wrong, so sorry!

    I want to pass a variable between functions, such as

    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        string strTest = "This is the text";
    }
    
    public String Import(object o)
    {
     // some how import strTest so I can use it!
    // do something with the object o, may be compare Convert.ToString(o) to strTest!?
    }
    Any ideas?

    Thanks
  • DaveRook
    New Member
    • Jul 2007
    • 147

    #2
    Sorry, forgot to mention, the above post is calling an object from the 'front' page to my code behind page (which is why I have the obejct o) and why the function has the name Import. I know how to call functions by giving it correct names,
    eg

    public String Status()
    {
    string me;
    sad (me);
    }

    public String sad (me)
    {
    Response.Write( me);

    }

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      so what problem are you facing? you can pass object as long as you can convert to right type ... obviously there will be performance hit due to boxing and unboxing ...

      Comment

      • DaveRook
        New Member
        • Jul 2007
        • 147

        #4
        My Better Explanation

        Please ignore this and go to the next post, I clicked submit by mistake

        Comment

        • DaveRook
          New Member
          • Jul 2007
          • 147

          #5
          My Better Explanation

          On my aspx page I have:

          Code:
          <asp:DataList ID="grdB2B" runat="server" CellPadding="0" CellSpacing="0" ForeColor="#333333" >
          <ItemTemplate> 
          <%#ParallelMating(Eval("Gender"))%>
          </ItemTemplate>
          </asp:DataList>

          In my code behind, I have a function:

          Code:
          protected void Page_Load(object sender, EventArgs e)
              {
                 string strTEST = "hell";
              }
          
          public string ParallelMating(object o)
          		{
          			string s = Convert.ToString(o);
          			//do stuff
                                 //how do I call the variable strTEST from Page_Load?
          			return s;
          		}


          All I want to do is send strTest from the Page_Load to ParallelMating function.

          However, I don't want to have the variable returned back to Page_Load as the ParallelMating function uses the Eval statement.

          I hope I have explained this better?!

          Comment

          • vekipeki
            Recognized Expert New Member
            • Nov 2007
            • 229

            #6
            strTEST is a local (temporary) variable defined in Page_Load, so it cannot be accessed from other methods. You can use the Session object to hold some state data.

            Read this link also: ASP.NET State Management Recommendations

            Comment

            • DaveRook
              New Member
              • Jul 2007
              • 147

              #7
              Hi

              Thank you for your help, sadly it does not work.

              It seems like the session is created when a person leaves the page, not when they enter it! Despite the session being created in the Page_Load.

              So, the page loads but the session state is not being populated. When I click on the page again, the session is populated, so this method won't work.

              Any other ideas?

              Comment

              • kunal pawar
                Contributor
                • Oct 2007
                • 297

                #8
                You can use Global variable define strTEST on page_load event

                like this

                Code:
                string strTEST ;
                
                protected void Page_Load(object sender, EventArgs e) 
                    { 
                       strTEST = "hell"; 
                    } 
                  
                public string ParallelMating(object o) 
                        { 
                            string s = Convert.ToString(o); 
                            //do stuff 
                // You can use strTEST here.
                                       //how do I call the variable strTEST from Page_Load? 
                            return s; 
                        }
                But problem is when user refresh page you gloabl variable value get reset.

                Comment

                Working...