[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

properties and invoking a method...

Posted on 2006-11-27
6
Medium Priority
?
220 Views
Last Modified: 2010-04-16
As I research, I'm trying to find out how to invoke a method from a property.  Maybe I'm not quite understanding the logic behind it, however, the examples I see have methods set to Public.  I'm not going crazy am I.  If it's public, what's the sense in invoking a method from the property..  I'm developing my own library, and I only want to provide property level access, etc.

I want to pass a string to a property, call a method from within that property, and pass back an array of strings to pick and choose for use.

Any suggestions?

0
Comment
Question by:scorp8
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 18023515
Well, I would tend to agree with you - making the method and the property both public doesn't make any sense. You can make the method private and still call from the property.

HOWEVER, as a good coding practice I tend to keep all my properties light - i.e. they especially don't call process intensive methods when accessed. This is becauase the consumer of the class (3rd party integrator, another member of the dev team, etc...) can safely assume that calling a property will be fast and will not cause an overhead if used in a tight loop, whereas calling a method usually involves some kind of processing overhead. Usually my properties simply return a private variable.
0
 
LVL 1

Expert Comment

by:Epurchase
ID: 18027476
Whenever you want to do some operation (either intensive or not), You must expose through methods. Properties should only be used for characteristics of your object.

However, You could create a class with a string and array of string say it "ParamClass".
Set the property type ParamClass. like that

public ParamClass MyProperty
{
    get { ... }
    set { ... }
}
0
 
LVL 1

Author Comment

by:scorp8
ID: 18028119
What is a good way to pass a string into a property, invoke a method,  then return an array?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Nightman
ID: 18028946
Do it through the method directly:

public Array ReturnMyArray(string MyInputString)
{
   //do some processing
   return MyArrayVariable;
}
0
 
LVL 1

Author Comment

by:scorp8
ID: 18039494
I don't want to expose my method as public, I'd rather expose the property and invoke the method from my property.
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18041213
Then you would do something like this:

    private string _myString;
    public string MyString
    {
      get
      {
        return _myString;
      }
      set
      {
        _myString=value;
      }
    }
    public Array ReturnMyArray
    {
      get
      {
        //do some processing
        return GenerateMyArray(_myString);
      }
    }
    private Array GenerateMyArray(string MyString)
    {
      Array MyArrayVariable;
      //do some processing
      return MyArrayVariable;
    }      //

You would have to set the property MyString first via the property, and then when you call the ReturnMyArray property it will call the private method passing in the string.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question