By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,116 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,116 IT Pros & Developers. It's quick & easy.

Passing variables between functions

100+
P: 147
Hi

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

I want to pass a variable between functions, such as

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     string strTest = "This is the text";
  4. }
  5.  
  6. public String Import(object o)
  7. {
  8.  // some how import strTest so I can use it!
  9. // do something with the object o, may be compare Convert.ToString(o) to strTest!?
  10. }
Any ideas?

Thanks
Mar 2 '09 #1
Share this Question
Share on Google+
7 Replies


100+
P: 147
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);

}
Mar 2 '09 #2

Expert 100+
P: 750
PRR
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 ...
Mar 2 '09 #3

100+
P: 147
Please ignore this and go to the next post, I clicked submit by mistake
Mar 2 '09 #4

100+
P: 147
On my aspx page I have:

Expand|Select|Wrap|Line Numbers
  1. <asp:DataList ID="grdB2B" runat="server" CellPadding="0" CellSpacing="0" ForeColor="#333333" >
  2. <ItemTemplate> 
  3. <%#ParallelMating(Eval("Gender"))%>
  4. </ItemTemplate>
  5. </asp:DataList>

In my code behind, I have a function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.        string strTEST = "hell";
  5.     }
  6.  
  7. public string ParallelMating(object o)
  8.         {
  9.             string s = Convert.ToString(o);
  10.             //do stuff
  11.                        //how do I call the variable strTEST from Page_Load?
  12.             return s;
  13.         }


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?!
Mar 2 '09 #5

vekipeki
Expert 100+
P: 229
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
Mar 2 '09 #6

100+
P: 147
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?
Mar 2 '09 #7

kunal pawar
100+
P: 297
You can use Global variable define strTEST on page_load event

like this

Expand|Select|Wrap|Line Numbers
  1. string strTEST ;
  2.  
  3. protected void Page_Load(object sender, EventArgs e) 
  4.     { 
  5.        strTEST = "hell"; 
  6.     } 
  7.  
  8. public string ParallelMating(object o) 
  9.         { 
  10.             string s = Convert.ToString(o); 
  11.             //do stuff 
  12. // You can use strTEST here.
  13.                        //how do I call the variable strTEST from Page_Load? 
  14.             return s; 
  15.         } 
  16.  
  17.  
But problem is when user refresh page you gloabl variable value get reset.
Mar 3 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.