Hello,
how do I convert types? Something like the following, but it is not possible
in Java:
Object objObject = (String) "No integer";
int intTemp;
try
{
intTemp = (int) objObject; // Suppose to cause exception....(this line
doesn't compile)
}
catch
{
intTemp = 0;
}
Basicly, I have an object which can contain any type and a set of functions
that must convert it to a specific type. If the conversion is not possible,
the functions must return a default value,
thanks for any info.
sreekanth.ramakrishnan@gmail.com - 22 Apr 2006 17:44 GMT
Hi
Why dont you work with reflections? You can retrive the class objects
with it, check for the particular type and stuff.
More over reflections are known to have serious performance drawbacks.
There are other options for doing the same work. But reflections api
comes with the java sdk.
http://java.sun.com/docs/books/tutorial/reflect/class/getClass.html
Patricia Shanahan - 22 Apr 2006 17:52 GMT
> Hello,
>
[quoted text clipped - 19 lines]
>
> thanks for any info.
Well, what you wrote is very close to one of the ways of doing it in
Java, going ahead with the conversion and catching the
ClassCastException if it fails.
However, I would do it with a precheck:
if(objObject instanceof Integer){
return ((Integer)objObject).intValue();
}else{
return 0;
}
Patricia
qwert - 22 Apr 2006 19:32 GMT
> Well, what you wrote is very close to one of the ways of doing it in
> Java, going ahead with the conversion and catching the
> ClassCastException if it fails.
Very close, but not close enough. It doesn't compile. Compiler says
"inconvertible types" when I do the following:
Object objObject;
int intTemp = (int) objObject;
> However, I would do it with a precheck:
>
[quoted text clipped - 3 lines]
> return 0;
> }
This code doesn't work either. Is it pseudo-code?
Thanks.
Patricia Shanahan - 22 Apr 2006 19:44 GMT
>>Well, what you wrote is very close to one of the ways of doing it in
>>Java, going ahead with the conversion and catching the
[quoted text clipped - 5 lines]
> Object objObject;
> int intTemp = (int) objObject;
You need to think through what classes of objects you want to convert to
integer, and what that means in each case. As in the code I posted, you
can get the int equivalent of an Integer by calling its intValue() method.
>>However, I would do it with a precheck:
>>
[quoted text clipped - 7 lines]
>
> Thanks.
It is working code doing my best guess at what you are trying to do. Of
course, you will need to modify it to fit how you want to use it. As
written, I'm assuming it fits in a conversion method that returns int.
Here is my test program:
public class TestConvert {
public static void main(String[] args) {
System.out.println(convertToInt(new Integer(33)));
System.out.println(convertToInt("xxx"));
}
static int convertToInt(Object objObject){
if(objObject instanceof Integer){
return ((Integer)objObject).intValue();
}else{
return 0;
}
}
}
Patricia
qwert - 22 Apr 2006 20:34 GMT
Heya,
Aah. Your thing is starting to work (new to Java...):
if( objInteger intstanceof String )
{
return Integer.parseInt((String)objInteger);
}
else
{
return ((Integer) objInteger.intValue();
}
...
catch
return 0;
Not sure what happens with other types like long's. But have a starting
point now. Thanks everyone for help and patience.
> >>Well, what you wrote is very close to one of the ways of doing it in
> >>Java, going ahead with the conversion and catching the
[quoted text clipped - 42 lines]
>
> Patricia
Dimitri Maziuk - 22 Apr 2006 19:23 GMT
qwert sez:
> Hello,
>
[quoted text clipped - 13 lines]
> intTemp = 0;
> }
You're implying that it's possible in some language other than Java.
I'm curious: what language is that?
Dima

Signature
"Mirrors and copulation are abominable because they increase the number of
entities." -- corollary to Occam's Razor
Stefan Ram - 22 Apr 2006 20:31 GMT
>You're implying that it's possible in some language other than
>Java. I'm curious: what language is that?
I guess VBA does not count as a language, nevertheless:
Sub Test()
Dim objObject As Variant
objObject = "No integer"
Dim intTemp As Integer
On Error GoTo Handler
intTemp = objObject
GoTo Over
Handler:
Debug.Print "caught"
intTemp = 0
GoTo Over
Over:
Debug.Print intTemp
End Sub
prints
caught
0
Using »objObject = "22"« in the third line will make it print
22
Dimitri Maziuk - 23 Apr 2006 04:14 GMT
Stefan Ram sez:
>>You're implying that it's possible in some language other than
>>Java. I'm curious: what language is that?
>
> I guess VBA does not count as a language, nevertheless:
Yeah, but then you might know that VB isn't exactly a statically
typed language... OP clearly does not.
Dima

Signature
I like the US government, makes the Aussie one look less dumb and THAT is a
pretty big effort. -- Craig Small
Chris Uppal - 23 Apr 2006 10:35 GMT
> Basicly, I have an object which can contain any type and a set of
> functions that must convert it to a specific type. If the conversion is
> not possible, the functions must return a default value,
I think it's unlikely that you have a valid need for this unless you are
writing an implementation of a different language (one with a different type
system). I suggest you review your design and see /why/ you need it, and
whether there is a better way of doing things.
If you want to, or have to, stick with this approach then the only way you can
do it, is by defining a special kind of object which contains the data in one
(or more) common forms, and converts as necessary. E.g.
class Variant
{
private String m_value;
public void set(String s) { m_value = s; }
public String get() { return m_value; }
public void set(int i) { m_value = Integer.toString(i); }
public int getInt() { return Integer.parseInt(m_value); }
public void set(double d) { m_value = Double.toString(i); }
public int getDouble() { return Double.parseDouble(m_value); }
// etc...
}
-- chris