Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. foolish question , help!

foolish question , help!

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jinzhecheng
    wrote on last edited by
    #1

    suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:

    R S L 3 Replies Last reply
    0
    • J jinzhecheng

      suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      You have already answered your question. When you create the array at each element a structure with all its members set to their default values is generated. Its like working with integers.

      1 Reply Last reply
      0
      • J jinzhecheng

        suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        There's no facility provided by the CLR to do it, you have to do it on your own. The simplest way is to have a bool variable that's set to true on any property/method access. You can then check if the bool variable is value and know that it's just been initialized. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        1 Reply Last reply
        0
        • J jinzhecheng

          suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Create a static 'Empty' variable for you struct. Similar to Rectangle/Point structs. Eg

          struct Foo
          {
          int a;
          int b;

          public readonly static Empty;

          static Foo() { Empty = new Foo() ;} //mite not even be neccesary infact,
          //should be initialized to default value
          }

          ...

          Foo[] a = new Foo[2];
          for (int i = 0; i < a.Length; i++)
          {
          if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype
          {
          Console.WriteLine("a[{0}] = Foo.Empty", i);
          }
          }

          xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          L 1 Reply Last reply
          0
          • L leppie

            Create a static 'Empty' variable for you struct. Similar to Rectangle/Point structs. Eg

            struct Foo
            {
            int a;
            int b;

            public readonly static Empty;

            static Foo() { Empty = new Foo() ;} //mite not even be neccesary infact,
            //should be initialized to default value
            }

            ...

            Foo[] a = new Foo[2];
            for (int i = 0; i < a.Length; i++)
            {
            if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype
            {
            Console.WriteLine("a[{0}] = Foo.Empty", i);
            }
            }

            xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

            L Offline
            L Offline
            lgstef
            wrote on last edited by
            #5

            I think the problem stays the same. Here in Foo struct int a, b are initialized with its default values 0. What happens if we want to set "a[0].a = a[0].b = 0"? Then the check a[0].Equals(Foo.Empty) would succeed when its not empty anymore.. I think you need some sentinel value - a special or illegal value. For example you could use -1 as a sentinel. Default int value 0 also would do but you have to be sure that this value won't be treated as legal value.

            L 1 Reply Last reply
            0
            • L lgstef

              I think the problem stays the same. Here in Foo struct int a, b are initialized with its default values 0. What happens if we want to set "a[0].a = a[0].b = 0"? Then the check a[0].Equals(Foo.Empty) would succeed when its not empty anymore.. I think you need some sentinel value - a special or illegal value. For example you could use -1 as a sentinel. Default int value 0 also would do but you have to be sure that this value won't be treated as legal value.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              It really depends on what you want. Initializing structs to 'illegal' values will have some overhead. The point here is to check if the struct has been used or not. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

              L 1 Reply Last reply
              0
              • L leppie

                It really depends on what you want. Initializing structs to 'illegal' values will have some overhead. The point here is to check if the struct has been used or not. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                L Offline
                L Offline
                lgstef
                wrote on last edited by
                #7

                not 'illegal'.. sentinel value. ...The point here is to check if the struct has been used or not.... Try this: void SomeMethod() { Foo[] a = new Foo[2]; if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 1; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 0; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); } ....................... Output: "a[0] = Foo.Empty" "a[0] != Foo.Empty" "a[0] = Foo.Empty" !!!! which means that this struct haven't been used before! ..but we used a[0] before...

                L 1 Reply Last reply
                0
                • L lgstef

                  not 'illegal'.. sentinel value. ...The point here is to check if the struct has been used or not.... Try this: void SomeMethod() { Foo[] a = new Foo[2]; if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 1; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 0; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); } ....................... Output: "a[0] = Foo.Empty" "a[0] != Foo.Empty" "a[0] = Foo.Empty" !!!! which means that this struct haven't been used before! ..but we used a[0] before...

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  In this case you will have to do some more work. Something like:

                  struct Foo
                  {
                  bool init;
                  int a;
                  int b;

                  public int A
                  {
                  get {return a;} set { init = true; a= value;}
                  }
                  public int B
                  {
                  get {return b;} set { init = true; b= value;}
                  }

                  ... the rest of previous example.
                  }

                  xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups