SPONSORS:






User Tag List

Page 1 of 3 123 LastLast
Results 1 to 10 of 30
  1. #1
    Senior Member
    Join Date
    Jul 1999
    Location
    Burlingame CA 94010
    Posts
    502
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tough Question #1

    When is the "this" keyword actually required, as opposed to when it can be used?


  2. #2
    Senior Member
    Join Date
    Jul 1999
    Location
    Burlingame CA 94010
    Posts
    502
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    James, Dave & AJ are excluded from answering

  3. #3
    Senior Member
    Join Date
    Dec 2001
    Location
    San Jose, CA
    Posts
    102
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    If you create objects out of classes, you are required to use "this" to refer to your current object.

    Am I right? Or, completely miss the point.

    Michael



    ------------------

  4. #4
    Senior Member
    Join Date
    Jul 1999
    Location
    Burlingame CA 94010
    Posts
    502
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    You'll need to provide more detail required to adequately answer the question. Posting code would be perfect. A code example of when its not required and when it is required would win something free from AJ and betasoft.

  5. #5
    Senior Member
    Join Date
    Aug 1999
    Location
    Cambridge, UK
    Posts
    470
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    An example of when the keyword 'this' is required would be when a method's return-type is window, and the method wants to return the object itself (whose method is being called). Rather an unusual situation in silktest admittedly.

    You can (but don't need to) use the keyword 'this' in a method to refer to the object whose method is being called, but in most cases you can leave it out; for example to call a different method of the same object you can code this as:
    Similarly when referring to member variables of the object from a method, you can refer to them as this.variablename, or you can legitimately leave off the 'this.' part.

    [This message has been edited by vincebowdren (edited 06-13-2002).]

  6. #6
    Senior Member
    Join Date
    Jul 2000
    Posts
    186
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    You need to use the this keyword when creating a method in a winclass and you are referring to the object itself:

    [+] winclass ComboBox : ComboBox
    [-] void Choose(string s)
    [ ] string temp
    [ ] list of string lsTemp=this.GetContents()
    [-] for each temp in lsTemp
    [+] if StrPos(s,temp)>0
    [ ] Select(temp)

    You don't need to use this when declaring a window (but you can)
    [-] window ComboBox myComboBox
    [-] void Choose(string s)
    [ ] string temp
    [ ] list of string lsTemp=myComboBox.GetContents()
    [-] for each temp in lsTemp
    [-] if StrPos(s,temp)>0
    [ ] Select(temp)


    ------------------
    Tom
    Tom

  7. #7
    Senior Member
    Join Date
    Mar 2000
    Location
    pisctaway, NJ USA
    Posts
    188
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    I know. Whenever you are doing something complicated in Silk and when you attempt to compile (you get the ever generic SYNTAX ERROR) and the only thing left to do is change your variable to ANYTYPE or rename the reference to THIS so that you get rid of the compile for now and want the runtime error!

    Anthony

    ------------------

  8. #8
    Member
    Join Date
    Aug 2001
    Location
    Burlington MA USA
    Posts
    35
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    If I am not wrong, 'this' is used in cases like following::

    class myclass
    {
    public:
    int x;
    void myfunction(int a);
    };

    void myclass::myfunction(int x)
    {
    this->x = x;
    //does some operations...
    cout<<x<<endl;
    }

    main()
    {
    myclass o;
    o.myfunction(5);
    return 0;
    }



    ------------------

  9. #9
    Senior Member
    Join Date
    Apr 2000
    Posts
    401
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    Hmm not sure about the above as I think you may be able to reference the instance methods and variables directly e.g. object.method or variable. I think it is definately required when referencing instance variables as in a case where a parameter and an instance variable have the SAME name. In this case (see example), this is used to explicitly access the instance variable as opposed to the parameter.

    EXAMPLE
    // in this example, this is used to specify which "name" to assign

    class Foo {
    String name;
    public Foo (String name) {
    this.name = name;
    }
    }

    I think that is the only time you really need to use it.
    But not 100% sure about the windows class stuff

    ------------------
    The Test Force is strong in this one

  10. #10
    Senior Member
    Join Date
    Jul 1999
    Location
    Burlingame CA 94010
    Posts
    502
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Tough Question #1

    'this' is only required when an instance overrides a class method.

    Copy the following code into a .t file and run it.

    Hope this helps.

    Stay tuned for the next Tough Question

    ------------------
    John W Green
    jwgreen@automationexpertise.com
    http://www.automationexpertise.com

 

 
Page 1 of 3 123 LastLast

Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 05:33 PM.

Copyright BetaSoft Inc.