By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

Best Practice: Property Usage

P: n/a
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";
}
}
}

Aug 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"MikeG" <Mi***@community.nospama écrit dans le message de news:
%2****************@TK2MSFTNGP02.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 7 '06 #2

P: n/a
Thanks Joanna!
I really appreciate your response.

Mike G.

"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"MikeG" <Mi***@community.nospama écrit dans le message de news:
%2****************@TK2MSFTNGP02.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


Aug 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.