#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    228
    Rep Power
    0

    How do I do a get/post for an array in c#?


    How do I do a get/post for an array in c#?

    In C#, you get and set variables like this:

    Code:
    public int ID { get; set; }
    How would one get and set an array in C#?

    This will not work:

    Code:
    public uint [5] BIG_Hash {get; set;}
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    You can't put the dimensions in the array when you use it as a type. If you want to enforce a limit of 5 items then you can't use the simple get;set; syntax for the property and will need some custom code (however trivial it may be).
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    228
    Rep Power
    0
    Originally Posted by requinix
    You can't put the dimensions in the array when you use it as a type. If you want to enforce a limit of 5 items then you can't use the simple get;set; syntax for the property and will need some custom code (however trivial it may be).
    How would that look like?

    I remember I did something like this months ago but I cannot remember how it is done. It is a special case for special variables. You have to add some more code.

    public uint[] BIG_Hash { get; set; }
    ...

    obj.BIG_Hash = new uint[5]; // set

    uint[] temp = obj.BIG_Hash; // get
    but the last two lines need to be inside a method, right? How would those methods look like?
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    Essentially (and I'm typing this from memory),
    Code:
    private uint[] _big_hash;
    public uint[] BIG_Hash
    {
    	get
    	{
    		return this._big_hash;
    	}
    	set
    	{
    		if (value == null)
    		{
    			// accept or reject?
    		}
    		else if (value.Length > 5)
    		{
    			// only keep the first five or throw an exception?
    		}
    		else
    		{
    			this._big_hash = value;
    		}
    	}
    }
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    228
    Rep Power
    0
    what I wonder now is where do I put the "new" statements for the public and private arrays.
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    It would go along with the uint[5] like in that example you posted about setting the value.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0
    1) A way to get to the data in its html-encoded form, where the comma used as a separator for the arrays can ONLY represent real arrays instead of customer form input (the customer input comma would remain encoded at this point).

    2) A way to simulate the PHP print_r(), var_dump() function (don't know PHP myself, but I'm told that does the trick there) to dump the variable into an array.

IMN logo majestic logo threadwatch logo seochat tools logo