Private Variables to Public Properties - question

  • Thread starter Thread starter t f
  • Start date Start date
T

t f

Hi

I have a class with a bunch of private variables in, is there an easy
way to make these have public properties without having to type it all in?

e.g.

public class Fu
{
private float fBar;

//now instead of having to type this
public float Bar
{
get
{
return fBar;
}

set
{
fBar = value;
}
}
}

thanks
t f
 
Unfortunately, no, there is nothing in the language that will allow you
to do this. You will have to type out all the properties.
 
I have a class with a bunch of private variables in, is there an easy way
to make these have public properties without having to type it all in?

e.g.

public class Fu
{
private float fBar;

//now instead of having to type this
public float Bar
{
get
{
return fBar;
}

set
{
fBar = value;
}
}
}

thanks
t f

If you really have that many then try a recorded macro or even a permanent
one you can assign to a hotkey. I find macros are undervalued (and even
unknown) by many developers but they frequently save a lot of time. It takes
some thinking for more elaborate activities like this but with practice you
can crank out even complicated scenarios quickly.
 
ReSharper can create the public members for you once you've created
the private ones.

http://www.jetbrains.com/resharper/

Sine you're using a prefix "f" be sure to set that prefix in the
options and it'll name the properties appropriately.

HTH,

Sam
 
Hi

I have a class with a bunch of private variables in, is there an easy
way to make these have public properties without having to type it all in?

e.g.

public class Fu
{
private float fBar;

//now instead of having to type this
public float Bar
{
get
{
return fBar;
}

set
{
fBar = value;
}
}

}

thanks
t f

If you have VS2005, you can right click the field and open the
refactor menu, and choose encapsulate field.
 
Put your cursor before the first letter of the variable, and press Ctrl R E
 
Andy said:
If you have VS2005, you can right click the field and open the
refactor menu, and choose encapsulate field.
lol i didnt even notice that one... cool thanks :-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top