473,625 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static variables?

Hi, I'm very new to C#, in fact I dont know anything about it. Im
having problem with its static variables. I used static variables alot
in C++, but seem like C# doesnt allow it inside a function the way C#
do, so how can you handle recursive functions that need static
variable.

One more thing, I have a variable like this:
OracleDataReade r dr = GetDataReader(s ql,conn, out errMsg);
How can I make dr static to use in my recursive function?

PS: Sorry to sound like a newbie, but Im actually am. I'd really
appreciate any help. Thanks

Nov 17 '05 #1
22 5070

"ye********@yah oo.com" <sh***********@ gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi, I'm very new to C#, in fact I dont know anything about it. Im
having problem with its static variables. I used static variables alot
in C++, but seem like C# doesnt allow it inside a function the way C#
do, so how can you handle recursive functions that need static
variable.
You either pass the variable via a parameter or you use a local class field.
One more thing, I have a variable like this:
OracleDataReade r dr = GetDataReader(s ql,conn, out errMsg);
How can I make dr static to use in my recursive function?
public class C
{
....
OracleDataReade r dr;
public void SetConnection()
{
dr = GetDataReader(s ql, conn,out errMsg);
}
}
I'd strongly consider passing the reader to the next method as a parameter
however.
PS: Sorry to sound like a newbie, but Im actually am. I'd really
appreciate any help. Thanks

Nov 17 '05 #2
Yrsh, it kind of sucks that you can't declare static method variables, but
there you are. Declare them at class scope, as private fields, instead.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"ye********@yah oo.com" <sh***********@ gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi, I'm very new to C#, in fact I dont know anything about it. Im
having problem with its static variables. I used static variables alot
in C++, but seem like C# doesnt allow it inside a function the way C#
do, so how can you handle recursive functions that need static
variable.

One more thing, I have a variable like this:
OracleDataReade r dr = GetDataReader(s ql,conn, out errMsg);
How can I make dr static to use in my recursive function?

PS: Sorry to sound like a newbie, but Im actually am. I'd really
appreciate any help. Thanks

Nov 17 '05 #3
Thanks^^. But then wouldnt the dr be cleared each time the function is
called?

Here is my code, and I know it's wrong obviously, but cant figure out
how to fix it
private static bool Connection( string fromloc, string
toloc,OracleCon nection conn)
{

string sql = "SELECT FLIGHTNO,LEGORD ER,TOLOC FROM LEG WHERE FROMLOC =
'" + fromloc + "'";
string errMsg = "";

OracleDataReade r dr = GetDataReader( sql, conn, out errMsg );
if ( errMsg != "" )
{
Console.WriteLi ne( errMsg );
return false;
}
if ( dr == null )
{
Console.WriteLi ne( "No result" );
return false;
}
while( dr.Read() )
{

if (dr[2].ToString()==to loc)
{
Console.WriteLi ne("flight: " + dr[0] + " legorder: " + dr[1]);
return true;
}
else
return(Connecti on(dr[2].ToString(),tol oc,conn));
}
//dr.Close(); //close the DataReader
//dr.Dispose();
//conn.Close(

Nov 17 '05 #4
I do like static method variables but it appears the CLR does not
support it. VB.NET supports static method variables - its compiler
compiles such variables into private class fields.

Nov 17 '05 #5
>But then wouldnt the dr be cleared each time the function is
called?

Then you could initialize the var at declaration or in constructor.
If that is not posible, in the method check and only init if it is null.

Nov 17 '05 #6
Im trying to understand it.... Can anyone give me an example (any) or
better on the code I posted:

Task: given start-end location, find all possible connections.
If the function runs the first time and I get the first dr, then it
calls itself and i get the 2nd dr,... so on till the n dr. Then it goes
back from n to n-1.... to 1, how can I keep every dr from being deleted
each time the function called itself? Or is there a better way to solve
this prblem?

private static bool Connection( string fromloc, string
toloc,OracleCon nection conn)
{

string sql = "SELECT FLIGHTNO,LEGORD ER,TOLOC FROM LEG WHERE FROMLOC =
'" + fromloc + "'";
string errMsg = "";

OracleDataReade r dr = GetDataReader( sql, conn, out errMsg );
if ( errMsg != "" )
{
Console.WriteLi ne( errMsg );
return false;
}

if ( dr == null )
{
Console.WriteLi ne( "No result" );
return false;
}

while( dr.Read() )
{

if (dr[2].ToString()==to loc)
{
Console.WriteLi ne("flight: " + dr[0] + " legorder: " + dr[1]);
return true;
}

else
return(Connecti on(dr[2].ToString(),tol oc,conn));

}
}

Nov 17 '05 #7

"ye********@yah oo.com" <sh***********@ gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Im trying to understand it.... Can anyone give me an example (any) or
better on the code I posted:

Task: given start-end location, find all possible connections.
If the function runs the first time and I get the first dr, then it
calls itself and i get the 2nd dr,... so on till the n dr. Then it goes
back from n to n-1.... to 1, how can I keep every dr from being deleted
each time the function called itself? Or is there a better way to solve
this prblem?


A local field would work.

Frankly, I don't see where you are going with your code here, why do you
need to keep old DataReaders around?
Nov 17 '05 #8

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Yrsh, it kind of sucks that you can't declare static method variables, but
there you are. Declare them at class scope, as private fields, instead.

Static variables are little more than fields with constrained access anyway.
I've never found them to be terribly useful and I do think they blur the
lines a bit much. I find that it is too easy to consider a field as local
state, even with the static marker. --
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"ye********@yah oo.com" <sh***********@ gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi, I'm very new to C#, in fact I dont know anything about it. Im
having problem with its static variables. I used static variables alot
in C++, but seem like C# doesnt allow it inside a function the way C#
do, so how can you handle recursive functions that need static
variable.

One more thing, I have a variable like this:
OracleDataReade r dr = GetDataReader(s ql,conn, out errMsg);
How can I make dr static to use in my recursive function?

PS: Sorry to sound like a newbie, but Im actually am. I'd really
appreciate any help. Thanks


Nov 17 '05 #9
If I have table flight with flightno, from, to,.. I want my program to
return all possible connections ,given from X and to Y.
Here is the solution I come up with:
1.Search all flights from X
For example I get:

flightno from to
1 X Y
2 X A
3 X B

2.Loop thru the result
a. If the flight's destination is Y, return this path
b. Else, Search all flights goes from this result's location (for
example: all flights from A)

I hope I make it clearer? Now you see why I want to keep the old
datareader?

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3665
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab, turned in, graded and passed so I'm not trying to get someone to do my lab assignments, but after I got this back I was reading about the DecimalFormat and I tried to format my output but I keep getting an error message. can anyone please tell me...
2
1869
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
6355
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
4
1864
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp". I opened up two instances of my browser to simulate two users accessing the page at the same time by pointing to Employee.aspx below. Refreshing several times, the counter value is increased and shared between both browser...
8
6805
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx code-behind and in business logic (C# classes used by the aspx) lose their value. I cannot reproduce this on our development server, so I cannot understand what the cause of all this is. We are using asp.net 1.1 with IIS6 on win2003.
28
4610
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can anyone confirm this. Thanks. -- Dennis in Houston
5
6770
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not referenced by any part of the program. All the
9
8642
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
55
6190
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
16
8606
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions - which is Very Bad (tm) for reasons I understand! However, does this rule apply only to global static variables, or does it apply to procedure-level static variables.
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.