Solved

properties and invoking a method...

Posted on 2006-11-27
6
214 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
Interview questions for support of a legacy ASP.NET site 4 39
C#  Radio button search for Date not DateTime 4 35
Exit the loop 4 35
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

11 Experts available now in Live!

Get 1:1 Help Now