Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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