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. structs and 'using'

structs and 'using'

Scheduled Pinned Locked Moved C#
questioncsharpcomtoolshelp
9 Posts 4 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.
  • N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #1

    A question for C# language experts: If I have a struct that implements IDisposable:

    struct C : IDisposable
    {
    public int clan;
    public void Dispose()
    {
    Console.WriteLine("Disposing");
    }
    }

    And later in the code something like:

    using (C s = new C())
    {
    s.clan = 1;
    }

    A compiler error is reported:

    error CS0131: The left-hand side of an assignment must be a variable, property or indexer

    :confused:


    My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

    L K N A 4 Replies Last reply
    0
    • N Nemanja Trifunovic

      A question for C# language experts: If I have a struct that implements IDisposable:

      struct C : IDisposable
      {
      public int clan;
      public void Dispose()
      {
      Console.WriteLine("Disposing");
      }
      }

      And later in the code something like:

      using (C s = new C())
      {
      s.clan = 1;
      }

      A compiler error is reported:

      error CS0131: The left-hand side of an assignment must be a variable, property or indexer

      :confused:


      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

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

      The problem lies with the fact that structs cant be safely casted, in this case IDisposable. The compiler will typical generate the following for the 'using' block:

      IDisposable o = new C();
      try
      {
      C s = o as C; //this is not allowable for structs,
      // lets assume it would then it would go out of scope before Dispose can be called
      // your code here
      }
      finally
      {
      o.Dispose();
      }

      Anyways, my best guess :p top secret
      Download xacc-ide 0.0.3 now!
      See some screenshots

      N 1 Reply Last reply
      0
      • L leppie

        The problem lies with the fact that structs cant be safely casted, in this case IDisposable. The compiler will typical generate the following for the 'using' block:

        IDisposable o = new C();
        try
        {
        C s = o as C; //this is not allowable for structs,
        // lets assume it would then it would go out of scope before Dispose can be called
        // your code here
        }
        finally
        {
        o.Dispose();
        }

        Anyways, my best guess :p top secret
        Download xacc-ide 0.0.3 now!
        See some screenshots

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        Hmmmm, but using as with structs causes

        error CS0077: The as operator must be used with a reference type ('sharptest.C' is a value type)

        And my code complains about assigning a value to a member of s. Also, this compiles (and works) fine:

        using (C s = new C())
        {
        Console.WriteLine(s.clan.ToString());
        }


        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

        1 Reply Last reply
        0
        • N Nemanja Trifunovic

          A question for C# language experts: If I have a struct that implements IDisposable:

          struct C : IDisposable
          {
          public int clan;
          public void Dispose()
          {
          Console.WriteLine("Disposing");
          }
          }

          And later in the code something like:

          using (C s = new C())
          {
          s.clan = 1;
          }

          A compiler error is reported:

          error CS0131: The left-hand side of an assignment must be a variable, property or indexer

          :confused:


          My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

          K Offline
          K Offline
          Ketty Avashia
          wrote on last edited by
          #4

          If U have the struct in the same namespace as ur code the code below works. C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); and if in different namespace -- in that case using can follow the initial using block. I mean immediately after using System; using System.Data; .... using NameSpaceName.C; C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); I hope this help and that i have not missunderstood ur question.:rolleyes: Ketty

          N 1 Reply Last reply
          0
          • K Ketty Avashia

            If U have the struct in the same namespace as ur code the code below works. C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); and if in different namespace -- in that case using can follow the initial using block. I mean immediately after using System; using System.Data; .... using NameSpaceName.C; C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); I hope this help and that i have not missunderstood ur question.:rolleyes: Ketty

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            Ketty Avashia wrote: I hope ... that i have not missunderstood ur question. Thanks, but you have :) I asked about using Statement[^], not using Directive[^].


            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

            1 Reply Last reply
            0
            • N Nemanja Trifunovic

              A question for C# language experts: If I have a struct that implements IDisposable:

              struct C : IDisposable
              {
              public int clan;
              public void Dispose()
              {
              Console.WriteLine("Disposing");
              }
              }

              And later in the code something like:

              using (C s = new C())
              {
              s.clan = 1;
              }

              A compiler error is reported:

              error CS0131: The left-hand side of an assignment must be a variable, property or indexer

              :confused:


              My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              According to the C# language spec[^]: Local variables declared in a resource-acquisition are read-only, and must include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (by assignment or the ++ and -- operators) or pass them as ref or out parameters. Wow!


              My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

              1 Reply Last reply
              0
              • N Nemanja Trifunovic

                A question for C# language experts: If I have a struct that implements IDisposable:

                struct C : IDisposable
                {
                public int clan;
                public void Dispose()
                {
                Console.WriteLine("Disposing");
                }
                }

                And later in the code something like:

                using (C s = new C())
                {
                s.clan = 1;
                }

                A compiler error is reported:

                error CS0131: The left-hand side of an assignment must be a variable, property or indexer

                :confused:


                My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                A Offline
                A Offline
                Ami Bar
                wrote on last edited by
                #7

                Structures are allocated on the stack and not on the heap as classes. Write the following code instead, it does the same trick: C s = new C(); using (s) { s.clan = 1; } Ami

                N 1 Reply Last reply
                0
                • A Ami Bar

                  Structures are allocated on the stack and not on the heap as classes. Write the following code instead, it does the same trick: C s = new C(); using (s) { s.clan = 1; } Ami

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #8

                  Ami Bar wrote: Structures are allocated on the stack and not on the heap as classes. That is correct in this context - I just didn't know that variables declared in using nlock need to be read-only. BTW, what exactly "read-only" really means in C#? It does not have const methods like C++. Whatever... Thanks for your answer :)


                  My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                  A 1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    Ami Bar wrote: Structures are allocated on the stack and not on the heap as classes. That is correct in this context - I just didn't know that variables declared in using nlock need to be read-only. BTW, what exactly "read-only" really means in C#? It does not have const methods like C++. Whatever... Thanks for your answer :)


                    My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                    A Offline
                    A Offline
                    Ami Bar
                    wrote on last edited by
                    #9

                    C# doesn't have const and its a petty. "The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.", MSDN Here is the link to the full readonly keyword explanation in the MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfreadonlypg.asp

                    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