[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

multiple parameters in properties

Posted on 2006-03-22
10
Medium Priority
?
291 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;
     }
}
0
Comment
Question by:davidlars99
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16263661
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
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16263686
david,

Are you talking about indexers?

http://www.csharphelp.com/archives/archive140.html
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16263731
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16264651
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
 
LVL 5

Expert Comment

by:Kelmen
ID: 16264838
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16270178
Indexers are not exactly the same thing, since you can't name the property.

Bob
0
 
LVL 5

Expert Comment

by:Kelmen
ID: 16276636
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
 
LVL 13

Author Comment

by:davidlars99
ID: 16487487
-->  Indexers are not exactly the same thing, since you can't name the property.

what is the distinctions?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16487966
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

873 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