use of struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Broadbent

    use of struct

    Is it only really a good idea to use a struct when all the struct data
    contains value types? I was writing one which used and exposed a public
    string array property but there are a couple of things with this.

    i. I am guessing the string array held within the struct would still be
    stored in the heap anyway.
    ii. I have noticed that the struct must be initialised using the
    constructor to set the public values since if I simply declare and set
    each property seperatly, the compiler complains that the struct is
    uninitialised.

    e.g.
    Page p; //Page is a simple struct
    string[] s = {"one", "two", "three"};
    p.ID = 1; // compiler states p is unassigned
    p.Attributes = s;

    I cannot see the point to struct in this instance where public reference
    types are used since the struct is less flexible. The following compiles..

    string[] s = {"one", "two", "three"};
    Page p = new Page(1, s);

    p.ID = 1; // I can now set the properties without compiler problem
    p.Attributes = s;

    What do you all think?

    Br,

    Mark.
  • cody

    #2
    Re: use of struct

    if you have struct

    S
    {
    public int P;
    public void Foo(){}
    }

    you can do

    S s;
    s.P = 1234;
    s.Foo();

    but you cannot do

    S s;
    s.Foo();
    s.P = 1234;

    That is because P is not a property but a field and the compiler so can
    recognize when all fields are initialized and access to a method/property of
    a struct is only allowed if every field is guaranteed to be initialized.
    [color=blue]
    > i. I am guessing the string array held within the struct would still be
    > stored in the heap anyway.[/color]

    Yes, it is. And storing a struct in an object would store the whole struct
    in the heap.


    Comment

    • Mark Broadbent

      #3
      Re: use of struct

      You would still have the same behaviour (in your code) if P was a
      property (for the value type) -and that is fine (I am happy with that)
      *but* if you tried to use a property to access a reference type the
      compiler would not like that.

      e.g.

      //this is ok.

      public struct S
      {
      int p;
      public int P
      {get {return p;}
      set {p = value;}}
      public void Foo(){}
      }

      S s;
      s.P = 1234;
      Foo();

      //this throws compiler unassigned error;

      public struct S
      {
      int p;
      string s;
      public int P
      {get {return p;}
      set {p = value;}}
      public int S
      {get {return s;}
      set {s = value;}}
      public void Foo(){}
      }

      S s;
      s.P = 1234;
      s.S = "Doesnt work"; //this code should compile since all values of
      struct have been set
      Foo();


      p.s.
      Value types are by default initialised to 0 in structs (if they haven't
      been set thru constructor).


      cody wrote:[color=blue]
      > if you have struct
      >
      > S
      > {
      > public int P;
      > public void Foo(){}
      > }
      >
      > you can do
      >
      > S s;
      > s.P = 1234;
      > s.Foo();
      >
      > but you cannot do
      >
      > S s;
      > s.Foo();
      > s.P = 1234;
      >
      > That is because P is not a property but a field and the compiler so can
      > recognize when all fields are initialized and access to a method/property of
      > a struct is only allowed if every field is guaranteed to be initialized.
      >
      >[color=green]
      >>i. I am guessing the string array held within the struct would still be
      >>stored in the heap anyway.[/color]
      >
      >
      > Yes, it is. And storing a struct in an object would store the whole struct
      > in the heap.
      >
      >[/color]

      Comment

      • cody

        #4
        Re: use of struct

        > You would still have the same behaviour (in your code) if P was a[color=blue]
        > property (for the value type) -and that is fine (I am happy with that)
        > *but* if you tried to use a property to access a reference type the
        > compiler would not like that.
        >
        > e.g.
        >
        > //this is ok.
        >
        > public struct S
        > {
        > int p;
        > public int P
        > {get {return p;}
        > set {p = value;}}
        > public void Foo(){}
        > }
        >
        > S s;
        > s.P = 1234;
        > Foo();
        >
        > //this throws compiler unassigned error;
        >
        > public struct S
        > {
        > int p;
        > string s;
        > public int P
        > {get {return p;}
        > set {p = value;}}
        > public int S
        > {get {return s;}
        > set {s = value;}}
        > public void Foo(){}
        > }
        >
        > S s;
        > s.P = 1234;
        > s.S = "Doesnt work"; //this code should compile since all values of
        > struct have been set
        > Foo();
        >
        >
        > p.s.
        > Value types are by default initialised to 0 in structs (if they haven't
        > been set thru constructor).[/color]

        This is very strange. What could be the reason of that?


        Comment

        Working...