Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

dynamic object identification problem

ok, I have a class that takes 3 inputs.
Variable(NAME, TYPE, DATA);
the name is a string, the type is a string, however the DATA is where it gets tricky.

DATA depends on what the string TYPE is. Example
if TYPE is a String
DATA isa String
if TYPE is a VectorObject
DATA isa VectorObject

you get the idea right?
Now, here's the problem. I don't know what to identify DATA as. It's type is determined at runtime, not compiletime, so I don't know what to define it as, since a string cannot be coverted into VectorObject, plus i can't keep redefining DATA.

Example:
if TYPE isa VectorObject
DATA isa VectorObject
would be shown as VectorObject DATA = new VectorObject();
the problem arises when i try to store or return the DATA object!

public <omg i dont know what identifier goes here, b/c i don't know the type yet> GetData { return DATA;}
So any ideas?
There's got to be a better way to do this or something small that im missing.
I'm not sure if i could use an interface for this situation, any suggestions?
This is for a Java game scripting system im developing.
[1207 byte] By [clownie] at [2007-11-11 7:33:35]
# 1 Re: dynamic object identification problem
Read up on Java's implementation of Generics. It fits the bill you want to implement.

Variable(String name, T type, T data)
{...}

public <T> T getData()
{
return DATA;
}

Here's a link to the "official" Generics tutorial, in pdf format:

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
nspils at 2007-11-11 22:38:02 >