简体   繁体   English

不可发音成员'xyz'不能像方法一样使用,vb和c#之间的区别

[英]Non-invocable member 'xyz' can not be used like a method, Difference between vb and c#

I have this code in VB: 我在VB中有以下代码:

Private mTemmpEmpTimesheetDeclareDetails As IEnumerable(Of 
EmpTimesheetDeclareDetail) = Nothing

If mTemmpEmpTimesheetDeclareDetails.Count > 0 Then
mEmpTimesheetDeclareDetail = mTemmpEmpTimesheetDeclareDetails(0)
End If

Now i have converted the same piece of code in c#: 现在我已经在c#中转换了同一段代码:

private IEnumerable<EmpTimesheetDeclareDetail> mTemmpEmpTimesheetDeclareDetails = null;

if (mTemmpEmpTimesheetDeclareDetails.Count() > 0)
{
  mEmpTimesheetDeclareDetail = (EmpTimesheetDeclareDetail)mTemmpEmpTimesheetDeclareDetails(0);
}

It gives me error here (EmpTimesheetDeclareDetail)mTemmpEmpTimesheetDeclareDetails(0) Non-invocable member can not be used like a method. 它在这里给我错误(EmpTimesheetDeclareDetail)mTemmpEmpTimesheetDeclareDetails(0)不能像方法一样使用不可调用的成员。 Though i understand the error that i am using mTemmpEmpTimesheetDeclareDetails as a method which is incorrect. 虽然我了解使用mTemmpEmpTimesheetDeclareDetails作为不正确方法的错误。 But how does it accepts it in VB? 但是它如何在VB中接受呢? and how can i achieve this in c#? 在C#中如何实现呢?

You've declared your member as an IEnumerable<T> . 您已将成员声明为IEnumerable<T> An IEnumerable<T> does not support indexing, as it's a sequence of values. IEnumerable<T>不支持索引,因为它是一个值序列。 You're looking for ElementAt() . 您正在寻找ElementAt() So, your code would become: 因此,您的代码将变为:

mEmpTimesheetDeclareDetail = mTemmpEmpTimesheetDeclareDetails.ElementAt(0);

Note that you don't need to cast the result, as the result of ElementOf() will be the same as T (in this case, since you're written IEnumerable<EmpTimesheetDeclareDetail> , the result will be EmpTimesheetDeclareDetail ). 请注意,您不需要ElementOf()结果,因为ElementOf()的结果将与T相同(在这种情况下,由于您已编写IEnumerable<EmpTimesheetDeclareDetail> ,因此结果将为EmpTimesheetDeclareDetail )。

In addition, there's a few other things you can tidy up: 此外,您还可以整理其他一些内容:

Use .Any() instead of Count() > 0 . 使用.Any()而不是Count() > 0 Depending on where your sequence comes from, this can prevent the entire sequence from being materialised, it'll only materialise the first item to check if the sequence is not empty. 根据序列的来源,这可以防止实现整个序列,它只会实现第一个项目,以检查序列是否为空。

Also, there exists First and FirstOrDefault() which are a bit clearer than ElementAt(0) . 另外,还有FirstFirstOrDefault() ,它们比ElementAt(0)更加清晰。 The former will throw an exception if there are no elements, the latter will return null in your case. 如果没有元素,前者将引发异常,后者将返回null However, since you've already checked there are elements, First() is more suitable here. 但是,由于您已经检查了元素,因此First()在这里更合适。

mTemmpEmpTimesheetDeclareDetails(0) should be mTemmpEmpTimesheetDeclareDetails.ElementAt(0) mTemmpEmpTimesheetDeclareDetails(0)应该是mTemmpEmpTimesheetDeclareDetails.ElementAt(0)

In c# index should be accessed through a pair of square brackets [] . c#索引应通过一对方括号[]进行访问。 here Brackets are for calling methods. 括号用于调用方法。 So when you use mTemmpEmpTimesheetDeclareDetails(0) , the compiler assumes that you are calling a method named mTemmpEmpTimesheetDeclareDetails with an integer parameter. 因此,当您使用mTemmpEmpTimesheetDeclareDetails(0) ,编译器假定您正在调用带有整数参数的名为mTemmpEmpTimesheetDeclareDetails的方法。

But in your case you are accessing an IEnumerable<T> based on its index, so you have to use the method .ElementAt(index) , Because Enumerable is more generic, and a collection represented by enumerable may not have an indexer 但是在您的情况下,您基于索引访问IEnumerable<T> ,因此您必须使用.ElementAt(index)方法,因为Enumerable更通用,并且由enumerable表示的集合可能没有索引器

暂无
暂无

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

相关问题 c#Flappybird“不能使用不可发音的成员作为方法” - c# Flappybird “Non-invocable member cannot be used like a method” C# 不可调用成员 &#39;ProdhimiQumështit.DataTabel&#39; 不能像方法一样使用 - C# Non-invocable member 'ProdhimiQumështit.DataTabel' cannot be used like a method linq不可召集成员-不能像方法一样使用 - linq non-invocable member - cannot be used like a method 不可调用成员“OperationResult”不能像方法一样使用 - Non-invocable member 'OperationResult' cannot be used like a method 不可发言的成员&#39;&#39;不能像方法一样使用 - Non-invocable member '' cannot be used like a method 不可调用的成员不能像方法一样使用? - Non-invocable member cannot be used like a method? 不可调用的成员不能像方法一样使用 - Non-invocable member cannot be used like a method CS1955 C#不可发音成员&#39;Match.Date&#39;不能像方法一样使用。 我遇到这样的错误,请有人帮我。 - CS1955 C# Non-invocable member 'Match.Date' cannot be used like a method. I'm getting an error like this,can someone help me please. 不可发音的成员&#39;System.Windows.Forms.Control.Text&#39;不能像方法一样使用。 在C#中 - Non-invocable member 'System.Windows.Forms.Control.Text' cannot be used like a method. in c# Visual C#2008:不可使用的成员“ Microsoft.VisualBasic.Devices.Ports.SerialPortNames”不能像方法一样使用 - Visual C# 2008 : Non-invocable member 'Microsoft.VisualBasic.Devices.Ports.SerialPortNames' cannot be used like a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM