Properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nak

    #1

    Properties

    Hi there,

    I have only just started using C# after becoming more than competent
    with VB.NET. I have come across something that has confused me slightly, it
    regards properties.

    In VB.NET it is possible to pass a value to a property (similar to an
    Indexer) but without it actually being an indexer, for example

    Public Property layers(ByVal iKey As Object) As Byte(,)
    Get
    'get an item out of a hastable object, using iKey as the key
    End Get
    Set(ByVal Value As Byte(,))
    'set an item in a hashtable, using iKey as the key
    End Set
    End Property

    I would have thought that you could write something very similar in C#,
    by writing,

    public Byte[,] layers(Object iKey)
    {
    get
    {
    'get an item out of a hastable object, using iKey as the key
    }
    set
    {
    'set an item in a hashtable, using iKey as the key
    }
    }

    But it seems that I am very wrong, and indeed even the Hashtable class
    is used slightly differently in C#, rather than using a property that would
    be layed out very similar to the above you have to use an Indexer, this is
    all very well but you can only have 1 indexer per class as you can't even
    name the indexer.

    Is the only way to get around this to have 2 methods to access the
    object, 1 to set and 1 to get? Would that be why some classes have
    setMyValue rather than MyValue? Thanks loads in advance

    Nick.

    --
    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    "No matter. Whatever the outcome, you are changed."

    Fergus - September 5th 2003
    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


  • Jon Skeet [C# MVP]

    #2
    Re: Properties

    Nak <a@a.com> wrote:[color=blue]
    > In VB.NET it is possible to pass a value to a property (similar to an
    > Indexer) but without it actually being an indexer, for example[/color]

    <snip>

    Basically, this (named indexers) isn't possible in C#, unfortunately.
    You can give the indexer a name for use in other languages, but you
    can't have more than one indexer with a given signature, and you can't
    use the names within C# code.
    [color=blue]
    > But it seems that I am very wrong, and indeed even the Hashtable class
    > is used slightly differently in C#, rather than using a property that would
    > be layed out very similar to the above you have to use an Indexer, this is
    > all very well but you can only have 1 indexer per class as you can't even
    > name the indexer.
    >
    > Is the only way to get around this to have 2 methods to access the
    > object, 1 to set and 1 to get? Would that be why some classes have
    > setMyValue rather than MyValue? Thanks loads in advance[/color]

    One thing you can do is have a property which returns something *else*
    which has an indexer. It's very ugly though.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Nak

      #3
      Re: Properties

      Hi there,
      [color=blue]
      > Basically, this (named indexers) isn't possible in C#, unfortunately.
      > You can give the indexer a name for use in other languages, but you
      > can't have more than one indexer with a given signature, and you can't
      > use the names within C# code.[/color]

      Cool, well, not so cool, but I was thinking for a moment that it might have
      been SharpDevelop, thanks for the help, I'll use 2 methods per read/write
      indexed property. Thanks again :-)

      Nick.

      --
      /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
      "No matter. Whatever the outcome, you are changed."

      Fergus - September 5th 2003
      /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


      Comment

      Working...