Advice

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi to all

What is the best way call methods with parameters or create properties to store values?

Thanks in advance.
Josema.
 
I'm afraid I cannot understand the meaning of your question. Do you mean
which VS .NET features to use to do that in the most efficient manner? Or do
you mean which language constructs are used to call methods and create
properties? If the latter is the case, see below:

A method is called by referencing its name followed by parenthes, in which
the values of the parameters are listed:

int result = Add(2, 2); // Guess what the value of the "result" variable is
:-)

To create a property, you need to declare a field storing property data and
the property construct itself:

string _firstName;
string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
 
=?Utf-8?B?am9zZW1h?= said:
What is the best way call methods with parameters or create properties
to store values?

Think of what makes good design...

Use method parameters if:

- The value passed does not modify the object
- The value passed will not be needed long-term by the object

Use properties if:

- The value should be "remembered" for some reason
- The value will be used for multiple operations / function calls

Of course, you have to design your application well from other aspects.
This is just the surface of the issue.
 
Hi josema,

If I understand correctly your question you are asking whether to use method
or property to set and get a member variables (Name = x or SetName(x)).
If you are concerned about the the performance you shouldn't be. Both
methods and properties are method calls so there is no difference in terms
of performance.
You should choose one of those depending on your application design and how
you are planning to set and get that value.
Bare in mind that setting and getting properties looks for the programmers
that are going to use that class like giving value and reading normal data
members (variables). Thus, if you use properties setting and getting
operations should be fast and it shouldn't has more site effects than it
would have if it was a regular class variable.

For example, IMHO, it is not good design practice to write Property that
takes 10 seconds to be set or read. If it is going to take 10 seconds it is
better to use SetName() method that gives the programmersusing the class a
hint that it might involve more actions than just setting a variable.
Of course proper naming is very important I believe that methods should be
named with verbs (or they should have a verb in their names), properties
should be named with adjectives or nouns.

HTH
B\rgds
100
 
Back
Top