Best Practice: Property Usage

  • Thread starter Thread starter MikeG
  • Start date Start date
M

MikeG

When creating a class library is it wrong or not a 'Best Practice' to
reference a property of an object from within a constructor or method of
that object? I recall being told not to do this but I really don't
understand why. Any insight would be greatly appreciated.
Here is an example of what I mean...

namespace ClassLibrary
{
public class MyClass
{
private string _myString;

public MyClass()
{
}

public MyClass(int myInt)
{
// Best Practice issue?
MyString = GetString(myInt);
}

public string MyString
{
get { return _myString; }
set { _myString = FormatString(value); }
}

public void SomePublicMethod()
{
// Best Practice issue?
SomePrivateMethod(MyString);
}

private void SomePrivateMethod(string myString)
{
// Lets pretend that this method does something
}

private string FormatString(string myString)
{
// Do some formatting
return myString;
}

private string GetString(int myInt)
{
// Lets pretend this value was retrieved
// based on the myInt parameter
return "someString";
}
}
}
 
"MikeG" <[email protected]> a écrit dans le message de %[email protected]...

| When creating a class library is it wrong or not a 'Best Practice' to
| reference a property of an object from within a constructor or method of
| that object? I recall being told not to do this but I really don't
| understand why. Any insight would be greatly appreciated.

I can't see any problem with using a property setter to set a field value;
aftera ll it is only really a prettified method. :-)

Joanna
 
Back
Top