如何在 VB.NET 中编写全部使用相同核心代码的重载函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15054880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write overloaded functions in VB.NET that will all use the same core code?
提问by Simon White
I want to write two overloaded functions in VB.NET.
我想在 VB.NET 中编写两个重载函数。
Most of the logic in the two functions will be the same so I don't want to just duplicate the entire function for the two overloads.
这两个函数中的大部分逻辑都是相同的,所以我不想只为两个重载复制整个函数。
I could achieve this by having each overloaded function call another function (that contains the core logic) with optional parameters, like this:
我可以通过让每个重载函数调用另一个带有可选参数的函数(包含核心逻辑)来实现这一点,如下所示:
Public Overloads Function GetLocationDetails(ByVal countryId As Integer) As LocationInfo
Return _GetLocationDetails(countryId)
End Function
Public Overloads Function GetLocationDetails(ByVal countryId As Integer, ByVal stateId As Integer) As LocationInfo
Return _GetLocationDetails(countryId, stateId)
End Function
' This is the function providing the core logic for the two overloaded functions
Private Function _GetLocationDetails(ByVal countryId As Integer, Optional ByVal stateId As Integer = 0) As LocationInfo
Dim returnVal As New LocationInfo
Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)
Using cmd As SqlCommand = con.CreateCommand
cmd.CommandText = "SELECT name, population FROM locations WHERE countryId = @countryId"
cmd.Parameters.Add(New SqlParameter("@countryId", countryId))
' If there is a stateId, this function was called by the overloaded function that has a stateId parameter, so add that to the query
If stateId <> 0 Then
cmd.CommandText &= " AND stateId = @stateId"
cmd.Parameters.Add(New SqlParameter("@stateId", stateId))
End If
con.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
If dr.Read Then
returnVal.Name = dr("name")
returnVal.Population = dr("population")
End If
End Using
End Using
End Using
Return returnVal
End Function
Public Class LocationInfo
Public Name As String
Public Population As Integer
End Class
But using optional parameters doesn't seem very elegant and seems to negate the whole purpose of overloading the functions in the first place.
但是使用可选参数似乎不是很优雅,而且似乎首先否定了重载函数的整个目的。
Is there a better way?
有没有更好的办法?
回答by Eugen Rieck
There are two things to mention:
有两点需要说明:
First: The function with optional parameters is private, while the fixed-parameter versions are public. This is a huge difference
第一:带可选参数的函数是私有的,而固定参数的版本是公共的。这是一个巨大的差异
Second: You could make stateIdin _GetLocationDetailsmandatory and use
第二:你可以做stateId的_GetLocationDetails强制性和使用
Public Overloads Function GetLocationDetails(ByVal countryId As Integer) As LocationInfo
Return _GetLocationDetails(countryId,0)
End Function
回答by Derek Tomes
I don't see what you're accomplishing with this code. You may as well just get rid of the two public functions and change your private function to public:
我看不出你用这段代码完成了什么。你也可以去掉这两个公共函数,把你的私有函数改成公共函数:
Public Function _GetLocationDetails(ByVal countryId As Integer, _
Optional ByVal stateId As Integer = 0) _
As LocationInfo

