0

Possible Duplicate:
Naming convention for private fields

I read on msdn about C# naming conventions but they don't talk about naming private fields vs parameters.

public void SetAnimation(int framesX, int framesY)
{
    framesX = framesX; // the first one is private class member
    framesY = framesY;
}

I must name private field differently than a parameter. I can't do camel case for both. What do you suggest?

Community
  • 1
  • 1
pokoko222
  • 57
  • 1
  • 3

10 Answers10

5

You can have same name. To have it more elegant, you can have:

this.framesX = framesX; // the first one is private class member
this.framesY = framesY;
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • Only the first one is a private class member (as suggested with the comment), therefore i assume the declaration of framesY is omitted on the posted code (and this.framesY would not compile) –  Aug 25 '11 at 12:04
  • I don't think he was trying to assign the parameter to itself. – Shadow The GPT Wizard Aug 25 '11 at 12:27
3

I generally do:

private int _framesX;
private int _framesY;

That makes it more clear:

public void SetAnimation(int framesX, int framesY)
{
    _framesX = framesX;
    _framesY = framesY;
}
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
1

This really depends on the company you work at. Basically a naming convention is a team rule and for example, Resharper allows you to add a team convention

Yurii Hohan
  • 4,021
  • 4
  • 40
  • 54
0

You can name it the same, but if referring to the private member you have to use

this.framesX;

So for example;

this.framesX = framesX;
0

I normaly name my private fields (are there others ;) ) with a '_' prefixed. For some people this is a smell others do the same - I guess it's a matter of taste.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
0

I use _framesX for marking private fields.

Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
0

You could go with the "_" prefix for private members or use "this.framesX" or both "this._framesX".

I'd say it depents on what your team is using.

0

Your code is fine, you might need to specify this thought. Well thats how I name my Fields and parameters mostly.

public void SetAnimation(int framesX, int framesY)
{
    this.framesX = framesX; // the first one is private class member
    this.framesY = framesY;
}
Jethro
  • 5,896
  • 3
  • 23
  • 24
0

private variables should start with lowercase. so you've got that right.

you can make yr code work by using 'this' keyword to refer back to the instantiated object.

public void SetAnimation(int framesX, int framesY)
{
    this.framesX = framesX; // the first one is private class member
    this.framesY = framesY;
}
Tom F
  • 1
0

I have a tendency to use only properties starting with a capital, and define the getter and setter public or private

public int FramesX { public get; private set; }
public int FramesY { get; set; }
Bazzz
  • 26,427
  • 12
  • 52
  • 69