简体   繁体   English

Array.Find无法正常工作

[英]Array.Find is not working as expected

Help me out here, I must be doing something wrong, 请在这里帮助我,我一定做错了什么,

The first example here works fine 这里的第一个例子很好用

 DatabaseType = Array.Find(pArgs, Function(s As String) s.ToLower.Contains("/db:")).Replace("/db:", "") 

but if that was false and my variable turned out to be nothing then it throws and object ref exception, so as a result I wrapped an IF around it, but then it never returns true, and also it doesnt populate the variable, like the one below: 但是,如果那是错误的,而我的变量却变成了空,那么它将抛出并抛出对象引用异常,因此,我在它周围包裹了一个IF,但是它永远不会返回true,并且也不会像该变量那样填充该变量。下面:

 If DatabaseType = Array.Find(pArgs, Function(s As String) s.ToLower.Contains("/db:")).Replace("/db:", "") Then LogAndTrace("Database Type", DatabaseType)

What I need to do is : 我需要做的是:

Basically I have a num of commandlines arguments I am getting and I need to distiguish what they are each according to their prefix and assign them to the right property , but they are not always going to be there so I wanted to first see if it exist then replace the prefix with nothing, assign it to the property and write it to the log. 基本上我有很多命令行参数,我需要根据其前缀来区分它们分别是什么,并将它们分配给正确的属性,但是它们并不总是存在,所以我想先看看它是否存在然后将前缀替换为空,将其分配给属性并将其写入日志。

It's a bad idea to try to perform assignment within the condition of an If statement. 尝试在If语句的条件下执行赋值是一个坏主意。 I suspect you want something like this: 我怀疑您想要这样的东西:

Dim databaseType = Array.Find(pArgs, Function(s As String) s.ToLower.Contains("/db:"))
If databaseType IsNot Nothing Then
    databaseType = databaseType.Replace("/db:", "")
    LogAndTrace("Database Type", databaseType)
End If

(That's using a local variable... if you want to assign a property , I'd do that within the If block, when you've performed the replacement.) (这使用的是局部变量...如果要分配属性 ,请在执行替换后在If块中执行此操作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM