Jump to content
  • Advertisement
Sign in to follow this  
Daniel Miller

C#: no this = no fun

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[I put this in the DX forum by accident... if you were replying I'm really sorry.] In static classes this has no meaning (and is a compile-time error if used). However, how are you supposed to deal with this:
public static void AddToScene(List<Image> images)
{
    this.images.AddRange(images); //images is a static field
}





After changing a class to a static class, I encountered this problem. How can I "access" the field images? Do I have to rename the arguement?

Share this post


Link to post
Share on other sites
Advertisement
I assume images is static.

try

<Name of the class>.images.AddRange(images);

[edit]
You can't use this, becuase this refers to the instance of the object being called. Since your method is static, it doesn't refer to a specific instance. So the only things it can access is other static variables and methods. There isn't any member variables avaiable.

Share this post


Link to post
Share on other sites
This has no meaning in a static class because you can use the class without it being associated with an instance of that class, so 'this' wouldn't make sense in this situation because it wouldn't be associated with any object.

You also cannot declare instance members in a static class, so if you have a field images in a static class it will not compile.

You are showing a static method, so I'm going to assume you have a regular class but are trying use a static method. A static method can be called with or without an instance of the class being instantiated, so you can also not use 'this' in a static method because it might not have an instance associated with it. In this case, you can call the static method as follows: Assume you have a class named MyClass and a static method named MyStaticMethod, MyClass.MyStaticMethod();

You can also not reference non-static fields inside static methods because you might not have an instance of the class to use. With static methods you never have to instantiate an object to use them, so they can only access static fields.

I hope that helps and makes sense.

- Kevin

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattrap
I assume images is static.

try

<Name of the class>.images.AddRange(images);

[edit]
You can't use this, becuase this refers to the instance of the object being called. Since your method is static, it doesn't refer to a specific instance. So the only things it can access is other static variables and methods. There isn't any member variables avaiable.



Right, I realize why this doesn't work. I'll try your method...

...and that's it. Actually, I feel like an idiot for not thinking of that, because that is how everything is reffered to outside of the class.

Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!