设置默认值 [英] Setting a Default Value

查看:85
本文介绍了设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net/csharp应用程序,需要一个特定的变量才能使b
正常工作。当应用程序首次运行时,此变量通常通过URL

中的查询字符串传递,但在某些情况下查询

字符串可能不包含变量。所以我需要一些建立

默认值的方法,如果没有设置。


有什么方法可以在page_load上设置查询字符串或者有某种方式

我可以使用一个全局变量,可以在我的应用程序中访问

而不是?在伪代码中类似于:


public void Page_Load(object sender,EventArgs e)

{

if(querystring == null)

{

global myvariable =" somevalue";

} else {

global myvariable = querystring ;

}

}


protected void dostuff(对象发送者,EventArgs e)

{

if(myvariable ==" foo")

{

//做东西

}

}


希望这是有道理的 - 为了清楚起见,我尽可能地简化了我的

情况。


谢谢

布拉德

解决方案

把它放在某个班级。


public static string SafeSessionGet(Page pageObj,string requestKey,

string defaultValue)

{

if( pageObj.Request [requestKey] == null)

{

return defaultValue;

}

else

{

返回pageObj.Request [requestKey] .ToString();

}

}


公共静态字符串SafeSessionGet(Page pageObj,string requestKey)

{

return SafeSessionGet(pageObj,requestKey,string.Empty);

}


如果你愿意的话你可以在2.0中写一个泛型版本,但那个

可能有点矫枉过正。

私有字符串m_empUUID = string.Empty;


public void Page_Load(object sender,EventArgs e)

{



m_empUUID = SafeSessionGet(Page," empkey" );

//或


m_empUUID = SafeSessionGet(Page," empkey",

" 00000-00000- 000000000-000000000");


}




" Brad Baker" < br ** @ nospam.nospamwrote in message

news:ub ************** @ TK2MSFTNGP03.phx.gbl ...
< blockquote class =post_quotes>
我有一个asp.net/csharp应用程序需要一个特定的变量





正常工作。首次运行应用程序时,此变量通常通过



URL


中的查询字符串传递但在某些情况下



查询


字符串可能不包含变量。所以我需要一些建立

默认值的方法,如果没有设置。


有什么方法可以在page_load上设置查询字符串或者有一些



方式


我可以使用可在整个应用程序中访问的全局变量

而不是?在伪代码中类似于:


public void Page_Load(object sender,EventArgs e)

{

if(querystring == null)

{

global myvariable =" somevalue";

} else {

global myvariable = querystring ;

}

}


protected void dostuff(对象发送者,EventArgs e)

{

if(myvariable ==" foo")

{

//做东西

}

}


希望这是有道理的 - 为了清楚起见,我尽可能地简化了我的

情况。


谢谢

布拉德



我是一点点asp.net/csharp新手所以请忍受m即如果我正确理解你,我会调用:SafeSessionGet(myvariable)来获取

每次需要时查询字符串值(或指定默认值)

参考myvariable?


假设'这是正确的,我不确定这是否是我最好的解决方案

将通过数据库查询分配默认值。所以例如

我可能有以下(再次使用伪代码):


如果myvariable == null {


运行以下SQL查询:select * from table,其中x = 1


myvariable = sql查询的第一个结果


}


例如,如果我需要从四个不同的部分调用myvariable

我的代码我将不得不四次运行sql查询?难道这不公平吗?b b b b效率低下吗?我曾经在尝试使用一个SQL查询设置myvariable

,然后在整个应用程序中重复使用它。我是希望通过某种全局变量来做到这一点(我不确定csharp

有没有这样的东西)。


也许我误解了你,或者也许还有其他的方法我很乐意忽略你。您可以提供的任何其他指导将是真诚的

赞赏。 :)


再次感谢,


布拉德


" sloan" < sl *** @ ipass.netwrote in message

news:Oc ************** @ TK2MSFTNGP03.phx.gbl ...


把它放在某个类的某个类中。


public static string SafeSessionGet(Page pageObj,string requestKey,

string defaultValue)

{

if(pageObj.Request [requestKey] == null)

{

return defaultValue;

}

else

{

return pageObj.Request [requestKey] .ToString(); < br $>
}

}


公共静态字符串SafeSessionGet(Page pageObj,string requestKey)

{

返回SafeSessionGet(pageObj,requestKey,string.Empty);

}



你可以如果你愿意,可以在2.0中写一个泛型版本,但是

可能有点过分。


私有字符串m_empUUID = string.Empty;


> public void Page_Load(object sender,EventArgs e)
{



m_empUUID = SafeSessionGet(Page," empkey" );

//或


m_empUUID = SafeSessionGet(Page," empkey",

" 00000-00000- 000000000-000000000");


>}




" ;布拉德贝克 < br ** @ nospam.nospamwrote in message

news:ub ************** @ TK2MSFTNGP03.phx.gbl ...
< blockquote class =post_quotes>
>我有一个asp.net/csharp应用程序,需要一个特定的变量





>正常工作。此变量通常通过



URL中的查询字符串传递


>当应用程序是首次运行但在某些情况下



查询


>字符串可能不包含变量。如果没有设置,我需要一些建立
默认值的方法。

是否有某种方法我可以在page_load上设置查询字符串或者是否有一些



way


>我可以使用可在整个应用程序中访问的全局变量
吗?伪代码如下:

public void Page_Load(object sender,EventArgs e)
{
if(querystring == null)
{
全球myvariable =" somevalue";
} else {
global myvariable = querystring;
}




受保护的void dostuff(对象发送者,EventArgs e) )
{
if(myvariable ==" foo")
{
//做的东西
}
}
希望这是有道理的 - 为了清楚起见,我尽可能地简化了我的
情况。

感谢
布拉德




如果你的默认值需要从数据库出来但它没有改变,

从Global.asax中的Application_Start中进行Sql调用。您可以

然后将其存储在应用程序状态或全局

类的静态字段中。

Peter


-

网站: http:// www。 eggheadcafe.com

UnBlog: http://petesbloggerama.blogspot .com

短网址&更多: http://ittyurl.net


Brad Baker写道:


我是一个asp.net/csharp新手所以请耐心等待。如果我正确理解你,我会调用:SafeSessionGet(myvariable)来获取

每次需要时查询字符串值(或指定默认值)

参考myvariable?


假设'这是正确的,我不确定这是否是我最好的解决方案

将通过数据库查询分配默认值。所以例如

我可能有以下(再次使用伪代码):


如果myvariable == null {


运行以下SQL查询:select * from table,其中x = 1


myvariable = sql查询的第一个结果


}


例如,如果我需要从四个不同的部分调用myvariable

我的代码我将不得不四次运行sql查询?难道这不公平吗?b b b b效率低下吗?我曾经在尝试使用一个SQL查询设置myvariable

,然后在整个应用程序中重复使用它。我是希望通过某种全局变量来做到这一点(我不确定csharp

有没有这样的东西)。


也许我误解了你,或者也许还有其他的方法我很乐意忽略你。您可以提供的任何其他指导将是真诚的

赞赏。 :)


再次感谢,


布拉德


" sloan" < sl *** @ ipass.netwrote in message

news:Oc ************** @ TK2MSFTNGP03.phx.gbl ...


把它放在某个类的某个类中。


public static string SafeSessionGet(Page pageObj,string requestKey,

string defaultValue)

{

if(pageObj.Request [requestKey] == null)

{

return defaultValue;

}

else

{

return pageObj.Request [requestKey] .ToString(); < br $>
}

}


公共静态字符串SafeSessionGet(Page pageObj,string requestKey)

{

返回SafeSessionGet(pageObj,requestKey,string.Empty);

}


你可以写一个泛型版本如果你愿意,可以在2.0中使用,但是

可能有点过分。

私有字符串m_empUUID = string.Empty;


公开void Page_Load(object sender,EventArgs e)

{



m_empUUID = SafeSessionGet(Page," empkey" );

//或


m_empUUID = SafeSessionGet(Page," empkey",

" 00000-00000- 000000000-000000000");


}




" Brad Baker" < br ** @ nospam.nospamwrote in message

news:ub ************** @ TK2MSFTNGP03.phx.gbl ...
< blockquote class =post_quotes>
我有一个asp.net/csharp应用程序需要一个特定的变量





正常工作。首次运行应用程序时,此变量通常通过



URL


中的查询字符串传递但在某些情况下



查询


字符串可能不包含变量。所以我需要一些建立

默认值的方法,如果没有设置。


有什么方法可以在page_load上设置查询字符串或者有一些



方式


我可以使用可在整个应用程序中访问的全局变量

而不是?在伪代码中类似于:


public void Page_Load(object sender,EventArgs e)

{

if(querystring == null)

{

global myvariable =" somevalue";

} else {

global myvariable = querystring ;

}

}


protected void dostuff(对象发送者,EventArgs e)

{

if(myvariable ==" foo")

{

//做东西

}

}


希望这是有道理的 - 为了清楚起见,我尽可能地简化了我的

情况。


谢谢

布拉德




I have an asp.net/csharp application that requires a particular variable to
work properly. This variable is usually passed via a query string in the URL
when the application is first run but under certain circumstances the query
string may not contain the variable. So I need some way of establishing a
default value if one isn''t set.

Is there some way I can set a query string on page_load OR is there some way
I can use a global variable which is accessible throughout my application
instead? In pseudo code something like:

public void Page_Load(object sender, EventArgs e)
{
if (querystring == null)
{
global myvariable = "somevalue";
} else {
global myvariable = querystring;
}
}

protected void dostuff(object sender, EventArgs e)
{
if (myvariable == "foo")
{
// do stuff
}
}

Hopefully this makes sense - for clarity sake I have tried to simplify my
situation as much as possible.

Thanks
Brad

解决方案

Put this in a class somewhere.

public static string SafeSessionGet(Page pageObj, string requestKey,
string defaultValue)
{
if (pageObj.Request[requestKey] == null)
{
return defaultValue;
}
else
{
return pageObj.Request[requestKey].ToString ();
}
}


public static string SafeSessionGet(Page pageObj, string requestKey)
{
return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
}

You could write a generics version of this in 2.0 if you wanted, but that
might be overkill.
private string m_empUUID = string.Empty;

public void Page_Load(object sender, EventArgs e)
{


m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
//or

m_empUUID = SafeSessionGet ( Page , "empkey" ,
"00000-00000-000000000-000000000" ) ;

}



"Brad Baker" <br**@nospam.nospamwrote in message
news:ub**************@TK2MSFTNGP03.phx.gbl...

I have an asp.net/csharp application that requires a particular variable

to

work properly. This variable is usually passed via a query string in the

URL

when the application is first run but under certain circumstances the

query

string may not contain the variable. So I need some way of establishing a
default value if one isn''t set.

Is there some way I can set a query string on page_load OR is there some

way

I can use a global variable which is accessible throughout my application
instead? In pseudo code something like:

public void Page_Load(object sender, EventArgs e)
{
if (querystring == null)
{
global myvariable = "somevalue";
} else {
global myvariable = querystring;
}
}

protected void dostuff(object sender, EventArgs e)
{
if (myvariable == "foo")
{
// do stuff
}
}

Hopefully this makes sense - for clarity sake I have tried to simplify my
situation as much as possible.

Thanks
Brad



I''m a bit of an asp.net/csharp newbie so please bear with me. If I
understand you correctly, I would call: SafeSessionGet(myvariable) to get
the query string value (or assign a default value) each time I needed to
reference myvariable?

Assuming that''s correct, I''m not sure if that''s the best solution as I am
going to assign the default value through a database query. So for instance
I might have the following (again in pseudo code):

if myvariable == null {

run the following SQL query: select * from table where x = 1

myvariable = first result of sql query

}

So for instance if I needed to call myvariable from four different sections
of my code I would have to run the sql query four times? Isn''t that fairly
inefficient performance wise? I was thinking of trying to set myvariable
once using one SQL query then reuse it throughout my application. I was
hoping to do that through some sort of global variable (I''m not sure csharp
has anything like that or not though).

Maybe I am misunderstanding you or maybe there is some other approach I''m
overlooking. Any additional guidance you could provide would be sincerely
appreciated. :)

Thanks Again,

Brad

"sloan" <sl***@ipass.netwrote in message
news:Oc**************@TK2MSFTNGP03.phx.gbl...

Put this in a class somewhere.

public static string SafeSessionGet(Page pageObj, string requestKey,
string defaultValue)
{
if (pageObj.Request[requestKey] == null)
{
return defaultValue;
}
else
{
return pageObj.Request[requestKey].ToString ();
}
}


public static string SafeSessionGet(Page pageObj, string requestKey)
{
return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
}

You could write a generics version of this in 2.0 if you wanted, but that
might be overkill.
private string m_empUUID = string.Empty;

>public void Page_Load(object sender, EventArgs e)
{



m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
//or

m_empUUID = SafeSessionGet ( Page , "empkey" ,
"00000-00000-000000000-000000000" ) ;

>}




"Brad Baker" <br**@nospam.nospamwrote in message
news:ub**************@TK2MSFTNGP03.phx.gbl...

>I have an asp.net/csharp application that requires a particular variable

to

>work properly. This variable is usually passed via a query string in the

URL

>when the application is first run but under certain circumstances the

query

>string may not contain the variable. So I need some way of establishing a
default value if one isn''t set.

Is there some way I can set a query string on page_load OR is there some

way

>I can use a global variable which is accessible throughout my application
instead? In pseudo code something like:

public void Page_Load(object sender, EventArgs e)
{
if (querystring == null)
{
global myvariable = "somevalue";
} else {
global myvariable = querystring;
}
}

protected void dostuff(object sender, EventArgs e)
{
if (myvariable == "foo")
{
// do stuff
}
}

Hopefully this makes sense - for clarity sake I have tried to simplify my
situation as much as possible.

Thanks
Brad




if your default value needs to come out of the database but it does not change,
the make the Sql call from within Application_Start in Global.asax. You can
then either store it in Application state or in a static field in the Global
class.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Brad Baker" wrote:

I''m a bit of an asp.net/csharp newbie so please bear with me. If I
understand you correctly, I would call: SafeSessionGet(myvariable) to get
the query string value (or assign a default value) each time I needed to
reference myvariable?

Assuming that''s correct, I''m not sure if that''s the best solution as I am
going to assign the default value through a database query. So for instance
I might have the following (again in pseudo code):

if myvariable == null {

run the following SQL query: select * from table where x = 1

myvariable = first result of sql query

}

So for instance if I needed to call myvariable from four different sections
of my code I would have to run the sql query four times? Isn''t that fairly
inefficient performance wise? I was thinking of trying to set myvariable
once using one SQL query then reuse it throughout my application. I was
hoping to do that through some sort of global variable (I''m not sure csharp
has anything like that or not though).

Maybe I am misunderstanding you or maybe there is some other approach I''m
overlooking. Any additional guidance you could provide would be sincerely
appreciated. :)

Thanks Again,

Brad

"sloan" <sl***@ipass.netwrote in message
news:Oc**************@TK2MSFTNGP03.phx.gbl...

Put this in a class somewhere.

public static string SafeSessionGet(Page pageObj, string requestKey,
string defaultValue)
{
if (pageObj.Request[requestKey] == null)
{
return defaultValue;
}
else
{
return pageObj.Request[requestKey].ToString ();
}
}


public static string SafeSessionGet(Page pageObj, string requestKey)
{
return SafeSessionGet ( pageObj , requestKey , string.Empty ) ;
}

You could write a generics version of this in 2.0 if you wanted, but that
might be overkill.
private string m_empUUID = string.Empty;

public void Page_Load(object sender, EventArgs e)
{


m_empUUID = SafeSessionGet ( Page , "empkey" ) ;
//or

m_empUUID = SafeSessionGet ( Page , "empkey" ,
"00000-00000-000000000-000000000" ) ;

}



"Brad Baker" <br**@nospam.nospamwrote in message
news:ub**************@TK2MSFTNGP03.phx.gbl...

I have an asp.net/csharp application that requires a particular variable

to

work properly. This variable is usually passed via a query string in the

URL

when the application is first run but under certain circumstances the

query

string may not contain the variable. So I need some way of establishing a
default value if one isn''t set.

Is there some way I can set a query string on page_load OR is there some

way

I can use a global variable which is accessible throughout my application
instead? In pseudo code something like:

public void Page_Load(object sender, EventArgs e)
{
if (querystring == null)
{
global myvariable = "somevalue";
} else {
global myvariable = querystring;
}
}

protected void dostuff(object sender, EventArgs e)
{
if (myvariable == "foo")
{
// do stuff
}
}

Hopefully this makes sense - for clarity sake I have tried to simplify my
situation as much as possible.

Thanks
Brad




这篇关于设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆