方法仅因可选参数错误而不同 [英] Method differs only by optional parameters error
问题描述
Function GetAuthor(entityId As Integer, authorId As Integer, Optional ByVal authorImageWidth As Integer = 250) As String
Function GetAuthor(entityId As Integer, authorId As Integer, feedId As Integer, Optional ByVal authorImageWidth As Integer = 250) As String
我不知道这有什么问题.我收到此错误消息.
I don't know whats wrong with this. I am getting this error message.
'Function GetAuthor(entityId As Integer, authorId As Integer, [authorImageWidth As Integer = 250]) As String'和'Function GetAuthor(entityId As Integer, authorId As Integer, feedId As Integer, [authorImageWidth As Integer = 250]) As String' 不能互相重载,因为它们的区别仅在于可选参数.
'Function GetAuthor(entityId As Integer, authorId As Integer, [authorImageWidth As Integer = 250]) As String' and 'Function GetAuthor(entityId As Integer, authorId As Integer, feedId As Integer, [authorImageWidth As Integer = 250]) As String' cannot overload each other because they differ only by optional parameters.
推荐答案
如果你调用 GetAuthor(int, int, int) 编译器将不知道你想要这两种方法中的哪一种,因为这两种方法都适合(也就是这条消息告诉你什么)
If you call GetAuthor(int, int, int) the compiler will have no clue which of the 2 methods you want since both would be suitable (and that is what this message is telling you)
如果删除第二个方法的可选部分,它将编译,因为GetAuthor(int, int)
和 GetAuthor(int, int, int)
只能解析到你的第一个方法,而 GetAuthor(int, int, int, int)
到您的第二个,从而使重载的参数独一无二
If you remove the optional part of your second method it will compile since
GetAuthor(int, int)
and GetAuthor(int, int, int)
can only resolve to your first method and GetAuthor(int, int, int, int)
to your second thus making the overload unique by it's parameters
这篇关于方法仅因可选参数错误而不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!