You are not logged in.

#1 2005-06-15 05:42:10

dust
Member
Registered: 2005-06-04
Posts: 152
Website

need some help understanding this

As in the oft used keyword "this" . I've been searching and reading through different tutorials, and I don't think I've ever once successfully used or even understood using this . Would anyone mind helping me understand the concept and usage of it? (currently mucking in c#, but I'd like to get a basic grasp so i could use it succesfully in all my endeavors)

Thanks.


Writing stories for a machine.

Offline

#2 2005-06-15 07:03:06

mico
Member
From: Slovenia
Registered: 2004-02-08
Posts: 239

Re: need some help understanding this

I know C++ and Java, not C#, but this is probably similar.

"this" is a pointer to the object (instance of a class) the method (class member function) is currently working on.

You can use "this" to distinguish local and member variables and other data types. It is often used in constructors that accept parameters to assign initial values of member variables. If they have the same name, you can access member variables only with "this.aVariable", because "aVariable" means the local one.

Some people prefer using prefixes c_ and m_ for static (class) and member data types. In that case you don't use this in constructors. There may still be other places where you could use this.

Of course usage is not limited to variables, you can use this for any member. Avoid using it for static members, because it makes the code ugly - use the class itself.

I use "this" only when I have to, or when it makes sense. When you write some code, you'll get some feeling about when to use it wink

Offline

#3 2005-06-15 07:45:44

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: need some help understanding this

Thank you mico, would you mind giving me some code examples in java ?


Writing stories for a machine.

Offline

#4 2005-06-15 08:16:02

glitz
Member
Registered: 2005-06-15
Posts: 19

Re: need some help understanding this

A very simple Java example:

public class TestA {
   private int value;
   public int getValue() {
      return this.value;
      // OR : there is no ambiguity in this function
      // return value;
   }
   public TestA(int value) {
      // assigns value to the member value!
      this.value = value;
      System.out.println("A: " + this.value);
   }
}

public class TestB {
   private int value;
   public int getValue() {
       return this.value; // or just return value; 
   }
   public TestB(int value) {
       // this'll assign the current value to itself, not to the member value!
       value = value;
       System.out.println("B: " + this.value);
   }
}

public class Test {
    public static void main(String[] args) {
        TestA a = new TestA(5);
        TestB b = new TestB(5);
    }
}

You can use the this pointer for clarity, but in some cases it's obligatory.  If there is any ambiguity between the member variable and a local variable, then you'll have to use the this pointer to differentiate between them.  If you try and run these examples, you'll notice that B's value will be 0, the default for an unassigned integer variable in Java, A's value will be 5.

edit: should've used code tags smile

Offline

#5 2005-06-15 08:19:43

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: need some help understanding this

Ok, I think I figured it out now for using this with variables, and I'm moderatley sure I understand it for passing a reference of an object to a method. Just need to play with it more I guess to get the hang of it. Though it seems like a an unwarranted complication when you can just use different varaible names and not need to worry about it then. There a reason behind the convention for using this in the manner you've outlined ?


Writing stories for a machine.

Offline

#6 2005-06-15 08:22:47

mico
Member
From: Slovenia
Registered: 2004-02-08
Posts: 239

Re: need some help understanding this

On Sun's java pages there is file TableSorter.java. Here's a member function of a class in it:

    public void setTableModel(TableModel tableModel) {
        if (this.tableModel != null) {
            this.tableModel.removeTableModelListener(tableModelListener);
        }

        this.tableModel = tableModel;
        if (this.tableModel != null) {
            this.tableModel.addTableModelListener(tableModelListener);
        }

        clearSortingState();
        fireTableStructureChanged();
    }

And here is another class definition and a constructor:

    private static class Arrow implements Icon {
        private boolean descending;
        private int size;
        private int priority;

        public Arrow(boolean descending, int size, int priority) {
            this.descending = descending;
            this.size = size;
            this.priority = priority;
        }
    // the code goes on ....
    }

Don't bother figuring out what the code does, but try to understand the difference between "this.tableModel" and "tableModel". And other pairs.

There is tableModel the parameter (local object). Obviously, there is another object with same name (tableModel) that must have been declared previously as a member of same class as the function setTableModel, or inherited. In any case, the class also has a member object, called tableModel. In a function where a local tableModel exists, the member tableModel becomes invisible, because different objects cannot share the same name, at least not in the same part of the code. Else it would be impossible to tell which object that name refers to.

So, to access the member, you can use "theInstanceOfClass.tableModel". But you may have many different instances of that class and you don't want to write the same code for each of them. So you use "this" to refer to the current instance of class.

Offline

#7 2005-06-15 08:27:29

glitz
Member
Registered: 2005-06-15
Posts: 19

Re: need some help understanding this

I don't know if this is a convention or not, but imo it doesn't make things more complicated.  It's even easier to read for someone not familiar with the code.  Of course, it does seem complicated in an easy example such as this, but if a constructor takes 5 parameters, it might improve readability.

Offline

#8 2005-06-15 08:28:17

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: need some help understanding this

I understand more now, thank you for all the help.


Writing stories for a machine.

Offline

#9 2005-06-15 08:48:01

dtw
Forum Fellow
From: UK
Registered: 2004-08-03
Posts: 4,436
Website

Re: need some help understanding this

dust - homework all sorted now then?

Offline

#10 2005-06-15 08:49:45

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: need some help understanding this

LOL

Heh, I'm not in school currently dibble. I'm just trying to teach myself c# smile .


Writing stories for a machine.

Offline

#11 2005-06-15 09:16:00

dtw
Forum Fellow
From: UK
Registered: 2004-08-03
Posts: 4,436
Website

Re: need some help understanding this

ok lol you can see how it looked tho smile

Offline

#12 2005-06-15 09:23:15

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: need some help understanding this

Yeah yeah.. tongue


Writing stories for a machine.

Offline

#13 2005-06-15 21:21:33

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: need some help understanding this

for the record, think of this this way:

SomeClass foo;
foo.bar();

now, when calling bar, "this" == "foo" it's just a way to reference the instance of the object from withing the object itself.

it's the same as python's "self" and VB's "Me"

Offline

#14 2005-06-15 21:31:51

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: need some help understanding this

Once you think you've got it all figured out, take a look at how Python implements the 'self' argument. That should either obfuscate or clarify the issue, I'm not sure which.

Dusty

Offline

#15 2005-06-15 23:09:55

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: need some help understanding this

Dusty wrote:

Once you think you've got it all figured out, take a look at how Python implements the 'self' argument. That should either obfuscate or clarify the issue, I'm not sure which.

Dusty

I always hated how you had to declare that... it could easilly be assumed based on scope... silly python 8)

Offline

#16 2005-06-15 23:46:41

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: need some help understanding this

phrakture wrote:
Dusty wrote:

Once you think you've got it all figured out, take a look at how Python implements the 'self' argument. That should either obfuscate or clarify the issue, I'm not sure which.

Dusty

I always hated how you had to declare that... it could easilly be assumed based on scope... silly python 8)

yeah, but you call it whatever you like.

Offline

#17 2005-06-16 04:03:18

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: need some help understanding this

That's why I prefer Ruby tongue .


Writing stories for a machine.

Offline

Board footer

Powered by FluxBB