Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2005, 11:02 AM
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Indexers and Properties

Hi All,

I may be a little confused on indexers and properties. Is there any way to a have a parameterized property?

For instance, say a control I am creating has a number of labels on it. I would like to have properties called "LabelColor" and "Text" for this class, but would like to pass in an index to tell which label to update.

For instance, here is a simplified class:
Code:
public class MyClass
{
       private ArrayList m_MyLabelList = new ArrayList();
               |
       public Color LabelColor
       {
            get
            { 
               return (m_MyLabelList[SomeIndex].ForeColor); 
            }
            set 
            { 
               m_MyLabelList[SomeIndex].ForeColor = value;
            }
       }

       public string Text
       {
            get
            { 
               return (m_MyLabelList[SomeIndex].Text); 
            }
            set 
            { 
               m_MyLabelList[SomeIndex].Text = value;
            }
       }
}
and then from code that uses this class:
Code:
       public MyClass test = new MyClass();
       Color ThisColor = test[0].Color;
       string ThisText = test[0].Text;
       test[1].Color = Color.Green;
       test[1].Text  = "This is label #1";
       test[2].Color = Color.DarkKhaki;
       test[2].Text = "This is label #2";



Any hints or pointers to code examples would be most appreciated.

Thanks,
pagates
 
Old February 24th, 2005, 12:01 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

use Indexers instead,
Code:
public Color this[int index]
{
     get
     {
          //check for a valid index 
          return (m_MyLabelList[index].ForeColor); 
     }
     set 
     { 
          //check for a valid index
          m_MyLabelList[index].ForeColor = value;
     }
}
_____________
Mehdi.
software student.
 
Old February 24th, 2005, 12:23 PM
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Mehdi, but that doesn't completely solve my problem.

In the example you've given, the indexer will (correctly) let me set the color. However, I cannot set the text.

If I try to do multiple indexers with different return types, such as:
Code:
public Color this[int index]
{
     get
     {
          //check for a valid index 
          return (m_MyLabelList[index].ForeColor); 
     }
     set 
     { 
          //check for a valid index
          m_MyLabelList[index].ForeColor = value;
     }
}

public string this[int index]
{
     get
     {
          //check for a valid index 
          return (m_MyLabelList[index].Text); 
     }
     set 
     { 
          //check for a valid index
          m_MyLabelList[index].Text = value;
     }
}

I get an "Class 'MyClass' already defines a member called 'this' with the same parameter types" error.

Is this just a shortcoming of C# (VB6 allowed indexed properties)?

Thanks,
PAGates
 
Old February 24th, 2005, 03:37 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

not shortcoming of C#(However I don't think VB6 could allow this),
if you write,
Code:
int index=0; 
object o=objMyClass[index];
compiler doesn't know what should return(string or Color)so in your case use methods simply or expose Labels in your indexer..

_____________
Mehdi.
software student.
 
Old February 24th, 2005, 03:50 PM
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Medhi,

Thanks - that's what I was afraid of. I still say it's a shortcoming (single indexer and/or non-indexed properties).

FYI - it was possible in VB6 (I used it quite a bit):
Code:
Public Property Get Text(ByVal Index As Integer) As String
    ' Do Error Checking and stuff
    Text = SomeClass(Index).Text
End Property

Public Property Let Text(ByVal Index As Integer, ByVal New_Text As String)
  ' Do Some Error Handling, etc.
  SomeClass(Index).Text = New_Text
  PropertyChanged "Text" & Index
End Property
Thanks again,
PAGates
 
Old March 15th, 2005, 08:08 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I didn't get your code,

in C# if you declare two things like,

everything TheSameName(TheSameArguments){}

compiler gives that error(IMO it's completely logical).

_____________
Mehdi.
software student.





Similar Threads
Thread Thread Starter Forum Replies Last Post
why use base[] indexers?? oooshola BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 January 31st, 2007 12:50 AM
Indexers with c# kobystud C# 2 May 28th, 2004 09:52 AM
Properties robert_83 .NET Web Services 0 July 30th, 2003 07:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.