Solved

properties and invoking a method...

Posted on 2006-11-27
6
213 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now