dynamic object identification problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: dynamic object identification problem

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    5

    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....

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Desfine data as Object
    Code:
    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.

  3. #3
    Join Date
    Dec 2005
    Posts
    5
    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?
    Last edited by clownie; 12-21-2005 at 04:19 AM.

  4. #4
    Join Date
    Dec 2005
    Posts
    5
    script class
    Code:
     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
    Code:
    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;}
    }

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    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.

  6. #6
    Join Date
    Dec 2005
    Posts
    5
    alright, thanks anyways. found the solution i was looking for anyways.

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Umm... you could have just done what Phaelax said.

    Code:
    class MyClass {
        private Object data;
    
        public MyClass( Object thing ) {
            data = thing;
        }
        public Object getData() {
            return data;
        }
    }
    and if you did this:
    Code:
    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:
    Code:
    5
    hi

Similar Threads

  1. Replies: 5
    Last Post: 09-27-2002, 04:40 PM
  2. Javascript object function problem
    By sean in forum Web
    Replies: 1
    Last Post: 08-04-2002, 10:40 AM
  3. Replies: 6
    Last Post: 03-26-2002, 09:58 AM
  4. Dynamic Object Creation... Possible?
    By Jordan in forum ASP.NET
    Replies: 2
    Last Post: 10-13-2001, 02:02 AM
  5. Object Modeling for the WEB
    By Greg Dirst in forum authorevents.appleman
    Replies: 1
    Last Post: 04-10-2000, 02:56 PM

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