C# 'Correct way' type question.

B

Bishman

Hi,

I have a property of my object 'IsValid'. In order to evaluate the status of
the object I have to invoke a method of some sort.

Can I invoke a Method from against the Get method of the property. This
private method sets a private variable, the value of which is then the
return value of the property....

OR

Not have a property 'IsValid' but have a public Method with a return value
.......

or should I be using some other technique ?

In general when should I use properties and when should I use a method with
a return value ( or an 'out' parameter for that matter ) ?

Thanks, Jon.
 
K

Kevin Spencer

It sounds like you're talking about a property, as "IsValid" sounds like it
refers to state rather than process. However, if there are multiple
conditions that must be met for "IsValid" to be true, you would use a
property and a get accessor method to do the calculations and return the
boolean state value.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer

A watched clock never boils.
 
B

Brian Gideon

Jon,

It's not always black and white. With validity it could really go
either way. But, without knowing anything else about your situation
I'm leaning towards a property simply because I think validity does
represent a logical attribute of your class. Of course, if determining
validity involves a lengthy calculation I would use a method instead.
Here's a link that may help you decide.

http://msdn2.microsoft.com/en-us/library/ms229054.aspx

Brian
 
B

Bishman

Thanks for that guys,

Based on the MS article I think a method may be what I need here as the
'IsValid' evaluation is relatively 'expensive' in this case.

Interesting Article !

Cheers.

Jon.
 

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