We help IT Professionals succeed at work.

multiple parameters in properties

davidlars99
davidlars99 asked
on
Medium Priority
306 Views
Last Modified: 2010-04-16
hi,

how do I create property with multliple parameters, this one supports only one parameter.

public int X
{
     get
     {
          return x;
     }
     set
     {
          x = value;
     }
}
Comment
Watch Question

Ravi SinghSenior Software Engineer

Commented:
Hi, you can't create a property that accepts multiple parameters, they are usually used to allow read/write or read only access to a single field in your class. I think you'll have to use setter/getter methods in your case.

public void SetMethod(int a, int b, ...)
{
   this.a = a;
   this.b = b;
   ...
}

public int GetA()
{
   return this.a;
}
...
david,

Are you talking about indexers?

http://www.csharphelp.com/archives/archive140.html
I am confused to the way you're using the word parameter. The property you defined doesn't have any parameters, methods have parameters as Zephyr pointed out.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
VB.NET supports properties with multiple arguments:

  Public ReadOnly Property GetImage(ByVal folder As String, ByVal index As Integer) As Image
    Get
      Return Image.FromFile(IO.Directory.GetFiles(folder)(index))
    End Get
  End Property

But, I don't believe the C# supports that kind of syntax.

Bob

Commented:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/5ecdc90355e21939

in C# , using indexer

 public string this [int param1, int param2]
 {
     get
        {
                return myprop;
        }
     set
        {
                myprop = value;
        }
 }
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Indexers are not exactly the same thing, since you can't name the property.

Bob

Commented:
then try this:

class B {
  public Image this[string, int] {
    get {...}
    set {...}
} }

class A {
  private B _b;
  public B GetImage{
    get { return _b; }
    set {...}
} }

using-
A objA;
objA.GetImage[string, int] = ...
... = objA.GetImage[string, int]

Author

Commented:
-->  Indexers are not exactly the same thing, since you can't name the property.

what is the distinctions?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Definition of indexers:

http://msdn2.microsoft.com/en-us/library/6x16t2tx.aspx

<Quote>
Indexers permit instances of a class or struct to be indexed in the same way as arrays. Indexers are similar to properties except that their accessors take parameters.
</Quote>

If you have an indexer, then you can short-cut the syntax.   Here is an example of syntax for a DataRow:

DataRow row = table.Rows[0];
row.Item[0] = "Bob";

    short-cuts to

DataRow row = table.Rows[0];
row[0] = "Bob";

In this case, Item is an indexer property.

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.