[英]Function always returns null?
This function always returns null. 此函数始终返回null。 sString is a class with a string "Name" and string "Value" values. sString是具有字符串“名称”和字符串“ Value”的值的类。 Don't ask why I'm not using a regular string-- it's complicated. 不要问为什么我不使用常规字符串-这很复杂。 here's my function: 这是我的功能:
static string Get_sString(string VarName, ref List<sString> VarList)
{
foreach (sString TestsString in VarList)
{
if (TestsString.Name == VarName)
{
return TestsString.Name;
}
}
return null;
}
It's supposed to return the instance with the same Name value as VarName, and it works, except for the if statement always is false . 它应该返回具有与VarName相同的Name值的实例,并且它可以工作,除了if语句始终为false之外 。 I can't figure out why. 我不知道为什么。 I actually have an almost identical class called sDecimal, where the only difference is the Value property is a decimal, not a string. 实际上,我有一个几乎相同的类sDecimal,唯一的区别是Value属性是一个十进制,而不是一个字符串。 The Get_sDecimal() works perfectly fine with that, and the only difference between Get_sDecimal() and Get_sString() are that one tests sDecimal and one tests sString. Get_sDecimal()可以很好地工作,并且Get_sDecimal()和Get_sString()之间的唯一区别是一个测试sDecimal和一个测试sString。 Thanks! 谢谢!
EDIT: Here's sString Class. 编辑:这是sString类。
class sString
{
public string Name;
public string Value;
/// <summary>
/// String Value variables that may have the same name.
/// </summary>
/// <param name="n">Internal name of variable.</param>
/// <param name="v">Value of variable.</param>
public sString(string n, string v)
{
Name = n;
Value = v;
}
}
EDIT: Here's some output code (and output) to clear things up. 编辑:这是一些输出代码(和输出),用于清除内容。
static string Get_sString(string VarName, ref List<sString> VarList)
{
foreach (sString TestsString in VarList)
{
Console.WriteLine("Looking for: " + VarName);
Console.WriteLine("Comparing with: " + TestsString.Name);
if (TestsString.Name == VarName)
{
Console.WriteLine("Match!");
return TestsString.Name;
}
}
return null;
}
Here's the output: 这是输出:
Looking for: Q Comparing with: Q
EDIT: I added a couple more variables to the list. 编辑:我在列表中添加了两个更多的变量。 Here's the new output: 这是新的输出:
Looking for: Q Comparing with: Q Looking for: Q Comparing with: Z Looking for: Q Comparing with: VariableX
Still no Match. 仍然没有比赛。
The code fundamentally works: 该代码从根本上起作用:
var list = new List<sString>
{
new sString("foo", "123"),
new sString("bar", "456")
};
var s = Get_sString("foo", ref list);
// = "foo", not null
Don't get me wrong - I'd change every line of it. 别误会我-我会更改每一行。 But it doesn't always return null
. 但是它并不总是返回null
。
Brief problem list: 简要问题清单:
ref
不必要的ref
Dictionary<string,string>
可以只使用Dictionary<string,string>
.Value
rather than .Name
我想知道您是否应该返回.Value
而不是.Name
You do realize that you have basically re-implemented LINQ: 您确实意识到您已经基本上重新实现了LINQ:
return VarList.FirstOrDefault(x=> x.Name == VarName);
Also your sString
class could just be a KeyValuePair<string,string>
另外,您的sString
类可能只是一个KeyValuePair<string,string>
UPDATE 更新
Actually, you said : 实际上,您说的是 :
It's supposed to return the instance with the same Name value as VarName 应该返回具有与VarName相同的Name值的实例
But it doesn't return an sString
instance. 但是它不会返回sString
实例。 Instead, it returns TestsString.Name
- which is the same string you started with, but only if it's in the list. 而是返回TestsString.Name
与您开始时使用的字符串相同,但仅在列表中。 So basically, this is a complicated "contains" check. 因此,基本上,这是一个复杂的“包含”检查。
You should use String.Equals instead of == operator. 您应该使用String.Equals而不是==运算符。
== operator does not always work. ==运算符并不总是起作用。 you can find samples in the thread Are string.Equals() and == operator really same? 您可以在线程中找到样本string.Equals()和==运算符真的一样吗? . 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.