CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Local variable within inner class

  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Local variable within inner class

    Hey gang,

    Having a slight issue that I know I knew how to resolve before but I am just starting to get back into coding. I am trying to find a way to create a string return or variable from within a override method so that it can be handed back out to my main program. The snippet of code is:

    // receive msg
    PacketListener pl = new PacketListener() {
    @Override
    public void processPacket(Packet p) {
    System.out.println(p.getFrom() + ": " + p.toString());
    if (p instanceof Message) {
    Message msg = (Message) p;
    System.out.println(msg.getFrom() + ": " + msg.getBody());
    }
    }
    };
    connection.addPacketListener(pl, null);
    // wait for user to end program
    System.in.read();

    The idea is im trying to get the msg.getBody() back out to the main program to be processed but I can't logically think of a way to do. I used to run into this when I first started using action listeners I believe. Any ideas?

    Thanks,
    Heath

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Local variable within inner class

    The simplest way is to take advantage of the fact that the overridden method has access to local variables in the scope where the anonymous PacketListener class is declared (i.e. the enclosing method), so you could use a variable you can set the text into. You can't use a simple variable like a String, because it must be final, so you can't assign a value to it after it's initialised, but you could use a variable that contains a String as a field, or an array. An array of one element is often used for this kind of thing:
    Code:
    final String[] msgBody = new String[1];
    ...
    PacketListener pl = new PacketListener() {
                @Override
                public void processPacket(Packet p) {
                    System.out.println(p.getFrom() + ": " + p.toString());
                    if (p instanceof Message) {
                        Message msg = (Message) p;
                        msgBody[0] = msg.getBody();
                        System.out.println(msg.getFrom() + ": " + msg.getBody());
                    }
                }
            };
            connection.addPacketListener(pl, null);
    // wait for user to end program
            System.in.read();
    However, if the processPacket method will get called asynchronously by another thread, but you want the results to be handled immediately after, you could write a calback method in your outer class to handle the message body and call it from inside the processPacket method (the inner class has access to the outer class's methods too). This is effectively a short form of making the outer class a listener to the PacketListener's processPacket method:
    Code:
    void processMsgBody(String msgBody) {
       ... // whatever
    }
    
    void snippetMethod() {
       ...
       PacketListener pl = new PacketListener() {
                @Override
                public void processPacket(Packet p) {
                    System.out.println(p.getFrom() + ": " + p.toString());
                    if (p instanceof Message) {
                        Message msg = (Message) p;
                        processMsgBody(msg.getBody());
                        System.out.println(msg.getFrom() + ": " + msg.getBody());
                    }
                }
            };
            connection.addPacketListener(pl, null);
    // wait for user to end program
            System.in.read();
       ...
    }
    You also have the option of explicitly subclassing PacketListener and adding methods to make life easier. This is probably only worth doing if you're likely to use the class more than once.

    A man's reach should exceed his grasp, or what's heaven for?
    R. Browning
    Last edited by dlorde; April 14th, 2010 at 04:45 AM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)