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 7 1785
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
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
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
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
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
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 > > >
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 > > > > > > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: chetan.33 |
last post by:
Hi All,
I am looking for means to efficiently convert inputs in varying format
-- say CSV, Excel file to XML format. I have with me the metadata of
the data for input files AND the XSD for the...
|
by: Eidolon |
last post by:
I am looking to have a base class which provides that all inherited
classes MUST have some property, but that they can each define it as
whatever type they want. So maybe three class inherit from...
|
by: fripper |
last post by:
I have had some experience with VB .Net but I am having difficulty coming
to grips with how to set up a subroutine to accept varying classes for its
arguments. An example will help to explain. I...
|
by: Mike Leahy |
last post by:
Okay.I'm following the documentation that came with the PostgreSQL source
code (located in /usr/doc/postgresql-7.3.4-2/html/arrays.hmtl in my cygwin
root).
I created have a table with a varchar...
|
by: Michael . |
last post by:
I had an error before involving a temporary table, and
that has been taken care of...
The last message I wrote where it seemed to have
needed it after I added it was because of different...
| |
by: dima |
last post by:
Hello All!
Does anybody know anything about type-safe implementation of functions
with varying number of arguments?
I know one old solution - elipsis (stdarg,h). But it is NOT type-safe!
...
|
by: louis_la_brocante |
last post by:
Dear all,
I am having trouble generating a client proxy for a webservice whose
methods return a "complex" type. The type is complex in that it is a
class whose members are a mix of primitive...
|
by: miked |
last post by:
Why are there still no covariant return types?
All searches reveal no workarounds accept for using an interface which is a
real pain. I wind up missing this capability almost every time I...
|
by: Confused Newbie |
last post by:
I'm converting an app written in VB 2003 to 2005 and need advice for how to
deal with this situation:
The app has a number of "manager" classes that handle the data access. They
all have...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |