[英]null value checking
public string AgentVersion
{
get { return m_version; }
} // property: Enabled
private string m_version = null;
The below declaration coding i did in Constructor 我在构造函数中执行的以下声明编码
string keySpoPath = SpoRegistry.SpoAgentRoot;
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(keySpoPath);
m_version = (string)regkey.GetValue(SpoRegistry.regValue_CurrentVersion);
here are my doubts 这是我的疑问
Do I need private string m_version = null;
我是否需要
private string m_version = null;
in property declaration in this context? 在这种情况下的财产申报? If I remove that one, are there any probs?
如果我删除那一个,有没有问题?
If AgentVersion
is null
or not getting any value or any strings other than numeric values I want to assign AgentVersion to value '0.0.0.0' otherwise i will display the numeric value which is coming. 如果
AgentVersion
为null
或未获得任何值或非数字值的任何字符串,我想将AgentVersion分配为值'0.0.0.0',否则我将显示即将出现的数字值。 Is this code below sufficient here string.IsNullOrEmpty(AgentVersion) ? "0.0.0.0" : AgentVersion;
下面的代码是否足够在这里
string.IsNullOrEmpty(AgentVersion) ? "0.0.0.0" : AgentVersion;
string.IsNullOrEmpty(AgentVersion) ? "0.0.0.0" : AgentVersion;
If then where and how can I implement in 'Property' 如果可以,那么在哪里以及如何在“属性”中实施
m_version
的值始终在构造函数中分配,因此无需将其设置为null
。 get { return string.IsNullOrEmpty(m_version) ? "0.0.0.0" : m_version; }
You can use Resharper, it will show you where you have redundant declarations. 您可以使用Resharper,它将向您显示您有多余的声明。 In this case it is redundant.
在这种情况下,这是多余的。
If String.Empty
is a valid value you can implement it using 如果
String.Empty
是有效值,则可以使用
public string AgentVersion { get { return m_version ?? "0.0.0.0"; } }
otherwise you are correct use String.IsNullOrEmpty
否则,您正确使用
String.IsNullOrEmpty
public string AgentVersion
{
get { return String.IsNullOrEmpty(m_version) ? "0.0.0.0":m_version; }
}
you can use this code. 您可以使用此代码。
public string AgentVersion
{
get
{
if(string.isNullOrEmpty(m_version))
{
string keySpoPath = SpoRegistry.SpoAgentRoot;
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(keySpoPath);
m_version = (string)regkey.GetValue(SpoRegistry.regValue_CurrentVersion);
}
m_version = string.isNullOrEmpty(m_version) ? m_version : "0.0.0.0";
return m_version;
}
}
string m_version;
1) You don't need to instantiate m_version
as null in this context. 1)在这种情况下,您无需将
m_version
实例m_version
null。 You usually only need to do this when declaring a local variable, to avoid the 'Use of unassigned local variable [name] ' compile error. 通常,仅在声明局部变量时才需要这样做,以避免出现“使用未分配的局部变量[name] ”的编译错误。 So
private string m_version;
因此
private string m_version;
will suffice. 就足够了。
2) For checking null and empty values, what you have with String.IsNullOrEmpty()
is fine. 2)对于检查空值和空值,使用
String.IsNullOrEmpty()
可以。 If you want to go check your property returns strings in the correct format, you can use Regex.IsMatch()
to ensure the property only ever returns strings in version number-format. 如果要检查属性是否以正确的格式返回字符串,则可以使用
Regex.IsMatch()
来确保属性仅以版本号格式返回字符串。
[DefaultValue("0.0.0.0")]
public string AgentVersion
{
get
{
return System.Text.RegularExpressions.Regex.IsMatch(m_version ?? String.Empty, @"\A\d+[.]\d+[.]\d+[.]\d+\z")
? m_version
: "0.0.0.0";
}
}
private string m_version;
My regular expressions are a bit rusty, so someone can probably improve on what I have here, but it's the general gist of what you need. 我的正则表达式有些生疏,所以有人可能可以改进我在这里的功能,但这是您所需要的基本内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.