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. use pointers in c#

use pointers in c#

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

    I need a collection to point to individual items in another collection. How would I write the ionline c++ code inside a c# function Or can I write a c++ managed code function inside a dll and will it work with my c# application I'm not an expert yet, but I play one at work. Yeah and here too.

    L B 2 Replies Last reply
    0
    • I Ista

      I need a collection to point to individual items in another collection. How would I write the ionline c++ code inside a c# function Or can I write a c++ managed code function inside a dll and will it work with my c# application I'm not an expert yet, but I play one at work. Yeah and here too.

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

      Ista wrote: I need a collection to point to individual items in another collection. How would I write the ionline c++ code inside a c# function Or can I write a c++ managed code function inside a dll and will it work with my c# application Please redefine the question!!! Else the answer will be 42. :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      I 1 Reply Last reply
      0
      • L leppie

        Ista wrote: I need a collection to point to individual items in another collection. How would I write the ionline c++ code inside a c# function Or can I write a c++ managed code function inside a dll and will it work with my c# application Please redefine the question!!! Else the answer will be 42. :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        I Offline
        I Offline
        Ista
        wrote on last edited by
        #3

        I have a collection A which have the sam items in each collection order detail class contains a detail description. In each detail description there is a collection ther we call collection A. Well in order for the data grid to work I need to create a collection B inside order details class. In each item of collection b I want to simple point to the correct item of collection A. nick I'm not an expert yet, but I play one at work. Yeah and here too.

        L 1 Reply Last reply
        0
        • I Ista

          I have a collection A which have the sam items in each collection order detail class contains a detail description. In each detail description there is a collection ther we call collection A. Well in order for the data grid to work I need to create a collection B inside order details class. In each item of collection b I want to simple point to the correct item of collection A. nick I'm not an expert yet, but I play one at work. Yeah and here too.

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

          But why do you want to mess with C++ and pointers. Just reference the object. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          I 1 Reply Last reply
          0
          • L leppie

            But why do you want to mess with C++ and pointers. Just reference the object. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

            I Offline
            I Offline
            Ista
            wrote on last edited by
            #5

            well if someone could help me with example on how i reference a collection item to another collection item I wouldnt resort to c++ pointers, but pointers are easy anyhow. I'm not an expert yet, but I play one at work. Yeah and here too.

            J N 2 Replies Last reply
            0
            • I Ista

              well if someone could help me with example on how i reference a collection item to another collection item I wouldnt resort to c++ pointers, but pointers are easy anyhow. I'm not an expert yet, but I play one at work. Yeah and here too.

              J Offline
              J Offline
              J Dunlap
              wrote on last edited by
              #6

              An object reference is a pointer to an object anyway, so: If you want the entire collection B to be the same as collection A:

              CollectionB=CollectionA;

              If you want to only reference certain items from Collection A in Collection B, just add references to those objects to Collection B:

              CollectionB.Add(CollectionA[index];

              If you want to change items in Collection A, so that you're actually changing what's in Collection A, not just the objects themselves, the best thing to do is have Collection B store the indexes of the elements in Collection A, so that you can modify Collection A's contents based on the indexes stored in collection A.

              "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
              "You must be the change you wish to see in the world." - Mahatma Gandhi

              I 2 Replies Last reply
              0
              • J J Dunlap

                An object reference is a pointer to an object anyway, so: If you want the entire collection B to be the same as collection A:

                CollectionB=CollectionA;

                If you want to only reference certain items from Collection A in Collection B, just add references to those objects to Collection B:

                CollectionB.Add(CollectionA[index];

                If you want to change items in Collection A, so that you're actually changing what's in Collection A, not just the objects themselves, the best thing to do is have Collection B store the indexes of the elements in Collection A, so that you can modify Collection A's contents based on the indexes stored in collection A.

                "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                "You must be the change you wish to see in the world." - Mahatma Gandhi

                I Offline
                I Offline
                Ista
                wrote on last edited by
                #7

                actually the second answer doesnt work because you cant reference an indexer. already tried that one I'm not an expert yet, but I play one at work. Yeah and here too.

                J 1 Reply Last reply
                0
                • J J Dunlap

                  An object reference is a pointer to an object anyway, so: If you want the entire collection B to be the same as collection A:

                  CollectionB=CollectionA;

                  If you want to only reference certain items from Collection A in Collection B, just add references to those objects to Collection B:

                  CollectionB.Add(CollectionA[index];

                  If you want to change items in Collection A, so that you're actually changing what's in Collection A, not just the objects themselves, the best thing to do is have Collection B store the indexes of the elements in Collection A, so that you can modify Collection A's contents based on the indexes stored in collection A.

                  "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                  "You must be the change you wish to see in the world." - Mahatma Gandhi

                  I Offline
                  I Offline
                  Ista
                  wrote on last edited by
                  #8

                  jdunlap wrote: If you want to change items in Collection A, so that you're actually changing what's in Collection A, not just the objects themselves, the best thing to do is have Collection B store the indexes of the elements in Collection A, so that you can modify Collection A's contents based on the indexes stored in collection A. I mean yeah I could override the indexers to pass through but I don't like that pattern. I would much rather access an array of pointers instead. I mean that is simplistic c++ code rather than having object forwarding with extra type casting and the whole works. I mean if I have to take the time to write a collection that then accesses another collection based on its index or an object type. Then theres much more debugging involved and if I do indexof then it compares objects and that defeats the whole purpose of it all. That and it involves more coding. Using a collection or array of pointers doesnt create a mess of code and I can simply check the value to see if its actually pointing to a null address which is one line of code to debug. That and if the object is changed to be a new one the address wont repeat itself while an index could. My thoughts and opinions about this whole thing. So how do you declare a c++ code block in c#. I know its possible but dont know how to set it up? nick I'm not an expert yet, but I play one at work. Yeah and here too.

                  N J 2 Replies Last reply
                  0
                  • I Ista

                    actually the second answer doesnt work because you cant reference an indexer. already tried that one I'm not an expert yet, but I play one at work. Yeah and here too.

                    J Offline
                    J Offline
                    J Dunlap
                    wrote on last edited by
                    #9

                    I wasn't talking about referencing an indexer - I was talking about a collection of indexes corresponding to the indexes of the items in collection A.

                    "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                    "You must be the change you wish to see in the world." - Mahatma Gandhi

                    1 Reply Last reply
                    0
                    • I Ista

                      well if someone could help me with example on how i reference a collection item to another collection item I wouldnt resort to c++ pointers, but pointers are easy anyhow. I'm not an expert yet, but I play one at work. Yeah and here too.

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #10

                      Ista wrote: I'm not an expert yet, but I play one at work. Yeah and here too. :rolleyes:


                      Extending MFC Applications with the .NET Framework [NW] (My book with Tom) Summer Love and Some more Cricket [NW] (My first novel) Shog's review of SLASMC [NW] Come with me if you want to live

                      I 1 Reply Last reply
                      0
                      • I Ista

                        jdunlap wrote: If you want to change items in Collection A, so that you're actually changing what's in Collection A, not just the objects themselves, the best thing to do is have Collection B store the indexes of the elements in Collection A, so that you can modify Collection A's contents based on the indexes stored in collection A. I mean yeah I could override the indexers to pass through but I don't like that pattern. I would much rather access an array of pointers instead. I mean that is simplistic c++ code rather than having object forwarding with extra type casting and the whole works. I mean if I have to take the time to write a collection that then accesses another collection based on its index or an object type. Then theres much more debugging involved and if I do indexof then it compares objects and that defeats the whole purpose of it all. That and it involves more coding. Using a collection or array of pointers doesnt create a mess of code and I can simply check the value to see if its actually pointing to a null address which is one line of code to debug. That and if the object is changed to be a new one the address wont repeat itself while an index could. My thoughts and opinions about this whole thing. So how do you declare a c++ code block in c#. I know its possible but dont know how to set it up? nick I'm not an expert yet, but I play one at work. Yeah and here too.

                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #11

                        Ista wrote: So how do you declare a c++ code block in c#. I know its possible but dont know how to set it up? If you wanna do pointers in C# see if my article helps :- Getting unsafe with pointers in C# Nish


                        Extending MFC Applications with the .NET Framework [NW] (My book with Tom) Summer Love and Some more Cricket [NW] (My first novel) Shog's review of SLASMC [NW] Come with me if you want to live

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          Ista wrote: I'm not an expert yet, but I play one at work. Yeah and here too. :rolleyes:


                          Extending MFC Applications with the .NET Framework [NW] (My book with Tom) Summer Love and Some more Cricket [NW] (My first novel) Shog's review of SLASMC [NW] Come with me if you want to live

                          I Offline
                          I Offline
                          Ista
                          wrote on last edited by
                          #12

                          its a bad link I'm not an expert yet, but I play one at work. Yeah and here too.

                          N 1 Reply Last reply
                          0
                          • I Ista

                            jdunlap wrote: If you want to change items in Collection A, so that you're actually changing what's in Collection A, not just the objects themselves, the best thing to do is have Collection B store the indexes of the elements in Collection A, so that you can modify Collection A's contents based on the indexes stored in collection A. I mean yeah I could override the indexers to pass through but I don't like that pattern. I would much rather access an array of pointers instead. I mean that is simplistic c++ code rather than having object forwarding with extra type casting and the whole works. I mean if I have to take the time to write a collection that then accesses another collection based on its index or an object type. Then theres much more debugging involved and if I do indexof then it compares objects and that defeats the whole purpose of it all. That and it involves more coding. Using a collection or array of pointers doesnt create a mess of code and I can simply check the value to see if its actually pointing to a null address which is one line of code to debug. That and if the object is changed to be a new one the address wont repeat itself while an index could. My thoughts and opinions about this whole thing. So how do you declare a c++ code block in c#. I know its possible but dont know how to set it up? nick I'm not an expert yet, but I play one at work. Yeah and here too.

                            J Offline
                            J Offline
                            J Dunlap
                            wrote on last edited by
                            #13

                            Well, if you really want to use pointers, you can do so in exactly the same way you would do it in C++, only when you declare a variable, you will want to add the fixed keyword to the variable declaration, to keep GC from moving the memory while you're still using it. A method that uses pointers must have the unsafe keyword, or else the code must be in an unsafe block:

                            public unsafe void Foo()
                            {
                            //'unsafe' code here
                            }

                            ...OR...

                            unsafe
                            {
                            //'unsafe' code here
                            }

                            You also must check the "allow unsafe code" option in project options, or compile with the /unsafe compiler option.

                            "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                            "You must be the change you wish to see in the world." - Mahatma Gandhi

                            I 1 Reply Last reply
                            0
                            • J J Dunlap

                              Well, if you really want to use pointers, you can do so in exactly the same way you would do it in C++, only when you declare a variable, you will want to add the fixed keyword to the variable declaration, to keep GC from moving the memory while you're still using it. A method that uses pointers must have the unsafe keyword, or else the code must be in an unsafe block:

                              public unsafe void Foo()
                              {
                              //'unsafe' code here
                              }

                              ...OR...

                              unsafe
                              {
                              //'unsafe' code here
                              }

                              You also must check the "allow unsafe code" option in project options, or compile with the /unsafe compiler option.

                              "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                              "You must be the change you wish to see in the world." - Mahatma Gandhi

                              I Offline
                              I Offline
                              Ista
                              wrote on last edited by
                              #14

                              thank you sir I'm not an expert yet, but I play one at work. Yeah and here too.

                              1 Reply Last reply
                              0
                              • I Ista

                                its a bad link I'm not an expert yet, but I play one at work. Yeah and here too.

                                N Offline
                                N Offline
                                Nish Nishant
                                wrote on last edited by
                                #15

                                Ista wrote: its a bad link Sorry, but I've fixed the link :-) Nish


                                Extending MFC Applications with the .NET Framework [NW] (My book with Tom) Summer Love and Some more Cricket [NW] (My first novel) Shog's review of SLASMC [NW] Come with me if you want to live

                                1 Reply Last reply
                                0
                                • I Ista

                                  I need a collection to point to individual items in another collection. How would I write the ionline c++ code inside a c# function Or can I write a c++ managed code function inside a dll and will it work with my c# application I'm not an expert yet, but I play one at work. Yeah and here too.

                                  B Offline
                                  B Offline
                                  Burt Harris
                                  wrote on last edited by
                                  #16

                                  Hello Ista, In C#, there are two kinds of types, value types and reference types. A very limited set of information fits into the value type category, for example int, and double. Most types, including string and all objects are reference types. Variables declared as reference types don't actually hold the underlying data, but instead have a reference to it, this is very much like pointer, but you don't have to use *. Several variables can hold references to the same object, copying from one variable (or collection) to another doesn't duplicate the underlying data, it just copies a pointer. In short, I don't think you need pointers, the standard collections should work the way you want. Burt Harris

                                  I 1 Reply Last reply
                                  0
                                  • B Burt Harris

                                    Hello Ista, In C#, there are two kinds of types, value types and reference types. A very limited set of information fits into the value type category, for example int, and double. Most types, including string and all objects are reference types. Variables declared as reference types don't actually hold the underlying data, but instead have a reference to it, this is very much like pointer, but you don't have to use *. Several variables can hold references to the same object, copying from one variable (or collection) to another doesn't duplicate the underlying data, it just copies a pointer. In short, I don't think you need pointers, the standard collections should work the way you want. Burt Harris

                                    I Offline
                                    I Offline
                                    Ista
                                    wrote on last edited by
                                    #17

                                    so are you saying if I type this CollectionA.Add(CollectionB[index]); then collection would hold a reference to B. not a copy but a reference thats where I'm confused with this c# thing because to pass by value MyMethod object obj) pass the value or a copy so to pass by reference = MyMethod( ref object obj ) and thats where I got foobar'd in that you cannot pass by reference an indexer MyMethod( ref CollectionA[0] ); so thats what I'm asking. The scenario is this I have to have a main collection for the datagrid, it cant concatenate multiple collections. Yes they are all the same type but belong to sevreral different parent objects. Thats my whole dilemna. I have written a workaround but I dont like nor want to keep it because the code will get very intricate over the next week and will cause debugging issues. nick I'm not an expert yet, but I play one at work. Yeah and here too.

                                    J 1 Reply Last reply
                                    0
                                    • I Ista

                                      so are you saying if I type this CollectionA.Add(CollectionB[index]); then collection would hold a reference to B. not a copy but a reference thats where I'm confused with this c# thing because to pass by value MyMethod object obj) pass the value or a copy so to pass by reference = MyMethod( ref object obj ) and thats where I got foobar'd in that you cannot pass by reference an indexer MyMethod( ref CollectionA[0] ); so thats what I'm asking. The scenario is this I have to have a main collection for the datagrid, it cant concatenate multiple collections. Yes they are all the same type but belong to sevreral different parent objects. Thats my whole dilemna. I have written a workaround but I dont like nor want to keep it because the code will get very intricate over the next week and will cause debugging issues. nick I'm not an expert yet, but I play one at work. Yeah and here too.

                                      J Offline
                                      J Offline
                                      J Dunlap
                                      wrote on last edited by
                                      #18

                                      Ista wrote: so are you saying if I type this CollectionA.Add(CollectionB[index]); then collection would hold a reference to B. not a copy but a reference Correct. Ista wrote: thats where I'm confused with this c# thing because to pass by value MyMethod object obj) pass the value or a copy so to pass by reference = MyMethod( ref object obj ) and thats where I got foobar'd in that you cannot pass by reference an indexer MyMethod( ref CollectionA[0] ); so thats what I'm asking. When you pass a reference type, it is always passed as a reference, because a variable of a reference type always holds a reference to the value, not the actual value. When you pass a reference type by reference, something interesting happens - a reference to the variable that holds the reference is passed. If you change that variable, the original variable will also hold the new reference.

                                      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                                      "You must be the change you wish to see in the world." - Mahatma Gandhi

                                      I 1 Reply Last reply
                                      0
                                      • J J Dunlap

                                        Ista wrote: so are you saying if I type this CollectionA.Add(CollectionB[index]); then collection would hold a reference to B. not a copy but a reference Correct. Ista wrote: thats where I'm confused with this c# thing because to pass by value MyMethod object obj) pass the value or a copy so to pass by reference = MyMethod( ref object obj ) and thats where I got foobar'd in that you cannot pass by reference an indexer MyMethod( ref CollectionA[0] ); so thats what I'm asking. When you pass a reference type, it is always passed as a reference, because a variable of a reference type always holds a reference to the value, not the actual value. When you pass a reference type by reference, something interesting happens - a reference to the variable that holds the reference is passed. If you change that variable, the original variable will also hold the new reference.

                                        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                                        "You must be the change you wish to see in the world." - Mahatma Gandhi

                                        I Offline
                                        I Offline
                                        Ista
                                        wrote on last edited by
                                        #19

                                        ok but how do i pass it since CollectionA[0] isnt a valid statement I'm not an expert yet, but I play one at work. Yeah and here too.

                                        J 1 Reply Last reply
                                        0
                                        • I Ista

                                          ok but how do i pass it since CollectionA[0] isnt a valid statement I'm not an expert yet, but I play one at work. Yeah and here too.

                                          J Offline
                                          J Offline
                                          J Dunlap
                                          wrote on last edited by
                                          #20

                                          CollectionB[0]=CollectionA[0];

                                          or

                                          CollectionB.Add(CollectionA[0];

                                          "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                                          "You must be the change you wish to see in the world." - Mahatma Gandhi

                                          I 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