multiple parameters in properties

hi,

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

public int X
{
     get
     {
          return x;
     }
     set
     {
          x = value;
     }
}
LVL 13
davidlars99Asked:
Who is Participating?
 
Bob LearnedCommented:
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
0
 
Ravi SinghSenior Software EngineerCommented:
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;
}
...
0
 
PoeticAudioCommented:
david,

Are you talking about indexers?

http://www.csharphelp.com/archives/archive140.html
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
PoeticAudioCommented:
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.
0
 
Bob LearnedCommented:
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
0
 
KelmenCommented:
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;
        }
 }
0
 
Bob LearnedCommented:
Indexers are not exactly the same thing, since you can't name the property.

Bob
0
 
KelmenCommented:
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]
0
 
davidlars99Author Commented:
-->  Indexers are not exactly the same thing, since you can't name the property.

what is the distinctions?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.