Functions that return different datatypes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Functions that return different datatypes

  1. #1
    Dennis Guest

    Functions that return different datatypes

    Since .Net doesn't allow variants, how do you create a function that can
    return different datatypes? Say we have a function that returns either a
    recordset or an XML string based on a parameter, so it has to use Variant
    for it's return type. Can we use this funciton in .Net somehow? Or would
    we have to separate it into two functions?

    Thanks,
    Dennis



  2. #2
    Jonathan Wood Guest

    Re: Functions that return different datatypes

    How about returning an Object. Since all objects derive from Object, that
    should work okay.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
    "Dennis" <dbronstein@brainranger.com> wrote in message
    news:3a8c21a5@news.devx.com...
    > Since .Net doesn't allow variants, how do you create a function that can
    > return different datatypes? Say we have a function that returns either a
    > recordset or an XML string based on a parameter, so it has to use Variant
    > for it's return type. Can we use this funciton in .Net somehow? Or would
    > we have to separate it into two functions?
    >
    > Thanks,
    > Dennis
    >
    >




  3. #3
    Kenny Acock Guest

    Re: Functions that return different datatypes


    Dennis,

    >Since .Net doesn't allow variants, how do you create a function that can
    >return different datatypes? Say we have a function that returns either

    a
    >recordset or an XML string based on a parameter, so it has to use Variant
    >for it's return type. Can we use this funciton in .Net somehow? Or would
    >we have to separate it into two functions?


    That is not entirely correct. In VB.Net you can think of Objects as SuperVariants.
    They do everything a VB 6 Variant could do (AFAIK). They aren't recommended
    unless you really need them, but they work.

    --Kenny

  4. #4
    Dennis Guest

    Re: Functions that return different datatypes

    So how can we put a string into an object today?

    Thanks,
    Dennis

    "Kenny Acock" <kennya@teacherspal.com> wrote in message
    news:3a8c3165$2@news.devx.com...
    >
    > Dennis,
    >
    > >Since .Net doesn't allow variants, how do you create a function that can
    > >return different datatypes? Say we have a function that returns either

    > a
    > >recordset or an XML string based on a parameter, so it has to use Variant
    > >for it's return type. Can we use this funciton in .Net somehow? Or

    would
    > >we have to separate it into two functions?

    >
    > That is not entirely correct. In VB.Net you can think of Objects as

    SuperVariants.
    > They do everything a VB 6 Variant could do (AFAIK). They aren't

    recommended
    > unless you really need them, but they work.
    >
    > --Kenny




  5. #5
    Kevin P. Fleming Guest

    Re: Functions that return different datatypes

    In .NET, a string _IS_ an Object. You can assign any string to any generic
    Object variable.

    "Dennis" <dbronstein@brainranger.com> wrote in message
    news:3a8c544b$1@news.devx.com...
    > So how can we put a string into an object today?
    >
    > Thanks,
    > Dennis
    >
    > "Kenny Acock" <kennya@teacherspal.com> wrote in message
    > news:3a8c3165$2@news.devx.com...
    > >
    > > Dennis,
    > >
    > > >Since .Net doesn't allow variants, how do you create a function that

    can
    > > >return different datatypes? Say we have a function that returns either

    > > a
    > > >recordset or an XML string based on a parameter, so it has to use

    Variant
    > > >for it's return type. Can we use this funciton in .Net somehow? Or

    > would
    > > >we have to separate it into two functions?

    > >
    > > That is not entirely correct. In VB.Net you can think of Objects as

    > SuperVariants.
    > > They do everything a VB 6 Variant could do (AFAIK). They aren't

    > recommended
    > > unless you really need them, but they work.
    > >
    > > --Kenny

    >
    >




  6. #6
    J W Cooper Guest

    Re: Functions that return different datatypes

    Why not just overload the function so that there are two kinds of arguments
    and two kinds of return types? Its far better programming practice and avoicd
    unpleasant type cast failures.

    Function getRecord(a as integer, b as integer) as RecordSet

    Function getRecord(a as integer) as String

    Jim Cooper

    Dennis wrote:

    > Since .Net doesn't allow variants, how do you create a function that can
    > return different datatypes? Say we have a function that returns either a
    > recordset or an XML string based on a parameter, so it has to use Variant
    > for it's return type. Can we use this funciton in .Net somehow? Or would
    > we have to separate it into two functions?
    >
    > Thanks,
    > Dennis



  7. #7
    Dennis Guest

    Re: Functions that return different datatypes

    Again, how can we do that _today_, because a string is not an object in VB6?
    Or are we going to have to change all the functions when we convert?

    Dennis

    "Kevin P. Fleming" <kevin@spam-labsysgrp-spam.com> wrote in message
    news:96hne2$crj$1@localhost.localdomain...
    > In .NET, a string _IS_ an Object. You can assign any string to any generic
    > Object variable.
    >
    > "Dennis" <dbronstein@brainranger.com> wrote in message
    > news:3a8c544b$1@news.devx.com...
    > > So how can we put a string into an object today?
    > >
    > > Thanks,
    > > Dennis
    > >
    > > "Kenny Acock" <kennya@teacherspal.com> wrote in message
    > > news:3a8c3165$2@news.devx.com...
    > > >
    > > > Dennis,
    > > >
    > > > >Since .Net doesn't allow variants, how do you create a function that

    > can
    > > > >return different datatypes? Say we have a function that returns

    either
    > > > a
    > > > >recordset or an XML string based on a parameter, so it has to use

    > Variant
    > > > >for it's return type. Can we use this funciton in .Net somehow? Or

    > > would
    > > > >we have to separate it into two functions?
    > > >
    > > > That is not entirely correct. In VB.Net you can think of Objects as

    > > SuperVariants.
    > > > They do everything a VB 6 Variant could do (AFAIK). They aren't

    > > recommended
    > > > unless you really need them, but they work.
    > > >
    > > > --Kenny

    > >
    > >

    >
    >




  8. #8
    Dennis Guest

    Re: Functions that return different datatypes

    We can't do that today, can we? And then we'd have a different set of
    parameters for each, which we don't need.

    Dennis

    "J W Cooper" <ctswim@ctswim.org> wrote in message
    news:3A8C9A87.F6E71BBA@ctswim.org...
    > Why not just overload the function so that there are two kinds of

    arguments
    > and two kinds of return types? Its far better programming practice and

    avoicd
    > unpleasant type cast failures.
    >
    > Function getRecord(a as integer, b as integer) as RecordSet
    >
    > Function getRecord(a as integer) as String
    >
    > Jim Cooper
    >
    > Dennis wrote:
    >
    > > Since .Net doesn't allow variants, how do you create a function that can
    > > return different datatypes? Say we have a function that returns either

    a
    > > recordset or an XML string based on a parameter, so it has to use

    Variant
    > > for it's return type. Can we use this funciton in .Net somehow? Or

    would
    > > we have to separate it into two functions?
    > >
    > > Thanks,
    > > Dennis

    >




  9. #9
    Dan Fergus Guest

    Re: Functions that return different datatypes

    No, you cannot do that today. I don't know of anyway that you can write
    code, to do what you are asking, now in VB6 so that it will move smoothly
    into VB.NET.

    My own opinion, you will have a hard time moving anything except the most
    basic code to VB.NET without major work.

    Make sure that you comment well now in your VB6 code so that you can find
    these problem spots later. I have even gone the the extreme of writing VB6
    and VB.NET code next to each other in my VB6 code and commentoing out the
    VB.NET code. When I port, I know exactly what I have to do.

    Dan

    "Dennis" <dbronstein@brainranger.com> wrote in message
    news:3a8d58f9$1@news.devx.com...
    > We can't do that today, can we? And then we'd have a different set of
    > parameters for each, which we don't need.
    >
    > Dennis
    >
    > "J W Cooper" <ctswim@ctswim.org> wrote in message
    > news:3A8C9A87.F6E71BBA@ctswim.org...
    > > Why not just overload the function so that there are two kinds of

    > arguments
    > > and two kinds of return types? Its far better programming practice and

    > avoicd
    > > unpleasant type cast failures.
    > >
    > > Function getRecord(a as integer, b as integer) as RecordSet
    > >
    > > Function getRecord(a as integer) as String
    > >
    > > Jim Cooper
    > >
    > > Dennis wrote:
    > >
    > > > Since .Net doesn't allow variants, how do you create a function that

    can
    > > > return different datatypes? Say we have a function that returns

    either
    > a
    > > > recordset or an XML string based on a parameter, so it has to use

    > Variant
    > > > for it's return type. Can we use this funciton in .Net somehow? Or

    > would
    > > > we have to separate it into two functions?
    > > >
    > > > Thanks,
    > > > Dennis

    > >

    >
    >




  10. #10
    Kenny Acock Guest

    Re: Functions that return different datatypes


    Dennis,

    >Again, how can we do that _today_, because a string is not an object in

    VB6?
    >Or are we going to have to change all the functions when we convert?


    VB.Net will automatically convert Variants to Objects. From a design standpoint,
    however, I would limit the use of Variants.

    --Kenny

  11. #11
    Dennis Guest

    Re: Functions that return different datatypes

    Sure we can. We can just have the function return a variant. The problem
    is we want something we can use today that will convert easily to .Net.

    "Dan Fergus" <dan@vbforest.com> wrote in message
    news:3a8d5dd0$1@news.devx.com...
    > No, you cannot do that today. I don't know of anyway that you can write
    > code, to do what you are asking, now in VB6 so that it will move smoothly
    > into VB.NET.
    >
    > My own opinion, you will have a hard time moving anything except the most
    > basic code to VB.NET without major work.
    >
    > Make sure that you comment well now in your VB6 code so that you can find
    > these problem spots later. I have even gone the the extreme of writing

    VB6
    > and VB.NET code next to each other in my VB6 code and commentoing out the
    > VB.NET code. When I port, I know exactly what I have to do.
    >
    > Dan
    >
    > "Dennis" <dbronstein@brainranger.com> wrote in message
    > news:3a8d58f9$1@news.devx.com...
    > > We can't do that today, can we? And then we'd have a different set of
    > > parameters for each, which we don't need.
    > >
    > > Dennis
    > >
    > > "J W Cooper" <ctswim@ctswim.org> wrote in message
    > > news:3A8C9A87.F6E71BBA@ctswim.org...
    > > > Why not just overload the function so that there are two kinds of

    > > arguments
    > > > and two kinds of return types? Its far better programming practice

    and
    > > avoicd
    > > > unpleasant type cast failures.
    > > >
    > > > Function getRecord(a as integer, b as integer) as RecordSet
    > > >
    > > > Function getRecord(a as integer) as String
    > > >
    > > > Jim Cooper
    > > >
    > > > Dennis wrote:
    > > >
    > > > > Since .Net doesn't allow variants, how do you create a function that

    > can
    > > > > return different datatypes? Say we have a function that returns

    > either
    > > a
    > > > > recordset or an XML string based on a parameter, so it has to use

    > > Variant
    > > > > for it's return type. Can we use this funciton in .Net somehow? Or

    > > would
    > > > > we have to separate it into two functions?
    > > > >
    > > > > Thanks,
    > > > > Dennis
    > > >

    > >
    > >

    >
    >




  12. #12
    Kenny Acock Guest

    Re: Functions that return different datatypes


    Dennis,

    >Sure we can. We can just have the function return a variant. The problem
    >is we want something we can use today that will convert easily to .Net.


    Your problem will actually be with the Recordset. VB.Net has replaced it
    with something else. (I forgot the exact name.) If you use a Variant, VB.Net
    has no information on which to do a conversion. You can use a variant to
    pass data, but be sure not to access the variant as a recordset directly:

    VariantVariable.SomeMethod <---Not a good idea (Even if it works)

    (VB.Net will *not* know how to handle the method because it is late bound.)

    Do this instead:

    Dim rs As ADODB.Recordset
    set rs = VariantVariable
    rs.SomeMethod

    (VB.Net *will* know how to handle the method because it is early bound.)
    When VB.Net converts the project, the Variant will become an Object, which
    will be able to do what VB6 Variants and VB6 Objects can do. (AFAIK)

    --Kenny

  13. #13
    Tim Romano Guest

    Re: Functions that return different datatypes

    It would be cleaner to separate these functions into separate methods of a
    VB6 class:

    myXMLString = myClass.GetXML( )
    Set myRecordset = myClass.GetRS( )

    Tim Romano



    "Dennis" <dbronstein@brainranger.com> wrote in message
    news:3a8c21a5@news.devx.com...
    > Since .Net doesn't allow variants, how do you create a function that can
    > return different datatypes? Say we have a function that returns either a
    > recordset or an XML string based on a parameter, so it has to use Variant
    > for it's return type. Can we use this funciton in .Net somehow? Or would
    > we have to separate it into two functions?
    >
    > Thanks,
    > Dennis
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center