CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Indexers

  1. #1
    Join Date
    May 2006
    Posts
    306

    Indexers

    How can I define a custom object/class, but also for it to have an indexer, so that I can get values via something like this.

    Code:
    var value = group1["bob"]; //return a person class, for example
    var planet = System[2] || System["Earth"]
    It works much like this.

    Code:
    myForm["username"].value

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Smile Re: Indexers

    I presume it is Javascript. You can use an index instead of an ID, e.g.:
    Code:
    <html>
    <head>
    <script language="JavaScript">
    function process_by_id()
    {
      document.getElementById("foo").value = "abc";
      document.getElementById("bar").value = "def";
      return true;
    }
    function process_by_index()
    {
      // alert(document.form1.elements.length);
      document.form1.elements[0].value = "ABC";
      document.form1.elements[1].value = "DEF";
      return true;
    }
    </script>
    </head>
    <body>
    <form id="form1" name="form1">
    <input type="text" id="foo" value="foo">
    <input type="text" id="bar" value="bar">
    <input type="button" value="Change element by id" onClick="javascript:process_by_id();">
    <input type="button" value="Change element by index" onClick="javascript:process_by_index();">
    </form>
    </body>
    </html>

  3. #3
    Join Date
    May 2006
    Posts
    306

    Re: Indexers

    No, you don't understand what I want.

    I want this.
    Code:
    var Groups =
    {
        _List: [],
        get this() { return _List; }
    };
    If the javascript parser could take "get this() {}" as itself, rather then a property named "this", you could access it like this.
    Code:
    Groups["group1"] = ["bob", "mike", "tom"];
    var group1 = Groups[0];
    How can you have a custom variable/class that has an indexer?

    Do I just have to make an object have array
    Last edited by code?; June 15th, 2009 at 02:19 PM.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Indexers

    I'm confused as to why you don't just use multidimensional arrays. Why can't your first dimension be indexed, and your second dimension be associative?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

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

Featured