BSD

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Global variables?

    6 answers - 940 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I understand that in P it's generally said to be a bad idea to use
    'global' variables outside the normal object-oriented methodology
    but can anyone please advise me on the best way to make a very small
    array of integers available to my entire application? I have a main
    controller object that had this:
    @public
    int myIntArray[10];
    But despite the public command, I can't seem to access it from another
    class. I made sure to include the controller's header file where it's
    needed.
    What's the easiest way to make some integers available to the whole application?
    Thank you all~
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com
    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.1 | | 1550 bytes | |

    27 dec 2005, at 11.55, Matt Reagan wrote:

    I understand that in P it's generally said to be a bad idea to use
    'global' variables outside the normal object-oriented methodology
    but can anyone please advise me on the best way to make a very small
    array of integers available to my entire application? I have a main
    controller object that had this:

    @public
    int myIntArray[10];

    But despite the public command, I can't seem to access it from another
    class. I made sure to include the controller's header file where it's
    needed.

    What's the easiest way to make some integers available to the whole
    application?

    Easiest or best?

    In P you have objects, not free floating data. holds data,
    and methods that acts on their data. So, who is the "owner" of your
    int array? You can probably build a class to hold that data, in which
    case you would allow access to that data via methods on that class:

    @class MyIntArray : NS
    {
    @private

    int myIntArray[10];
    }
    - (int) intAtIndex:(int) index;
    - (void) setInt:(int) integer atIndex:(int) index

    @end

    You might want to consider implementing this class as a singleton class.

    j o a r

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.2 | | 3432 bytes | |

    I actually tried doing that- encompassing the data inside its own
    object. But the problem I'm having involves how to get the data when I
    need it.

    I have a controller object that does much of the processing and
    coordinating for the program, its much of the 'guts' of the simple
    program, and it is also responsible for init'ing the single instance
    of this data object.

    That's fine and dandy- the controller can then access the integers via
    the data object's accessor methods. The problem is that I have a
    custom NSView (among other things) that needs to also access this
    data. Since the controller creates the instance of the data object,
    it's the only object that has a reference pointing to the data object,
    so any other classes doesn't know how to find the data object to
    request the integers. I suppose I could create a method in the
    controller that would allow me to do this:

    int x;
    x = [[myController ] giveMeTheXInteger];

    But this roundabout way of referencing the controller, which
    references the data object, which then returns its integer, seems
    really silly it's just an integer. I just need multiple classes to
    be able to modify and access it.

    Maybe I'm just not understanding how this works, I apologize, I'm a
    newbie. I'm not fully understanding how all these objects are supposed
    to find each other. C seems like an efficient language, I
    can't believe I would have to create an instance variable in every
    single class just to point to this data object will I? Thank you
    for your feedback, any additional help in explaining this will be
    greatly appreciated.
    -matt

    12/27/05, j o a r <joar (AT) joar (DOT) comwrote:

    27 dec 2005, at 11.55, Matt Reagan wrote:

    I understand that in P it's generally said to be a bad idea to use
    'global' variables outside the normal object-oriented methodology
    but can anyone please advise me on the best way to make a very small
    array of integers available to my entire application? I have a main
    controller object that had this:

    @public
    int myIntArray[10];

    But despite the public command, I can't seem to access it from another
    class. I made sure to include the controller's header file where it's
    needed.

    What's the easiest way to make some integers available to the whole
    application?

    Easiest or best?

    In P you have objects, not free floating data. holds data,
    and methods that acts on their data. So, who is the "owner" of your
    int array? You can probably build a class to hold that data, in which
    case you would allow access to that data via methods on that class:

    @class MyIntArray : NS
    {
    @private

    int myIntArray[10];
    }

    - (int) intAtIndex:(int) index;
    - (void) setInt:(int) integer atIndex:(int) index

    @end

    You might want to consider implementing this class as a singleton class.

    j o a r
    >
    >
    >
    >
    >


    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.3 | | 1607 bytes | |

    27 dec 2005, at 12.42, Matt Reagan wrote:

    But this roundabout way of referencing the controller, which
    references the data object, which then returns its integer, seems
    really silly it's just an integer. I just need multiple classes to
    be able to modify and access it.

    I don't think it's silly at all - data hiding is at the core of P,
    and object graphs and object relationships are part of this.

    That said, there are times when you want to provide public access to
    some methods / objects, and that's why I mentioned the singleton
    pattern in my original reply. You should read up on it, there's
    documentation on that design pattern in Apple's documentation, in the
    list archives and most Cocoa / C books you could find. It relies
    on using a class method to access a shared instance - so anyone who
    has #imported the header file from that class can reference the
    shared instance, and it's methods.

    An alternative is of course to simply use class methods and a static
    variable, and not bother with the creating the shared instance at
    all. Personally I prefer to use singleton classes though, as they
    allow you to stick to the regular C design & memory management
    patterns - less room for errors that way.

    j o a r

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.4 | | 1173 bytes | |

    I noticed that the responses thus far have neglected to answer the
    "easy" part of your question.

    If you want to take the "quick and dirty" route for this, it has
    nothing to do with C or P. Just use a global variable in
    your main controller's source file, as you would in any conventional
    C setting:

    int myArray[10];

    Then make an extern reference to it in the associated header file:

    extern int myArray[10];

    Now everybody who imports the header file has access to the array.

    Daniel

    Dec 27, 2005, at 5:55 AM, Matt Reagan wrote:

    int myIntArray[10];

    But despite the public command, I can't seem to access it from another
    class. I made sure to include the controller's header file where it's
    needed.

    What's the easiest way to make some integers available to the whole
    application?

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.5 | | 1465 bytes | |

    Thanks everyone for the help!
    -matt

    12/27/05, Daniel Jalkut <jalkut (AT) red-sweater (DOT) comwrote:
    I noticed that the responses thus far have neglected to answer the "easy"
    part of your question.

    If you want to take the "quick and dirty" route for this, it has nothing to
    do with C or P. Just use a global variable in your main
    controller's source file, as you would in any conventional C setting:

    int myArray[10];

    Then make an extern reference to it in the associated header file:

    extern int myArray[10];

    Now everybody who imports the header file has access to the array.

    Daniel
    --
    Dec 27, 2005, at 5:55 AM, Matt Reagan wrote:
    --
    int myIntArray[10];
    >
    >
    >
    >

    But despite the public command, I can't seem to access it from another

    class. I made sure to include the controller's header file where it's

    needed.
    >
    >
    >
    >

    What's the easiest way to make some integers available to the whole
    application?
    >
    >
    >
    >


    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.6 | | 1794 bytes | |

    Am 27.12.2005 um 15:50 schrieb Daniel Jalkut:
    If you want to take the "quick and dirty" route for this, it has
    nothing to do with C or P. Just use a global variable
    in your main controller's source file, as you would in any
    conventional C setting:

    int myArray[10];

    Then make an extern reference to it in the associated header file:

    extern int myArray[10];

    Now everybody who imports the header file has access to the array.

    Just do yourself and your friends a favor and name the variable
    gMyArray or follow some other standard that makes clear this is a
    global.

    I'd personally would combine two suggestions made: Declare the global
    as a static in your source file and write class-level accessor
    methods for it through which all others will go. That way, when you
    change the size of the array or whatever, you can easily change
    central code in the accessor and track down bugs more easily as they
    all go through the accessors. I.e.:

    #define MY_ARRAY_SIZE10

    static int sMyArray[MY_ARRAY_SIZE];

    @implementation ArrayWrapperClass

    +(int)getIntAtIndex: (unsigned)idx
    {
    if( idx >= MY_ARRAY_SIZE || idx < 0 )
    [NSException raise ];
    return sMyArray[idx];
    }

    +(void)setInt: (int)val AtIndex: (unsigned)idx
    {
    if( idx >= MY_ARRAY_SIZE || idx < 0 )
    [NSException raise ];
    sMyArray[idx] = val;
    }

    @end

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com

Re: Global variables?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 4 =
QUESTION ON "BSD"

EMSDN.COM