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