By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

varying return types

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"nafri" <sp****@spam.net> wrote in message
news:A6**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
thanks Peter

nafri
"Peter Proost" wrote:
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

"nafri" <sp****@spam.net> wrote in message
news:A6**********************************@microsof t.com...
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


Nov 21 '05 #3

P: n/a

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

"Peter Proost" wrote:
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

"nafri" <sp****@spam.net> wrote in message
news:A6**********************************@microsof t.com...
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


Nov 21 '05 #4

P: n/a
Before you call your getFirstElement (col as collection) do you know what
you expect to get back from your function?

Grtz Peter

"nafri" <sp****@spam.net> wrote in message
news:25**********************************@microsof t.com...

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

"Peter Proost" wrote:
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

"nafri" <sp****@spam.net> wrote in message
news:A6**********************************@microsof t.com...
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


Nov 21 '05 #5

P: n/a
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

"Peter Proost" wrote:
Before you call your getFirstElement (col as collection) do you know what
you expect to get back from your function?

Grtz Peter

"nafri" <sp****@spam.net> wrote in message
news:25**********************************@microsof t.com...

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

"Peter Proost" wrote:
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

"nafri" <sp****@spam.net> wrote in message
news:A6**********************************@microsof t.com...
> 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


Nov 21 '05 #6

P: n/a
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
"nafri" <sp****@spam.net> wrote in message
news:21**********************************@microsof t.com...
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

"Peter Proost" wrote:
Before you call your getFirstElement (col as collection) do you know what you expect to get back from your function?

Grtz Peter

"nafri" <sp****@spam.net> wrote in message
news:25**********************************@microsof t.com...

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

"Peter Proost" wrote:

> 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
>
> "nafri" <sp****@spam.net> wrote in message
> news:A6**********************************@microsof t.com...
> > 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
>
>
>


Nov 21 '05 #7

P: n/a
thanks peter, i am sorted as well as got some xtra knowledge.

nafri

"Peter Proost" wrote:
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
"nafri" <sp****@spam.net> wrote in message
news:21**********************************@microsof t.com...
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

"Peter Proost" wrote:
Before you call your getFirstElement (col as collection) do you know what you expect to get back from your function?

Grtz Peter

"nafri" <sp****@spam.net> wrote in message
news:25**********************************@microsof t.com...
>
> 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
>
> "Peter Proost" wrote:
>
> > 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
> >
> > "nafri" <sp****@spam.net> wrote in message
> > news:A6**********************************@microsof t.com...
> > > 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
> >
> >
> >


Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.