473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing variables between functions

147 100+
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
7 2751
DaveRook
147 100+
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
PRR
750 Expert 512MB
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
DaveRook
147 100+
Please ignore this and go to the next post, I clicked submit by mistake
Mar 2 '09 #4
DaveRook
147 100+
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
229 Expert 100+
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
DaveRook
147 100+
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
297 100+
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

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
3
by: jeffg | last post by:
The answer probably is staring me in the face and I don't see it, so I'll just ask. I try to limit the scope of variables and avoid global variables. But sometimes I get stumped by the following...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
3
by: tigrfire | last post by:
I don't want to use global variables to do this, but I'm stumped on how to pass a variable between one function to another, so if someone could help out, that'd be appreciated. Here's an example of...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
7
by: Khai | last post by:
First off, yes, I understand the crapload of tutorials out there, (well, rather, I understand there /are/ a crapload of tutorials out there), the problem is my comprehension. I'm trying to pass...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.