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;}

There's got to be a better way to do this or something small that im missing.
like an interface.. but i dont know how to implment the interface if so...
[1121 byte] By [clownie] at [2007-11-11 7:33:37]
# 1 Re: dynamic object identification problem
Desfine data as Object

if (data instanceof String)
String s = (String)data;
else if(data instanceof Vector)
Vector v = (Vector)data;

Not sure what you're wanting to do here, but it doesn't sound like a very good way of doing it. I could help you more if you tell me how those objects are going to be used.
Phaelax at 2007-11-11 22:37:54 >
# 2 Re: dynamic object identification problem
here's the problem tho, when i go to define data it keep *****ing for an identifier!
like (keep in mind im a c programmer) i cant do
public void data;
and if i give it a type when i create data variable i have to cast, (which i said i can't do with vector)
So basically i'm really asking how do i make a variable with no type, then GIVE it a type later...

this is for ascripting system.
I read in a file with:

varName varType varData

then i pass those values to Variable class to store them.
I make an arraylist of variable objects to keep track of all the variables in the file.

but in the Variable(name,type,data) class i need to establish the correct data values.
If the type in the file says "vector" or"float" i know the value is a vector or float.
However when i declare a variable "Data" i don't know what type (int,string,vector,float) to make it when i declare it.
I'd have to make it return a string constantly, then parse the string to see if it's the correct type...

So i eithr have to keep creating different instances of a varibale data or just make one instance and have it a void type which i thought id be able to cast or change into the needed type later...

also a problem arises when i attempt to return the varData value. Since i wont know what type it is at compile time i wont know what to return!
would i do as you said, and justdo

if data instanceOf vector
return vectored data;
if data instanceOf float
return floated data;

but, how do i make data a float or vector dynamically like that(since a string or other type wont cast correctly to a vector...)?
and how do i know what to make the return type for the method that returns the data variable value.

public String GetData(){}//isn't right if data is a vector, b/c it should return a vector object... not a string.
public void GetData(){} //wont let me return something... or will it?
is that slightly more clear? if you'd like to see the classes, please let me know.
Basically is there a way to make a variable without a type at compile time? then just have it "casted" (for lack of a better term) at runtime with the proper datatype?
clownie at 2007-11-11 22:38:59 >
# 3 Re: dynamic object identification problem
script class

import java.io.*;
import java.util.ArrayList;
public class Script
{
public ArrayList<Variable> variables = new ArrayList<Variable>();//make this array list global? or just return it?

public Script(String fileName, String path)
{
if (fileName==null)
return;

String line;
String[] lineSplit;

//ArrayList file = new ArrayList(); -- not needed? use normal string array instead?
try
{
boolean read=false;//true when you should start reading for variables. false when encounters #end or doesn't find #begin
BufferedReader in = new BufferedReader(new FileReader(fileName));

if(!in.ready()) throw new IOException();
//MAIN SCRIPT READING&PROCESSING HERE
while(in.readLine() != null)
{
line=in.readLine();
//split up line into tokens
lineSplit = line.split(" ");
//before for loops so next line gets read in by while loop, since after a #begin the next line must be a variable declaration
if(read==true)
{
if(lineSplit[0].equals("#end"))
read=false;
else
{
variables.add(new Variable(lineSplit[0],lineSplit[1],lineSplit[2]));//name, type, value or just pass lineSplit
System.out.println("Added new Variable: "+lineSplit[0]+" "+lineSplit[1]+" "+lineSplit[2]);
}
/*Variable() is going to need to be able to differentiate b/t types and then alter it's own
*data based on its type.
*Example:
*if type is Vector 3D
*split up the value into it's 3 xyz components.
*store the new value as a Vector3D object.
*varvalue=new Vector3D(x,y,z); or
*varvalue=new Vector3D;
*varvalue.x=x;
*varvalue.y=y;
*varvalue.z=z;
*you get the idea.
*/
}//endif

//check token for beginning of variables -- #begin and #end should be on own line to simplify reading. Can take out the for loops and just put 0 in place of i.
for(int i=0; i<lineSplit.length;i++)
if(lineSplit[i].equals("#begin"))
read=true;

for(int i=0; i<lineSplit.length;i++)
if(lineSplit[i].equals("#end"))
read=false;

line=null;
}//endwhile

in.close();
}catch(IOException e)
{
System.out.println(e);
return;
}


}

/* void addVariable(){}
void setVariable(){} //return a variable's value with specified name

boolean getBoolData(){}
float getFloatData(){}
Color getColorData(){}
Vector3D getVectorData(){}
String getStringData(){}
double getNumberData(){}
*/
}

variable class

public class Variable
{
private String varname; //Name of variable.
private String vartype; //type of value (float,bool, etc).
private String varvalue; //Data stored in variable.

public Variable(String name, String type, String value)
{
varname=name;
//check types
if(type.equals("bool"))
{
//variable is a boolean
vartype=type;
boolean varvalue=Boolean.parseBoolean(value);
//check to see if true or false
}
else if(type.equals("float"))
{
vartype=type;
float varvalue=Float.parseFloat(value);

}
else if(type.equals("string"))
{
vartype=type;
varvalue=value;
}
else if(type.equals("color"))
{
//take care of color Color3D (r,g,b,Alpha)
}
else if(type.equals("vector"))
{
//take care of Vector3D split the value with commma as delimiter
}
else if(type.equals("point"))
{
//take care of Point3D split value with comma as delimeter
}
else //type=unknown... so ignore it...
{

}

}
public String GetType(){ return vartype;}
public String GetName(){ return varname;}
public String GetStringData(){ return varvalue;}
}
clownie at 2007-11-11 22:40:03 >
# 4 Re: dynamic object identification problem
you can't create a void data type, void is more for the return type of a method that returns nothing. I don't know of any way to do what you want.
Phaelax at 2007-11-11 22:41:02 >
# 5 Re: dynamic object identification problem
alright, thanks anyways. found the solution i was looking for anyways.
clownie at 2007-11-11 22:42:01 >
# 6 Re: dynamic object identification problem
Umm... you could have just done what Phaelax said.

class MyClass {
private Object data;

public MyClass( Object thing ) {
data = thing;
}
public Object getData() {
return data;
}
}

and if you did this:

MyClass test = new MyClass( new Integer( 5 ));
System.out.println( test.getData() );

test = new MyClass( new String( "hi" ));
System.out.println( test.getData() );

that would print:

5
hi
destin at 2007-11-11 22:43:05 >