varying return types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello all,
I want to create a function that returns the first element of the Array
that is input to it. However, the Input Array can be an Array of points,
double, or anyother type, which means the return type of the function depends
on the type of the objects the Input array holds.
I can have the return type as Object, but it has problems like late binding
and more importantly the client has to always Cast it. For example.

class myclass
public shared function getFirstElement(arr as Array) as Object
return arr(1)
end function
end class

//client code
imports myclass.getFirstElement

dim arr() as point = {point(1,2),point(3,4)....}
dim firstPoint as point
firstPoint = directcast(getfirstElement(arr),point)


Has someone got a better solution or another way, so that the last line of
the code is
firstPoint = getfirstElement(arr)

Thanks in Advance.
nafri
 
You could use overloading:

Class arrayElement
Public Shared Function getFirstElement(ByVal arr As Point()) As Point
Return arr(0)
End Function
Public Shared Function getFirstElement(ByVal arr As String()) As String
Return arr(0)
End Function
Public Shared Function getFirstElement(ByVal arr As Double()) As Double
Return arr(0)
End Function
End Class



example a form with a button and 3 radbuttons
(radpoints,raddouble,radstrings) in the form click event use this code

If RadPoints.Checked = True Then
Dim arr() As Point = {New Point(1, 2), New Point(3, 4)}
Dim firstPoint As Point
firstPoint = getFirstElement(arr)
MsgBox(firstPoint.ToString)
End If

If radStrings.Checked = True Then
Dim arr() As String = {"abcde", "defg"}
Dim firstString As String
firstString = getFirstElement(arr)
MsgBox(firstString)
End If

If radDouble.Checked = True Then
Dim arr() As Double = {10, 2, 4, 6}
Dim firstDouble As Double
firstDouble = getFirstElement(arr)
MsgBox(firstDouble.ToString)
End If





hth Peter
 
thanks Peter

nafri


Peter Proost said:
You could use overloading:

Class arrayElement
Public Shared Function getFirstElement(ByVal arr As Point()) As Point
Return arr(0)
End Function
Public Shared Function getFirstElement(ByVal arr As String()) As String
Return arr(0)
End Function
Public Shared Function getFirstElement(ByVal arr As Double()) As Double
Return arr(0)
End Function
End Class



example a form with a button and 3 radbuttons
(radpoints,raddouble,radstrings) in the form click event use this code

If RadPoints.Checked = True Then
Dim arr() As Point = {New Point(1, 2), New Point(3, 4)}
Dim firstPoint As Point
firstPoint = getFirstElement(arr)
MsgBox(firstPoint.ToString)
End If

If radStrings.Checked = True Then
Dim arr() As String = {"abcde", "defg"}
Dim firstString As String
firstString = getFirstElement(arr)
MsgBox(firstString)
End If

If radDouble.Checked = True Then
Dim arr() As Double = {10, 2, 4, 6}
Dim firstDouble As Double
firstDouble = getFirstElement(arr)
MsgBox(firstDouble.ToString)
End If





hth Peter
 
thanks again, Peter... i have another related question.

what if the input parameter is a collection not an array of(points, double
etc).In that case we dont know what is in the collection.

public shared function getFirstElement(col as collection) as ????

return col(0)

For example how can we return the firsteElement of a collection of points or
collection of strings, with the correct datatype.

TIA
 
Before you call your getFirstElement (col as collection) do you know what
you expect to get back from your function?

Grtz Peter
 
yes, The choices of return types that i expect will be either Point type, a
string type or Double type.

public shared function getFirstElement(col as collection) as POINT or DOUBLE
OR STRING
 
almost the same way as before

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
If RadPoints.Checked = True Then
Dim col As New Collection
Dim str As Point
col.Add(New Point(4, 5))
col.Add(New Point(5, 7))
col.Add(New Point(10, 10))
str = getFirstElement(col, New Point(0, 0))
MsgBox(str.ToString)
End If

If radStrings.Checked = True Then
Dim col As New Collection
Dim str As String
col.Add("Hy")
col.Add("Why")
col.Add("Bye")
str = getFirstElement(col, "")
MsgBox(str)
End If

If radDouble.Checked = True Then
Dim col As New Collection
Dim str As Double
col.Add(CDbl(10))
col.Add(CDbl(11))
col.Add(CDbl(12))
str = getFirstElement(col, 0)
MsgBox(str)
End If
End Sub


Class arrayElement
Public Shared Function getFirstElement(ByVal arr As Collection, ByVal
str As _
String) As String
Return DirectCast(arr(1), String)
End Function
Public Shared Function getFirstElement(ByVal arr As Collection, ByVal
dbl As _ Double) As Double
Return DirectCast(arr(1), Double)
End Function
Public Shared Function getFirstElement(ByVal arr As Collection, ByVal pt
As Point) _ As Point
Return DirectCast(arr(1), Point)
End Function
end class

hth Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top