Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Indexer in variable without index

Discussion in 'Scripting' started by qqqqqqqqqqqqqqqqq1, Jul 4, 2020.

  1. qqqqqqqqqqqqqqqqq1

    qqqqqqqqqqqqqqqqq1

    Joined:
    Jun 3, 2020
    Posts:
    13
    I'm trying to get following to work:
    Code (CSharp):
    1.  
    2. class Cover<T>
    3. {
    4.  
    5.    T something;
    6.  
    7.    public T this
    8.    {
    9.       get
    10.       {
    11.          return something;
    12.       }
    13.       set
    14.       {
    15.          something = value;
    16.       }
    17.    }
    18.  
    19.    public void Something(T variable)
    20.    {
    21.    }
    22.  
    23.    public void Test() {
    24.       Cover<int> something = new Cover<int>();
    25.    }
    26.  
    27. }
    Regular
    int something { get; set; }
    is almost there, but is not an option.

    I want to add additional functions to select few variables, but the variable itself cannot be public. The context is rather complicated. I would preferably solve this without
    T Get()
    and
    void Set(T newValue)
    . Is there a way to have
    Cover
    refer itself when the item it contains is not a KeyValuePair?
     
    Last edited: Jul 4, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    It's not entirely clear what you're trying to accomplish or how you would expect to use that if it worked. Are you wanting to be able to write
    Code (CSharp):
    1. Cover<int> foo;
    2. foo[] = 7;
    ?

    You definitely can't make it so that a naked variable name evaluates to something other than that variable (e.g. you cannot just have
    foo = 7
    ). That would be absurdly confusing and would implicitly block a bunch of important language features.

    Why exactly is
    int something { get; set; }
    not an option?

    Whatever you're trying to accomplish, remember that properties are just a weird syntax for defining functions. They have only cosmetic differences from regular functions. So if the reason you don't want to do this in an ordinary way is something other than cosmetic, you're probably out of luck.
     
    qqqqqqqqqqqqqqqqq1 likes this.
  3. qqqqqqqqqqqqqqqqq1

    qqqqqqqqqqqqqqqqq1

    Joined:
    Jun 3, 2020
    Posts:
    13
    That's a bummer. Thanks.