none
C# Structures RRS feed

  • Question

  • This is some helpful information of My S/W

    Visual C# 2008 Express Edition
    Microsoft Visual Studio 2008
    Version 9.0.30729.1 SP

    Microsoft .NET Framework
    Version 3.5 SP1

    --------------------------------------------

    I don't know anything about get and set used in structures

    The following I'm posting is the code I got from MSDN online Library :

    // struct1.cs
    using System;
    struct SimpleStruct
    {
    private int xval;
    public int X
    {
    get // <--- What does it ?
    {
    return xval;
    }
    set // <==== What does it ?
    {
    if (value < 100)
    xval = value;
    }
    }
    public void DisplayX()
    {
    Console.WriteLine("The stored value is: {0}", xval);
    }
    }

    class TestClass
    {
    public static void Main()
    {
    SimpleStruct ss = new SimpleStruct();
    ss.X = 5;
    ss.DisplayX();
    }
    }

    Saturday, July 24, 2010 3:53 PM

Answers

  • You would probably get a better response in one of the C# forums (for instance: http://social.msdn.microsoft.com/Forums/en/Vsexpressvcs/threads), instead of the Training and Certification forum.

    Anyway, get and set in a structure perform exactly the same function as when they are used in a class: they are used for defining a Property. The property, when it is accessed from code written elsewhere, looks the same as a Field (i.e., a viriable such as "int x;"). However, the values assigned to, or read from, that "variable" need to go accross the code written in the get and set parts. This lets you add some checks or perform operations when values are assigned to your "variable" (property). For instance, the code in your sample prevents any value greater or equal to 100 from being assigned to x.

    Saturday, July 24, 2010 4:52 PM

All replies

  • You would probably get a better response in one of the C# forums (for instance: http://social.msdn.microsoft.com/Forums/en/Vsexpressvcs/threads), instead of the Training and Certification forum.

    Anyway, get and set in a structure perform exactly the same function as when they are used in a class: they are used for defining a Property. The property, when it is accessed from code written elsewhere, looks the same as a Field (i.e., a viriable such as "int x;"). However, the values assigned to, or read from, that "variable" need to go accross the code written in the get and set parts. This lets you add some checks or perform operations when values are assigned to your "variable" (property). For instance, the code in your sample prevents any value greater or equal to 100 from being assigned to x.

    Saturday, July 24, 2010 4:52 PM
  • ok thanks alot.

    Your knowledge is great !!

    But can you Explain in detail ? Or can you kindly help via giving hyper-link(s) towards this issue.

    Saturday, July 24, 2010 5:24 PM
  • what about :

    http://msdn.microsoft.com/en-us/library/ms173109.aspx ?

    Sunday, July 25, 2010 2:22 PM