Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

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


  • Please log in to reply

#1
Xarzu

Xarzu

    New Member

  • Member
  • Pip
  • 2 posts
How do I do a get/post for an array in c#?

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

public int ID { get; set; }

How would one get and set an array in C#?

This will not work:

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

  • 0

Advertisements


#2
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
Hi Xarzu,

The syntax for your property is incorrect, you can't set the array size in the property:
public uint[] BIG_Hash { get; set; }

You use it like this:
BIG_Hash = new uint[5];

Regards,

Ax
  • 0

#3
AceInfinity

AceInfinity

    Visiting Staff

  • Visiting Consultant
  • 34 posts
  • MVP

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

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

public int ID { get; set; }

How would one get and set an array in C#?

This will not work:

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


That's not how you define a property in C#. What you should do is have a backing value which is set to from the setter of that property, and the getter retrieves the current value of this backing.

private uint[] _bigHash = new uint[5];
public uint[] bigHash
{
	get { return _bigHash; }
	set { _bigHash = value; }
}

Edited by AceInfinity, 07 October 2012 - 06:54 PM.

  • 1

#4
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
Sorry to drag this back from the dead, but I feel the need to set the record straight. The "get; set;" syntax is valid, they're called auto-implemented properties in C#:
http://msdn.microsof...y/bb384054.aspx

The problem with the OP's code is that you can't set the size of the array with the auto-implemented property syntax.

Cheers!

Ax
  • 0

#5
AceInfinity

AceInfinity

    Visiting Staff

  • Visiting Consultant
  • 34 posts
  • MVP

Sorry to drag this back from the dead, but I feel the need to set the record straight. The "get; set;" syntax is valid, they're called auto-implemented properties in C#:
http://msdn.microsof...y/bb384054.aspx

The problem with the OP's code is that you can't set the size of the array with the auto-implemented property syntax.

Cheers!

Ax


Yes, but that was already explained... Read my post. I'm sure he was aware that they were auto implemented properties, otherwise he wouldn't have them in his code, but the issue was not understanding why he couldn't set a fixed size to that property. The solution to that is as easy as understanding that properties are meant for encapsulation. It's the same reason why you can't do this in C#:
static int[5] GetArray()
{

}

With all respect, unfortunately you brought this thread back to life with nothing new put on the table from what was already known back then... (from post #3 and earlier within this thread).

Edited by AceInfinity, 02 July 2013 - 11:29 PM.

  • 0

#6
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
Sorry, but to me, this was ambiguous/potentially misleading:

That's not how you define a property in C#.


I wasn't disagreeing or arguing with you, I was merely trying to clarify for posterity sake that auto-implemented properties are acceptable in C#, i.e. you can define properties using "get; set;". I apologize for any confusion.

Cheers! :cool:
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP