Closed Thread
Results 1 to 4 of 4

Thread: global variables

  1. #1
    yellowzelo is offline Newbie
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    global variables

    Hi,
    this is my code:

    Code:
    protected void method1(){
        ...
        if (condition1==true){
            MyObjectClass myObject1=new MyObjectClass();
            MyObjectClass myObject2=new MyObjectClass();
        }
        ...
    }
    
    protected void method2(){
        ...
        If (condition1==true){
            //Do some operations with myObject1 and myObject2;
        }
        ...
    }

    The problem is that myObject1 and myObject2 are not accessible from method2. I Think I can't use singleton, because I need more than one instance of MyObjectClass and I neither can't use this:

    Code:
    class GlobalVariables{
        MyObjectClass myObject1=new MyObjectClass();
        MyObjectClass myObject2=new MyObjectClass();
    }
    because I don't need to create these two objects unless condition1==true.

    Thank you for advice.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: global variables

    Declare them in the class but don't instantiate them unless your condition1 is true.

  4. #3
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: global variables

    Code:
    MyObjectClass myObject1;
    MyObjectClass myObject2;
    
    
    protected void method1(){
        ...
        if (condition1==true){
            myObject1=new MyObjectClass();
            myObject2=new MyObjectClass();
        }
        ...
    }
    
    protected void method2(){
        ...
        If (condition1==true){
            //Do some operations with myObject1 and myObject2;
        }
        ...
    }

  5. #4
    yellowzelo is offline Newbie
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Re: global variables

    I had no clue it is so simple. :-)

    Thank you, guys for your response.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. global variables...how to make them
    By alirezan in forum C and C++
    Replies: 1
    Last Post: 09-17-2011, 09:05 AM
  2. Best way to use global variables
    By rabintech in forum C and C++
    Replies: 5
    Last Post: 07-25-2010, 10:33 AM
  3. confused about this global variables
    By jwxie518 in forum C and C++
    Replies: 4
    Last Post: 02-17-2009, 07:28 PM
  4. Global variables (SERVER)
    By Jaan in forum PHP Tutorials
    Replies: 0
    Last Post: 03-22-2007, 07:43 PM
  5. Howto use global variables
    By dirkfirst in forum PHP Development
    Replies: 4
    Last Post: 07-15-2006, 12:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

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