Custom Class Get and Set issues...
  • Butterfly
    Butterfly
    rebraist
    Posts: 96 from 2011/4/6
    From: Naples, Italy
    Hi! i'm studying class2.c example and i modified it a little bit.
    I added to the class the Input_Mode_RelVerify so it acts as a button.
    When i click the button i make start two methods:
    1) i read a string costant with get and put it in a variable
    2) i take that value and i want to use it as a muia text value of a textbox.
    Problem: the best thing i can obtain is to trash the textbox. the worst it writes nothing. Prob'bly it's a "visibility" problem or a casting one.
    These are two "requests". myobj is the are object and mytext a simple text area...
    static ULONG mGet(struct IClass *cl,Object *obj,struct opGet *msg)
    {

    struct MyData *data = INST_DATA(cl,obj);

    switch ((msg)->opg_AttrID)
    {
    case MYSNDATTR:
    *(char**)msg->opg_Storage="foo";


    return(TRUE);


    }

    return(DoSuperMethodA(cl,obj,(APTR)msg));
    }

    char caoo[7];
    these are two domethods

    DoMethod(MyObj, MUIM_Notify, MUIA_Pressed, FALSE, MyObj,3, OM_GET, MYSNDATTR,(ULONG)&caoo);
    DoMethod(MyObj, MUIM_Notify, MUIA_Pressed, FALSE, mytext, 3, MUIM_Set, MUIA_Text_Contents,(ULONG) caoo);

    I really don't understand why and i know it's surely a silly thing...


    [ Edited by rebraist 04.03.2012 - 08:32 ]
    Mac Mini g4 1,5 mos 3.1 registered
    Powerbook g4 1,67 mos 3.1 unregistered
    Sam440 Os4.1.6
    Aros-Aros-Aros.
  • »03.03.12 - 22:30
    Profile
  • MorphOS Developer
    jacadcaps
    Posts: 2988 from 2003/3/5
    From: Canada
    Well... caoo looks like a temporary variable, so by the time the notify is executed, its memory address is already invalid and you're trashing memory. It could probably work if you put that array into the instance data of your object. I would however simply add a method of my own that gets triggered by the click, does the first OM_GET and sets the string.
  • »04.03.12 - 01:04
    Profile Visit Website
  • Butterfly
    Butterfly
    rebraist
    Posts: 96 from 2011/4/6
    From: Naples, Italy
    i even tried with a global static variable but nothing (caoo is defined in main).
    i'll try the click-trigger method way.
    tx!

    edit:
    this is new to me. how can i add a variable to instdata?
    something like this?
    instdata(cl,obj, variable) ??

    [ Edited by rebraist 04.03.2012 - 08:31 ]
    Mac Mini g4 1,5 mos 3.1 registered
    Powerbook g4 1,67 mos 3.1 unregistered
    Sam440 Os4.1.6
    Aros-Aros-Aros.
  • »04.03.12 - 08:30
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    Tcheko
    Posts: 511 from 2003/2/25
    From: France
    At the begining of class2.c, you have a struct Data { ULONG foobar, ...}; This is the instance data of the object.

    You can expand it with whatever you like. Be carefull, AFAIR, there is a 64KB limit. Dunno if this is still the case with MUI4. But well. 64KB should be enough.

    In your method, you get the instance data with INST_DATA(cl,obj).

    struct Data *data = INST_DATA(cl,obj);

    data->foobar = 1337.

    You should read the MUI tutorial written by krahsan in the morphzone library.

    It will certainly help with your current problem.


    ++
    Quelque soit le chemin que tu prendras dans la vie, sache que tu auras des ampoules aux pieds.
    -------
    I need to practice my Kung Fu.
  • »04.03.12 - 09:42
    Profile Visit Website
  • Butterfly
    Butterfly
    rebraist
    Posts: 96 from 2011/4/6
    From: Naples, Italy
    hmmm
    what i'm trying to produce is simply two objects that communicate one each other:
    - the first obj "get"(s) some data to the second obj at the button click.
    yes i can extend data as i want.
    but what i have to understand is: is msg->opg_storage the area where...
    yes. correct behaviour. let's resume...
    msg->opg_storage POINTS to the area where the "string is".
    msg->opg_storage IS NOT a container to the string.
    So---> when i close GET, obviously, the string costant simply doesn'exist nomore.
    When, at the exit, msg->opg_storage goes to my caoo array and tells him: hey kid! i've got a long voyage!here's the pointer to your string costant, the poor caoo looks at the memory and says:trash!
    Anssswer: get is no good to passing volatile string costants (i think about all is no good to it) but it's use is another: to act as an interface "mainly" (if not only) to the "data" area of the object (as oop wants anyway).
    Ok. solved (at least in my mind)

    [ Edited by rebraist 04.03.2012 - 15:22 ]
    Mac Mini g4 1,5 mos 3.1 registered
    Powerbook g4 1,67 mos 3.1 unregistered
    Sam440 Os4.1.6
    Aros-Aros-Aros.
  • »04.03.12 - 15:21
    Profile
  • Butterfly
    Butterfly
    rebraist
    Posts: 96 from 2011/4/6
    From: Naples, Italy
    I solved calling a function in domethod.
    why? every other mothed based on these failed. even using static global variables there was no game.
    DoMethod(MyObj, MUIM_Notify, MUIA_Pressed, FALSE, MyObj,3, OM_GET, MYSNDATTR,(ULONG)&data);
    DoMethod(MyObj, MUIM_Notify, MUIA_Pressed, FALSE, MyObj, 1, MyClickMethod);
    MyClickMethod simply takes the data from user data, eventually transforms it in a string with sprintf and then calls a set.


    [ Edited by rebraist 04.03.2012 - 21:50 ]
    Mac Mini g4 1,5 mos 3.1 registered
    Powerbook g4 1,67 mos 3.1 unregistered
    Sam440 Os4.1.6
    Aros-Aros-Aros.
  • »04.03.12 - 21:48
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    Tcheko
    Posts: 511 from 2003/2/25
    From: France
    Keep in mind that it is a real bad bad bad practice to use globals in OO programming. Most objects could exist in multiple instances. Then, if any method is using some global to keep track of some data, this global will be shared by many objects at the same time.

    This can only lead to unexpected behaviour unless this is the intented one.

    Always keep object data in instance data and nowhere else.
    Quelque soit le chemin que tu prendras dans la vie, sache que tu auras des ampoules aux pieds.
    -------
    I need to practice my Kung Fu.
  • »04.03.12 - 22:45
    Profile Visit Website
  • Butterfly
    Butterfly
    rebraist
    Posts: 96 from 2011/4/6
    From: Naples, Italy
    Totally agree on globals.
    But when you have two objects that want to talk each other, let's say two textboxex where the output of the first should be given to the second textbox, surely you'll write the output of the 1st in a variable (global or not) which will be served as input for the 2nd textbox.
    Or do you mean there's other direct ways?
    Mac Mini g4 1,5 mos 3.1 registered
    Powerbook g4 1,67 mos 3.1 unregistered
    Sam440 Os4.1.6
    Aros-Aros-Aros.
  • »05.03.12 - 21:15
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    Tcheko
    Posts: 511 from 2003/2/25
    From: France
    DoMethod(obj_text_1, MUIM_Notify, MUIA_Text_Contents, MUIV_EveryTime, obj_text_2, 3, MUIM_Set, MUIA_Text_Contents, MUIV_TriggerValue);

    Afair.
    Quelque soit le chemin que tu prendras dans la vie, sache que tu auras des ampoules aux pieds.
    -------
    I need to practice my Kung Fu.
  • »05.03.12 - 21:19
    Profile Visit Website
  • Butterfly
    Butterfly
    rebraist
    Posts: 96 from 2011/4/6
    From: Naples, Italy
    Thx but what i meant was in the case i'd have overrided set and get:)
    can i use public attributes to do it?
    Mac Mini g4 1,5 mos 3.1 registered
    Powerbook g4 1,67 mos 3.1 unregistered
    Sam440 Os4.1.6
    Aros-Aros-Aros.
  • »06.03.12 - 18:37
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    Tcheko
    Posts: 511 from 2003/2/25
    From: France
    Overriding OM_GET & OM_SET? Hey, sounds good. :)

    There are next to no difference with not overridden set/get method appart some overhead.

    You can use MUIM_Notify even with private attributes too. Just implement Get & Set method for them.

    ++
    Quelque soit le chemin que tu prendras dans la vie, sache que tu auras des ampoules aux pieds.
    -------
    I need to practice my Kung Fu.
  • »06.03.12 - 18:52
    Profile Visit Website