静态变量? [英] static variables?
问题描述
我对C#很新,事实上我对此一无所知。我的b $ b有静态变量问题。我在C ++中使用静态变量很多,但看起来C#并不像C#
那样允许它在函数内部,所以如何处理需要静态的递归函数
变量。
还有一件事,我有一个这样的变量:
OracleDataReader dr = GetDataReader(sql,conn,out errMsg);
如何在我的递归函数中使用dr静态?
PS:抱歉听起来像个新手,但我实际上是。我真的很赞赏b $ b感谢任何帮助。谢谢
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:
OracleDataReader dr = GetDataReader(sql,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
推荐答案
" ye ******** @ yahoo.com" < SH *********** @ gmail.com>在消息中写道
news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
"ye********@yahoo.com" <sh***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
我对C#很新,其实我对此一无所知。我的静态变量存在问题。我在C ++中使用了很多静态变量,但看起来C#并不像C#
那样允许它在函数内部,所以如何处理需要静态变量的递归函数。
您可以通过参数传递变量,也可以使用本地类字段。
还有一件事,我有一个这样的变量:
OracleDataReader dr = GetDataReader(sql) ,conn,out errMsg);
如何在我的递归函数中使用dr static?
公共舱C
{
....
OracleDataReader dr;
public void SetConnection()
{
dr = GetDataReader(sql,conn,out errMsg);
}
}
我会强烈考虑将读者传递给下一个方法作为参数
但是。
PS:抱歉听到声音像一个新手,但我实际上是。我真的很感激任何帮助。谢谢
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:
OracleDataReader dr = GetDataReader(sql,conn, out errMsg);
How can I make dr static to use in my recursive function?
public class C
{
....
OracleDataReader dr;
public void SetConnection()
{
dr = GetDataReader(sql, 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
Yrsh,你不能声明静态方法变量很糟糕,但是
就在那里。在类范围内声明它们,作为私有字段,而不是。
-
HTH,
Kevin Spencer
Microsoft MVP
..Net开发人员
每分钟都有寻求者出生。
- 博士"快乐"哈利考克斯
" ye ******** @ yahoo.com" < SH *********** @ gmail.com>在消息中写道
news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
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********@yahoo.com" <sh***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
我对C#很新,其实我对此一无所知。我的静态变量存在问题。我在C ++中使用了很多静态变量,但看起来C#并不像C#
那样允许它在函数内部,所以如何处理需要静态变量的递归函数。
还有一件事,我有一个这样的变量:
OracleDataReader dr = GetDataReader(sql,conn,out errMsg);
如何在我的递归函数中使用dr static?
PS:抱歉听起来像个新手,但我其实是。我真的很感激任何帮助。谢谢
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:
OracleDataReader dr = GetDataReader(sql,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
谢谢^^。但是每次调用函数时都不会清除dr博士吗?
这是我的代码,我知道这显然是错的,但是无法弄清楚
如何解决它
private static bool Connection(string fromloc,string
toloc,OracleConnection conn)
{
string sql =" SELECT FLIGHTNO,LEGORDER,TOLOC from LEG WHERE FROMLOC =
''" + fromloc +"''" ;;
string errMsg ="" ;;
OracleDataReader dr = GetDataReader(sql,conn,out errMsg) ;
if(errMsg!="")
{
Console.WriteLine(errMsg);
返回false;
}
if(dr == null)
{
Console.WriteLine(" ;没有结果");
返回false;
}
while(dr.Read())
{
if(dr [2] .ToString()== toloc)
{
Console.WriteLine("航班:" + dr [0] +" legorder:" + dr [1]);
返回true;
}
else
return(连接(dr [2] .ToString(),toloc,conn));
}
//博士。关(); //关闭DataReader
//dr.Dispose();
//conn.Close(
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,OracleConnection conn)
{
string sql = "SELECT FLIGHTNO,LEGORDER,TOLOC FROM LEG WHERE FROMLOC =
''" + fromloc + "''";
string errMsg = "";
OracleDataReader dr = GetDataReader( sql, conn, out errMsg );
if ( errMsg != "" )
{
Console.WriteLine( errMsg );
return false;
}
if ( dr == null )
{
Console.WriteLine( "No result" );
return false;
}
while( dr.Read() )
{
if (dr[2].ToString()==toloc)
{
Console.WriteLine("flight: " + dr[0] + " legorder: " + dr[1]);
return true;
}
else
return(Connection(dr[2].ToString(),toloc,conn));
}
//dr.Close(); //close the DataReader
//dr.Dispose();
//conn.Close(
>
这篇关于静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!