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
05-10-2000, 08:59 AM
Jason Bock
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
05-10-2000, 11:08 AM
Paul Clapham
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.
05-10-2000, 11:13 AM
Tom Duffy
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
>
>
>
05-17-2000, 12:18 PM
Gary
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
>
>