This site requires JavaScript, please enable it in your browser!
Greenfoot back
kiarocks
kiarocks wrote ...

2011/7/29

getClass()?

1
2
3
kiarocks kiarocks

2011/7/29

#
Is there such thing as a getClass() method like the getWorld() method?
kiarocks kiarocks

2011/7/29

#
mjrb4 mjrb4

2011/7/30

#
Yup, it's on every object: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#getClass()
kiarocks kiarocks

2011/7/30

#
how could you get a string from one class?
danpost danpost

2011/7/30

#
Again, kiarocks, you may want to be more specific -- Called from where -- How is String declared in Class -- etc. The more information we have, the better armed we are at giving you a helpful response. You do not have to give the exact variable names you may have or explain what you are trying to do, just give the information required to bring across the idea of what you want to accomplish, programmatically.
hotrails hotrails

2011/7/31

#
Again danrocks, you expose a lack of knowledge, while the matter is so obvious.
danpost danpost

2011/7/31

#
@hotrails, who are you referring to by danrocks, and what matter is so obvious? You may want to be more specific, also! I pray that you are not thinking that you are better than any (or every) one else. This is a site for learning, not for railing on others. If you have anything helpful to say, then say it. Otherwise, keep your posts to a minimum (maybe zero). Thanks in advance. (By the way, this is not to get on your case; it is just that there should be a certain etiquite (spelling?) that should be maintained here, and if that was not your intent, praises be, but the lack of specifics in your posting makes it appear that way,and also makes it unhelpful (or maybe it unhelpfulness makes your intentions appear to be other than honorable; and this is just food for thought).
mjrb4 mjrb4

2011/7/31

#
how could you get a string from one class?
If you're referring to getting the name of the class, getSimpleName() might be what you're after? But as people have already said in one form or another, your question is very ambiguous! Again, you'd really help yourself if you were more specific (and help those trying to help you!)
kiarocks kiarocks

2011/8/1

#
ok, so it is declared in class TextMaker:
public String txtString = "";  
and i'm trying to call it in SaveButton class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
/**
 * Write a description of class SaveButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaveButton extends Actor
{
    /**
     * Act - do whatever the SaveButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         try{
            // Create file 
            FileWriter fstream = new FileWriter("out.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(getWorld().getStringFromObject(TxtString));<here!!
            //Close the output stream
            out.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

    }
   
}  
davmac davmac

2011/8/1

#
So you just want to access the txtString variable in the world object? (i.e. TextMaker is your world class?) Then - change line 21 to:
out.write(((TextMaker)getWorld()).txtString);
By the way, you are not getting the string from the class, but from the object which is an instance of the class.
kiarocks kiarocks

2011/8/1

#
is this from the world or the actor?
mjrb4 mjrb4

2011/8/1

#
This would be from the actor, in SaveButton on the line you highlighted earlier. Note that Java is case sensitive - txtString is not the same as TxtString!
kiarocks kiarocks

2011/8/2

#
ok
kiarocks kiarocks

2011/8/2

#
are you thinking that textmaker is the world?
kiarocks kiarocks

2011/8/2

#
error: inconvertible types
There are more replies on the next page.
1
2
3