Solved

properties and invoking a method...

Posted on 2006-11-27
6
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 125 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 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