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. Constant Reference

Constant Reference

Scheduled Pinned Locked Moved C#
csharpc++
5 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.
  • S Offline
    S Offline
    SteveUK
    wrote on last edited by
    #1

    Is there no way to pass an object by constant reference as you can in C++. It doesn't seem to be possible in c#. There must be some way of passing an object into a function and guaranteeing that the function does no change the object. This is a pretty basic and essential feature for writing robust code.

    D L A 3 Replies Last reply
    0
    • S SteveUK

      Is there no way to pass an object by constant reference as you can in C++. It doesn't seem to be possible in c#. There must be some way of passing an object into a function and guaranteeing that the function does no change the object. This is a pretty basic and essential feature for writing robust code.

      D Offline
      D Offline
      dynamic
      wrote on last edited by
      #2

      here's an example of 3 different constants in C# VB:


      Public Const int PBM_SETBKCOLOR = 0x2001;
      Public Const int PBM_SETBARCOLOR = 0x409;
      Public Const int WM_CLOSE = 0x10;


      hope that helps.

      My Signature


      Private void ExpectingTwins(string twins)
      {
      switch(twins)
      {
      Case ("twins on the way"):
      MessageBox.Show("for mr and mrs dynamic","twins on the way");
      break;
      }
      }


      J 1 Reply Last reply
      0
      • D dynamic

        here's an example of 3 different constants in C# VB:


        Public Const int PBM_SETBKCOLOR = 0x2001;
        Public Const int PBM_SETBARCOLOR = 0x409;
        Public Const int WM_CLOSE = 0x10;


        hope that helps.

        My Signature


        Private void ExpectingTwins(string twins)
        {
        switch(twins)
        {
        Case ("twins on the way"):
        MessageBox.Show("for mr and mrs dynamic","twins on the way");
        break;
        }
        }


        J Offline
        J Offline
        Jesse Vogt
        wrote on last edited by
        #3

        I think SteveUK is talking about something like this in c++: void foo( const string& text ); According to Programming C# by Jesse Liberty you can pass by reference but I was not able to find anything about passing by const reference: void foo( ref string text ) { ... } The only ways I was able to find on passing parameters are: by-val, by-ref, and with the "out" modifier (which appears to be similar to by-ref.) Hope this helps. jv

        1 Reply Last reply
        0
        • S SteveUK

          Is there no way to pass an object by constant reference as you can in C++. It doesn't seem to be possible in c#. There must be some way of passing an object into a function and guaranteeing that the function does no change the object. This is a pretty basic and essential feature for writing robust code.

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

          SteveUK wrote: There must be some way of passing an object into a function and guaranteeing that the function does no change the object. This is a pretty basic and essential feature for writing robust code. This is pretty much built into .NET. The following shows you:

          Foo(object o)
          {
          o = null;
          }

          Main()
          {
          Object o = new Object();
          Foo(o);
          WriteLine(o); //o still exists
          }

          leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          1 Reply Last reply
          0
          • S SteveUK

            Is there no way to pass an object by constant reference as you can in C++. It doesn't seem to be possible in c#. There must be some way of passing an object into a function and guaranteeing that the function does no change the object. This is a pretty basic and essential feature for writing robust code.

            A Offline
            A Offline
            Andy Smith
            wrote on last edited by
            #5

            non-editable parameters is not supported by the CLS nor c#. In order to simulate this, you can make a clone of the object you are passing in.

            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