Best Practice: Property Usage

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";
}
}
}
 
J

Joanna Carter [TeamB]

"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
 

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

Top