Const Object Reference in C# function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shreyas Ranade

    #1

    Const Object Reference in C# function

    I want to pass an Object Reference to a C# function, but also I want to make
    sure that the calling function should not change it's value.
    The C++ Code Like

    MyClass cls;
    Fun1(cls)

    void Fun1(const MyClass & cls)
    {
    int a = cls.m_No;
    }


    I want to write same code in C#. How should I write because
    you can not send Cont Object Reference to a function in C#.

    --
    Shreyas S. Ranade


  • Greg Ewing [MVP]

    #2
    Re: Const Object Reference in C# function

    Shreyas, there isn't a straighforward way to do this. One work around would
    be only providing a getter like Morten suggested. You could also use a
    Field instead of a property and apply the readonly attribute to the field.
    Not sure which will work with your exact scenario.

    --
    Greg Ewing [MVP]




    "Shreyas Ranade" <shreyas_ranade @hotmail.com> wrote in message
    news:Ot$DH8tcDH A.736@TK2MSFTNG P09.phx.gbl...[color=blue]
    > I want to pass an Object Reference to a C# function, but also I want to[/color]
    make[color=blue]
    > sure that the calling function should not change it's value.
    > The C++ Code Like
    >
    > MyClass cls;
    > Fun1(cls)
    >
    > void Fun1(const MyClass & cls)
    > {
    > int a = cls.m_No;
    > }
    >
    >
    > I want to write same code in C#. How should I write because
    > you can not send Cont Object Reference to a function in C#.
    >
    > --
    > Shreyas S. Ranade
    >
    >[/color]


    Comment

    • Darren Oakey

      #3
      Re: Const Object Reference in C# function

      My favorite way is to have an interface for every class which is
      changeable -

      ie
      interface ConstMyClass
      {
      int number {get;}
      string value {get;}
      ConstMyClass CombineWith( ConstMyClass);
      ...
      }

      then have your class implement that
      ie
      class MyClass : ConstMyClass
      {
      ....
      }

      then most of the places you use it, you can define
      void Fun1( ConstMyClass x )
      {
      }

      and be 100% guaranteed that your object isn't going to be manipulated.







      "Shreyas Ranade" <shreyas_ranade @hotmail.com> wrote in message
      news:Ot$DH8tcDH A.736@TK2MSFTNG P09.phx.gbl...[color=blue]
      > I want to pass an Object Reference to a C# function, but also I want to[/color]
      make[color=blue]
      > sure that the calling function should not change it's value.
      > The C++ Code Like
      >
      > MyClass cls;
      > Fun1(cls)
      >
      > void Fun1(const MyClass & cls)
      > {
      > int a = cls.m_No;
      > }
      >
      >
      > I want to write same code in C#. How should I write because
      > you can not send Cont Object Reference to a function in C#.
      >
      > --
      > Shreyas S. Ranade
      >
      >[/color]


      Comment

      • Jon Skeet

        #4
        Re: Const Object Reference in C# function

        Darren Oakey <you@darrenoake y.info> wrote:[color=blue]
        > My favorite way is to have an interface for every class which is
        > changeable -
        >
        > ie
        > interface ConstMyClass
        > {
        > int number {get;}
        > string value {get;}
        > ConstMyClass CombineWith( ConstMyClass);
        > ...
        > }
        >
        > then have your class implement that
        > ie
        > class MyClass : ConstMyClass
        > {
        > ...
        > }
        >
        > then most of the places you use it, you can define
        > void Fun1( ConstMyClass x )
        > {
        > }
        >
        > and be 100% guaranteed that your object isn't going to be manipulated.[/color]

        Unfortunately it doesn't, actually - because the client can always
        check whether or not the instance is *actually* an instance of MyClass,
        cast it, and then manipulate it.

        What you *can* do is have a wrapper which *contains* an instance of
        MyClass, and delegates read access to it, eg:

        class ConstMyClass
        {
        MyClass target;

        public ConstMyClass (MyClass target)
        {
        this.target = target;
        }

        public int Number
        {
        get { return target.Number; }
        }
        }

        etc

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...