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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C++ guy missing pointers

C++ guy missing pointers

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpc++help
9 Posts 4 Posters 1 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.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    Hi, I have a problem in C#, which in C++ i would solve within seconds using pointers/references :) But how to solve this in C# I will try to explain my problem with an example: I have two classes Data & Formatter

      public class Data
      { 
        public string strText;
        Formatter formatProtocol;
        public Data()
        {
          formatProtocol = new Formatter();
        }
      }
    
      public class Formatter
      {
        public Formatter() {}
    
        void FormatNow()
        {
          //
          //  I need to format the parent object strText member
          //
        }
      }
    

    How do I let the Formatter change the strText member ??? How should i solve this if it wasn't a string but a ValueType ? codito ergo sum -- modified at 4:02 Thursday 16th March, 2006

    V J M 3 Replies Last reply
    0
    • B BadKarma

      Hi, I have a problem in C#, which in C++ i would solve within seconds using pointers/references :) But how to solve this in C# I will try to explain my problem with an example: I have two classes Data & Formatter

        public class Data
        { 
          public string strText;
          Formatter formatProtocol;
          public Data()
          {
            formatProtocol = new Formatter();
          }
        }
      
        public class Formatter
        {
          public Formatter() {}
      
          void FormatNow()
          {
            //
            //  I need to format the parent object strText member
            //
          }
        }
      

      How do I let the Formatter change the strText member ??? How should i solve this if it wasn't a string but a ValueType ? codito ergo sum -- modified at 4:02 Thursday 16th March, 2006

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      Please post into the C++ forum. This is the C# forum. No hurries, no worries.

      B 1 Reply Last reply
      0
      • V V 0

        Please post into the C++ forum. This is the C# forum. No hurries, no worries.

        B Offline
        B Offline
        BadKarma
        wrote on last edited by
        #3

        I know, I need to know how to solve this problem in C# codito ergo sum

        V 1 Reply Last reply
        0
        • B BadKarma

          I know, I need to know how to solve this problem in C# codito ergo sum

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          OK, my apologies, you can create a getter/setter method (more Java based) or a property (C# based) This is exactly the concept of incapsulation. so to get to a private member of your class you can do this: C# based: public string AStringName{ get{ return theprivatemember; } set{ theprivatemember = value; } } This allows you to check if value has correct value (according to your needs) before actually setting them. eg.: classinstance.AStringName = "Hello CodeProject!"; will result in the theprivatemember = "Hello CodeProject!" You can do the same for int, arrays and even objects. good luck! No hurries, no worries.

          B 1 Reply Last reply
          0
          • B BadKarma

            Hi, I have a problem in C#, which in C++ i would solve within seconds using pointers/references :) But how to solve this in C# I will try to explain my problem with an example: I have two classes Data & Formatter

              public class Data
              { 
                public string strText;
                Formatter formatProtocol;
                public Data()
                {
                  formatProtocol = new Formatter();
                }
              }
            
              public class Formatter
              {
                public Formatter() {}
            
                void FormatNow()
                {
                  //
                  //  I need to format the parent object strText member
                  //
                }
              }
            

            How do I let the Formatter change the strText member ??? How should i solve this if it wasn't a string but a ValueType ? codito ergo sum -- modified at 4:02 Thursday 16th March, 2006

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            If you used to solve it in seconds using references using C++, why cant you solve it in seconds using references in C# :confused: AM I missing something? public class Data { public string strText; Formatter formatProtocol; public Data() { formatProtocol = new Formatter(this); // pass a reference to myself } } public class Formatter { private Data toFormat public Formatter(Data theData) { this.toFormat = theData } void FormatNow() { this.toFormat.strData = "Hello reference"; } }

            B 1 Reply Last reply
            0
            • J J4amieC

              If you used to solve it in seconds using references using C++, why cant you solve it in seconds using references in C# :confused: AM I missing something? public class Data { public string strText; Formatter formatProtocol; public Data() { formatProtocol = new Formatter(this); // pass a reference to myself } } public class Formatter { private Data toFormat public Formatter(Data theData) { this.toFormat = theData } void FormatNow() { this.toFormat.strData = "Hello reference"; } }

              B Offline
              B Offline
              BadKarma
              wrote on last edited by
              #6

              Thanks for the response. I see few problems with this approach. 1. The Formatter has to know the structure of the Data object this will lead to difficulties when I need to support multiple non-inherented classes Data1, Data2; 2. This alsoo breaks security when the modifier of the member is protected or private, here it needs to be public. Or can I make the two class friend of each other. 3. I'm not sure about this one, but isn't there a Garbage collection issue. I mean both objects point to each other. But I can only 'delete' one (Set the Data object = null) In c++ i'm used to pass only the data that the other class needs to know/change. If it only was a function I could pass the string with the ref or out token. But i need to store it for later processing. I'm really stuck here :^) codito ergo sum

              J 1 Reply Last reply
              0
              • V V 0

                OK, my apologies, you can create a getter/setter method (more Java based) or a property (C# based) This is exactly the concept of incapsulation. so to get to a private member of your class you can do this: C# based: public string AStringName{ get{ return theprivatemember; } set{ theprivatemember = value; } } This allows you to check if value has correct value (according to your needs) before actually setting them. eg.: classinstance.AStringName = "Hello CodeProject!"; will result in the theprivatemember = "Hello CodeProject!" You can do the same for int, arrays and even objects. good luck! No hurries, no worries.

                B Offline
                B Offline
                BadKarma
                wrote on last edited by
                #7

                No apologie needed (no offense taken :) ) this solves the private/protected issue of my problem but i still need to pass and store a reference to the Parent object. See my reply[^] on the solution J4amieC of to understand what i mean. I have used this simple example to state what I need, I know that the suggested solution works perfect in this case, but the real object model is more complex then this. codito ergo sum

                1 Reply Last reply
                0
                • B BadKarma

                  Thanks for the response. I see few problems with this approach. 1. The Formatter has to know the structure of the Data object this will lead to difficulties when I need to support multiple non-inherented classes Data1, Data2; 2. This alsoo breaks security when the modifier of the member is protected or private, here it needs to be public. Or can I make the two class friend of each other. 3. I'm not sure about this one, but isn't there a Garbage collection issue. I mean both objects point to each other. But I can only 'delete' one (Set the Data object = null) In c++ i'm used to pass only the data that the other class needs to know/change. If it only was a function I could pass the string with the ref or out token. But i need to store it for later processing. I'm really stuck here :^) codito ergo sum

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  BadKarma wrote:

                  The Formatter has to know the structure of the Data object

                  Isnt this true of anything termed a "formatter"? If I am to format a word document, I have to have the word document right? I cant format it it i cant see it!

                  BadKarma wrote:

                  This alsoo breaks security when the modifier of the member is protected or private, here it needs to be public. Or can I make the two class friend of each other.

                  Make them internal if you like, or a better solution is for all data structures to implement an interface and take that interface in the ctor of the formatter.

                  BadKarma wrote:

                  I'm not sure about this one, but isn't there a Garbage collection issue. I mean both objects point to each other. But I can only 'delete' one (Set the Data object = null)

                  No issue, it will all be taken care of for you by the runtime.

                  1 Reply Last reply
                  0
                  • B BadKarma

                    Hi, I have a problem in C#, which in C++ i would solve within seconds using pointers/references :) But how to solve this in C# I will try to explain my problem with an example: I have two classes Data & Formatter

                      public class Data
                      { 
                        public string strText;
                        Formatter formatProtocol;
                        public Data()
                        {
                          formatProtocol = new Formatter();
                        }
                      }
                    
                      public class Formatter
                      {
                        public Formatter() {}
                    
                        void FormatNow()
                        {
                          //
                          //  I need to format the parent object strText member
                          //
                        }
                      }
                    

                    How do I let the Formatter change the strText member ??? How should i solve this if it wasn't a string but a ValueType ? codito ergo sum -- modified at 4:02 Thursday 16th March, 2006

                    M Offline
                    M Offline
                    mcljava
                    wrote on last edited by
                    #9

                    I know your pain, I was experience C/C++ withdrwals too. Here's my take: you don't need any pointers in managed safe code. In system code (i.e. unmanaged you can still use them). Now with that said most applications are more easily maintained and written if they leverage the C# managed classes. You still have Ref and plus a new Qualifier call Out. But instead of operating on addresses, all C# logic is based on arrays and indices. So I could have an 100 item array of int called myList. If I call a funtion called AddTheValues it might look like this: public long AddTheValues( int[] numbers ) { long total = 0; // // note we do not need an int* to operate on numbers // for ( int i=0; i < numbers.Length; ++i ) total += numbers[i]; return total; } Out is sort of like a built in malloc or new that a function returns thru the parameter list instead of as a return value. Good fpr special allocator type code. public void NextValueList( out int[] num, int size ) { num = new int[size]; } Ref is used when like in C++ we are concerned about operating the SAME structure. In C# you normally don'd have to clean up (i.e. free) new'd elements. This is true for the general case. BUT if you have hairy I/O classes then there is a destructor equiavlent called IDisposable. Basically you derive your C# class from this Interface and it provides the Dispose() method you can customize to clean up. Good Luck Mike Luster CTI/IVR/Telephony SME

                    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