• (3 Pages)
  • +
  • 1
  • 2
  • 3

C# Learning Series - Properties

#31 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,643
  • Joined: 02-June 10

Posted 26 April 2014 - 08:07 AM

Are you trying to merge my tutorial with some other lesson you're reading? Because what you have here doesn't follow what I wrote.

First: Avoid making fields and properties called "Value". Value is already a keyword in C# so trying to a make a field with that word is going to cause nothing bug grief.

Second: Is there a reason you're using a struct instead of a class? Generally in C# you don't see a lot of struct use unless there is a very specific reason for it, generally because of some interaction with an API/DLL written in a language that needs them.
Notice here that if you use a class instead of a struct it works as expected:
Spoiler


Structs are very weird, and mutable structs even worse to deal with, because you're actually dealing with copies of the struct. Rather than repeat a bunch of explanations easily found with a search for C# struct property vs field, I'll just direct you to a good explanation about it so the author of that article can get the credit they deserve. Read this article
Was This Post Helpful? 0
  • +
  • -

#32 Gergan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 14

Posted 29 April 2014 - 02:23 PM

Thank you for your answer, I really appreciate it.
Well I'm reading, learning anything I can find about property, I read your tutorial too and it's awesome, not just this about propertys, every of them is pretty good! But while I was learning about them (prop.) I find somewhere some block of code, and I tryed to understand it, I tested a lot, even with classes, but I had error, reason why I had error was next: I wrote exacly same code as you did, but missed this 2 lines of code

" holder.StructAsField = new MutableStruct();"
and
"holder.StructAsProperty = new MutableStruct();"

I thought I can add value to StructAsField && StructAsProperty, without using "=new.." keyword.

Anyway, thank you very much for your answer, and for links you gave me. I'm gonna read them now
Was This Post Helpful? 0
  • +
  • -

#33 DEnumber50  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 15-February 14

Posted 02 July 2014 - 07:22 AM

This is a well written tutorial. Thanks for the information.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3