通过引用将参数传递到公共共享方法中的问题 [英] Issue with passing parameters by reference into public shared methods

查看:87
本文介绍了通过引用将参数传递到公共共享方法中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AddlInfo的List(Of AddlInfo).

I have a List(Of AddlInfo) with AddlInfo being an object.

我正在尝试通过引用将addlInfoList传递给另一个类的函数:

I'm trying to pass addlInfoList by reference into a function of another class:

Public Shared Sub SortAddlInfo(ByRef addlInfoList As List(Of AddlInfo))
    addlInfoList.Sort(AddressOf Comparer)
End Sub

Private Function Comparer(ByVal x As AddlInfo, ByVal y As AddlInfo) As Integer
    Dim result As Integer = x.AddlInfoType.CompareTo(y.AddlInfoType)
    Return result
End Function

如果我没有将引用传递给另一个类,则此方法有效,但是当我尝试执行此操作时,出现以下错误:

This works if I'm not passing the reference into another class, but when I try to do this, I get the following error:

重载解析失败,因为无法使用以下参数调用可访问的排序":

Overload resolution failed because no accessible 'Sort' can be called with these arguments:

'Public Sub Sort(comparison As System.Comparison(Of AddlInfo))': Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
'Public Sub Sort(comparer As System.Collections.Generic.IComparer(Of AddlInfo))': 'AddressOf' expression cannot be converted to 'System.Collections.Generic.IComparer(Of MyProject.AddlInfo)' because 'System.Collections.Generic.IComparer(Of MyProject.AddlInfo)' is not a delegate type.

我可以将这些方法放回调用类中,但是我希望能够从我的应用程序中的不同类中调用这些方法.

I could put the methods back into the calling class, but I'd like to be able to call these methods from different classes within my application.

我也可以在方法中实例化一个新的List,但是为什么呢?看起来很傻.

I could also instantiate a fresh List in the methods, but why? Seems silly.

有什么办法解决吗? (或者我需要解释更多吗?)

Any way around this? (Or do I need to explain more?)

提前谢谢!

推荐答案

尝试将您的compare函数放入实现IComparer的类中.

Try putting your compare function into a class that implements IComparer.

这篇关于通过引用将参数传递到公共共享方法中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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