Results 1 to 3 of 3

Thread: [2.0] Property Args

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    102

    [2.0] Property Args

    Hi guys,
    Just wondering if someone can help here, I asked a few people I know and also tried many ways myself aswell. Im trying to perform a property in C# which accepts an Index argument. I have the code working in VB.NET but doesn't port direct to c#. Any Ideas?

    VBCODE
    Code:
        Public Property Randomizer(ByVal Index As Integer) As Int64
            Get
                Return _Randomizer(Index)
            End Get
            Set(ByVal value As Int64)
                _Randomizer(Index) = value
            End Set
        End Property

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Property Args

    It's called an indexer in C#:
    Code:
    public object this[int index]
    {
        get { /* return the specified index here */ }
        set { /* set the specified index to value here */ }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Property Args

    Strictly speaking a C# indexer is not the same thing as a VB indexed property. In VB you can have as many indexed properties as you like, but only one of them can be declared Default. The Default property is the one whose name you can omit when using it, so it looks like you're indexing the class itself. For instance, The ArrayList.Item property is declared Default. You can access it like this:
    vb.net Code:
    1. myObject = myArrayList.Item(i)
    or like this:
    vb.net Code:
    1. myObject = myArrayList(i)
    in C# that's:
    vb.net Code:
    1. myObject = myArrayList.Item[i];
    and:
    vb.net Code:
    1. myObject = myArrayList[i];
    A C# indexer is equivalent to a VB Default Property. In VB Default properties MUST be indexed.

    Now, as I said, in VB you can have as many indexed properties as you like, but only the one declared Default can be omitted in code. In C# you can have one indexer and that is the only indexed property you're allowed. Now, having said this, it is possible to overload properties so if the number or type of the index arguments vary then, strictly speaking, you can have multiple indexers in C#. It is considered bad practice to have overloaded indexers return different data though. Generally it will be the same data indexed by, say, ordinal index and string key.

    Because VB indexed properties are named, you can have as many as you like with the same argument list, which is absolutely not possible in C#.

    In summary, the code in post #1 actually has no direct translation in C#, while the code in post #2 actually translates directly to this:
    Code:
    Default Public Property Item(ByVal index As Integer) As Object
        Get
            'return the specified index here
        End Get
        Set(ByVal value As Object)
            'set the specified index to value here
        End Set
    End Property
    If your VB class only has one indexed property then it's not a problem, although you will not be able to name it Randomizer in C#. If your VB class has another indexed property declared default though, then you're stuffed. You'd have to declare Randomizer as discrete getter and setter methods rather than as a property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width