"Variants" in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: "Variants" in Java

  1. #1
    Matthew Cromer Guest

    "Variants" in Java

    I'm wondering if Java offers a good way to simulate the Visual Basic / COM
    Variant type. I don't suppose it is possible to have multiple return types
    from a single named function, is it.

    I've got some Visual Basic architecture that uses variants as return types,
    typically for database columns in generic database classes where you don't
    know ahead of time whether the column will contain a string, an integer
    type, a date, etc.
    --
    Matthew Cromer
    President, SDA Consulting, Inc.
    matthew@sdaconsulting.com
    http://www.sdaconsulting.com/
    (919) 274-0074




  2. #2
    Jason Bock Guest

    Re: "Variants" in Java

    "Matthew Cromer" <matthew_cromer@iname.com> wrote in message
    news:B53E332B.C28D%matthew_cromer@iname.com...
    > I'm wondering if Java offers a good way to simulate the Visual Basic / COM
    > Variant type. I don't suppose it is possible to have multiple return

    types
    > from a single named function, is it.


    Not that I know of, although it isn't too difficult to create a Variant
    class that simulates what you get in COM. You have a constructor for each
    of the primitives along with the Object type, and then you have a bunch of
    toXXX() methods like this:

    public class Variant
    {
    private String internalValue;

    public void Variant(int baseValue)
    {
    internalValue = Integer.toString(baseValue);
    }

    public void Variant(long baseValue)
    {
    internalValue = Long.toString(baseValue);
    }

    public void Variant(Object baseValue)
    {
    internalValue = baseValue;
    }
    // And so on...

    public long toLong()
    {
    return Long.getLong(internalValue);
    }

    public int toInt()
    {
    return Integer.getInteger(internalValue);
    }

    public Object toObject()
    {
    return internalValue;
    }
    // And so on...
    }

    You could also add resetter methods if you want.

    Regards,

    Jason



  3. #3
    Paul Clapham Guest

    Re: "Variants" in Java


    Matthew Cromer <matthew_cromer@iname.com> wrote in message
    news:B53E332B.C28D%matthew_cromer@iname.com...
    > I'm wondering if Java offers a good way to simulate the Visual Basic / COM
    > Variant type. I don't suppose it is possible to have multiple return

    types
    > from a single named function, is it.
    >
    > I've got some Visual Basic architecture that uses variants as return

    types,
    > typically for database columns in generic database classes where you don't
    > know ahead of time whether the column will contain a string, an integer
    > type, a date, etc.
    > --


    Have your named function return an Object, but inside it have code like

    return new String("I'm a string.");

    Then the code that calls it can inspect the Object to find out what class it
    actually belongs to.



  4. #4
    Tom Duffy Guest

    Re: "Variants" in Java


    Hello Matthew:

    You can use the database MetaData classes to determine which datatype an
    individual column holds.

    Tom Duffy

    Matthew Cromer <matthew_cromer@iname.com> wrote:
    >I'm wondering if Java offers a good way to simulate the Visual Basic / COM
    >Variant type. I don't suppose it is possible to have multiple return types
    >from a single named function, is it.
    >
    >I've got some Visual Basic architecture that uses variants as return types,
    >typically for database columns in generic database classes where you don't
    >know ahead of time whether the column will contain a string, an integer
    >type, a date, etc.
    >--
    >Matthew Cromer
    >President, SDA Consulting, Inc.
    >matthew@sdaconsulting.com
    >http://www.sdaconsulting.com/
    >(919) 274-0074
    >
    >
    >



  5. #5
    Gary Guest

    Re: "Variants" in Java


    Jason, I thought that understood your technique, but after further review,
    I am confused. Your first line in the "Variant" class is 'private String
    internal Value;' Your last constructor is 'public Object toObject' which
    returns 'internalValue'. How does this accomplish sending back an object
    when it was declared as a String?

    Thanks,
    Gary

    "Jason Bock" <jrbock@execpc.com> wrote:
    >"Matthew Cromer" <matthew_cromer@iname.com> wrote in message
    >news:B53E332B.C28D%matthew_cromer@iname.com...
    >> I'm wondering if Java offers a good way to simulate the Visual Basic /

    COM
    >> Variant type. I don't suppose it is possible to have multiple return

    >types
    >> from a single named function, is it.

    >
    >Not that I know of, although it isn't too difficult to create a Variant
    >class that simulates what you get in COM. You have a constructor for each
    >of the primitives along with the Object type, and then you have a bunch

    of
    >toXXX() methods like this:
    >
    >public class Variant
    >{
    > private String internalValue;
    >
    > public void Variant(int baseValue)
    > {
    > internalValue = Integer.toString(baseValue);
    > }
    >
    > public void Variant(long baseValue)
    > {
    > internalValue = Long.toString(baseValue);
    > }
    >
    > public void Variant(Object baseValue)
    > {
    > internalValue = baseValue;
    > }
    > // And so on...
    >
    > public long toLong()
    > {
    > return Long.getLong(internalValue);
    > }
    >
    > public int toInt()
    > {
    > return Integer.getInteger(internalValue);
    > }
    >
    > public Object toObject()
    > {
    > return internalValue;
    > }
    > // And so on...
    >}
    >
    >You could also add resetter methods if you want.
    >
    >Regards,
    >
    >Jason
    >
    >



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