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

c# pointers ?

Scheduled Pinned Locked Moved C#
csharpquestion
18 Posts 6 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.
  • L Offline
    L Offline
    l a u r e n
    wrote on last edited by
    #1

    i think i know there aren't really pointers in c# as such but... if i have a class so: public class { int id; string name; <---- not much data byte [] data; <---- could be a few mb of data } and i need to populate a combobox with name & id values should i simply use the existing class as a combobox item OR should i define a new class just for this scenario? thnx

    "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

    M D P D A 5 Replies Last reply
    0
    • L l a u r e n

      i think i know there aren't really pointers in c# as such but... if i have a class so: public class { int id; string name; <---- not much data byte [] data; <---- could be a few mb of data } and i need to populate a combobox with name & id values should i simply use the existing class as a combobox item OR should i define a new class just for this scenario? thnx

      "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I don't see anything related to pointers there. Since you have a reference class with all references, there's really no need to create another class unless you get some gain out of it. What are you thinking the issue is here?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      L 1 Reply Last reply
      0
      • L l a u r e n

        i think i know there aren't really pointers in c# as such but... if i have a class so: public class { int id; string name; <---- not much data byte [] data; <---- could be a few mb of data } and i need to populate a combobox with name & id values should i simply use the existing class as a combobox item OR should i define a new class just for this scenario? thnx

        "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

        D Offline
        D Offline
        Dan Mos
        wrote on last edited by
        #3

        Depends on how many objects of your class you'll have/load at a time. If you have many each of them keeping a few mb of data, not so OK. A different approach(that I mostly use) is to create separate methods in the "DataLayer" one that loads only id and name(null or zero sized/empty byte[]). And another one thatn loads the byte[] too, or maybe just the byte[] for a given ID. That way a much lower impact on memory and speed too(given that the byte[] can be easily retrieved by using the ID in the DB if any). In other words load the few mb only when required/needed. My post/answer utterly assumes that there is a DB somewhere. If not, please ignore it.

        All the best, Dan

        L 1 Reply Last reply
        0
        • L l a u r e n

          i think i know there aren't really pointers in c# as such but... if i have a class so: public class { int id; string name; <---- not much data byte [] data; <---- could be a few mb of data } and i need to populate a combobox with name & id values should i simply use the existing class as a combobox item OR should i define a new class just for this scenario? thnx

          "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          l a u r e n wrote:

          should i simply use the existing class

          Sure, but maybe it needs a ToString method?

          L 1 Reply Last reply
          0
          • L l a u r e n

            i think i know there aren't really pointers in c# as such but... if i have a class so: public class { int id; string name; <---- not much data byte [] data; <---- could be a few mb of data } and i need to populate a combobox with name & id values should i simply use the existing class as a combobox item OR should i define a new class just for this scenario? thnx

            "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

            D Offline
            D Offline
            David1987
            wrote on last edited by
            #5

            l a u r e n wrote:

            there aren't really pointers in c# as such

            There are.

            L 1 Reply Last reply
            0
            • L l a u r e n

              i think i know there aren't really pointers in c# as such but... if i have a class so: public class { int id; string name; <---- not much data byte [] data; <---- could be a few mb of data } and i need to populate a combobox with name & id values should i simply use the existing class as a combobox item OR should i define a new class just for this scenario? thnx

              "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              Use the existing class. You can always use pointers in C#. Its called unsafe code.

              Too much of heaven can bring you underground Heaven can always turn around Too much of heaven, our life is all hell bound Heaven, the kill that makes no sound

              1 Reply Last reply
              0
              • D David1987

                l a u r e n wrote:

                there aren't really pointers in c# as such

                There are.

                L Offline
                L Offline
                l a u r e n
                wrote on last edited by
                #7

                yes i know there *are* pointers in c# but they are not the *preferred* way of writing c# afaik if i wanted to write my code as i see best i wouldn't use .net for anything but i am trying to do things right

                "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                1 Reply Last reply
                0
                • M Mark Salsbery

                  I don't see anything related to pointers there. Since you have a reference class with all references, there's really no need to create another class unless you get some gain out of it. What are you thinking the issue is here?

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  L Offline
                  L Offline
                  l a u r e n
                  wrote on last edited by
                  #8

                  well ok so i have a List<> of POCO objects that are serialized into json and back for transport over the wire from a web service ... now i don't send the actual data bytes with the list of items just the id and name (the data bytes can be several mb) and request the actual data bytes on demand ... once they are loaded from the server they are stored in memory in the List<> my main form has a function to get a given document from the List<> ... that causes the doc class to see if the data is there, and if not, go get it and set a flag saying we have it now then it returns the doc.doc_data which is a byte [] variable my actual question is this: when i do "return doc.doc_data" does it make a copy of the data on the stack and pass it back, or does it simply return a ptr / ref to that data? the 1st option would be very bad .... the 2nd is what i hope happens but i don't know how to tell what it is doing hope that explains it better :)

                  "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                  M 1 Reply Last reply
                  0
                  • D Dan Mos

                    Depends on how many objects of your class you'll have/load at a time. If you have many each of them keeping a few mb of data, not so OK. A different approach(that I mostly use) is to create separate methods in the "DataLayer" one that loads only id and name(null or zero sized/empty byte[]). And another one thatn loads the byte[] too, or maybe just the byte[] for a given ID. That way a much lower impact on memory and speed too(given that the byte[] can be easily retrieved by using the ID in the DB if any). In other words load the few mb only when required/needed. My post/answer utterly assumes that there is a DB somewhere. If not, please ignore it.

                    All the best, Dan

                    L Offline
                    L Offline
                    l a u r e n
                    wrote on last edited by
                    #9

                    essentially what i'm doing yes except the database is over the wire and the data is downloaded on demand my actual question is explained better above thnx :)

                    "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      l a u r e n wrote:

                      should i simply use the existing class

                      Sure, but maybe it needs a ToString method?

                      L Offline
                      L Offline
                      l a u r e n
                      wrote on last edited by
                      #10

                      no idea what this means / relates to but thanks for trying to help :)

                      "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                      1 Reply Last reply
                      0
                      • L l a u r e n

                        well ok so i have a List<> of POCO objects that are serialized into json and back for transport over the wire from a web service ... now i don't send the actual data bytes with the list of items just the id and name (the data bytes can be several mb) and request the actual data bytes on demand ... once they are loaded from the server they are stored in memory in the List<> my main form has a function to get a given document from the List<> ... that causes the doc class to see if the data is there, and if not, go get it and set a flag saying we have it now then it returns the doc.doc_data which is a byte [] variable my actual question is this: when i do "return doc.doc_data" does it make a copy of the data on the stack and pass it back, or does it simply return a ptr / ref to that data? the 1st option would be very bad .... the 2nd is what i hope happens but i don't know how to tell what it is doing hope that explains it better :)

                        "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        l a u r e n wrote:

                        my actual question is this: when i do "return doc.doc_data" does it make a copy of the data on the stack and pass it back, or does it simply return a ptr / ref to that data?

                        Depends on the code in the property/method returning the array... is it making a copy and returning a reference to that copy or just returning a reference to the original data? Arrays are reference types. For reference types you need to explicitly make a copy of the data to have two copies. Otherwise a reference refers to the same object, even if you make a copy of the reference. Pointers are so not relevant here... :)

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        L 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          l a u r e n wrote:

                          my actual question is this: when i do "return doc.doc_data" does it make a copy of the data on the stack and pass it back, or does it simply return a ptr / ref to that data?

                          Depends on the code in the property/method returning the array... is it making a copy and returning a reference to that copy or just returning a reference to the original data? Arrays are reference types. For reference types you need to explicitly make a copy of the data to have two copies. Otherwise a reference refers to the same object, even if you make a copy of the reference. Pointers are so not relevant here... :)

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          L Offline
                          L Offline
                          l a u r e n
                          wrote on last edited by
                          #12

                          i know pointers aren't really the thing here i guess i was asking if the function returns a pointer to the data or a copy of the data that's what i do not quite get in c# lets say i have a class:

                          class doc {
                          int doc_id;
                          string doc_title;
                          byte [] doc_data;
                          public doc()
                          {
                          doc_id = 0;
                          doc_title = string.Empty;
                          doc_data = null;
                          }

                          byte [] getdoc()
                          {
                          if (doc_data == null){
                          doc_data = get_the_data_from_somewhere_over_the_network(id);
                          }
                          return doc_data;
                          }
                          }

                          does the getdoc() function return a copy of the data or a ptr to the data? :)

                          "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                          M 1 Reply Last reply
                          0
                          • L l a u r e n

                            i know pointers aren't really the thing here i guess i was asking if the function returns a pointer to the data or a copy of the data that's what i do not quite get in c# lets say i have a class:

                            class doc {
                            int doc_id;
                            string doc_title;
                            byte [] doc_data;
                            public doc()
                            {
                            doc_id = 0;
                            doc_title = string.Empty;
                            doc_data = null;
                            }

                            byte [] getdoc()
                            {
                            if (doc_data == null){
                            doc_data = get_the_data_from_somewhere_over_the_network(id);
                            }
                            return doc_data;
                            }
                            }

                            does the getdoc() function return a copy of the data or a ptr to the data? :)

                            "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            l a u r e n wrote:

                            does the getdoc() function return a copy of the data or a ptr to the data?

                            It returns a reference to doc_data. No copy is made. The reference "refers" to the same exact data the doc_data field "refers" to.

                            l a u r e n wrote:

                            i guess i was asking if the function returns a pointer to the data or a copy of the data
                            that's what i do not quite get in c#

                            In .NET, this is arguably THE fundamental thing that must be understood, especially if you come from a language like C/C++. Except for simple classes that are defined as value types (which are copied when you do an assignment), most classes are reference types, so any time you pass/return objects of those classes you pass/return references to an object - the SAME object. The implication is that if you alter that object, every other place there's a reference held on that same object is effected. This MUST be understood... Types (C# Reference)[^]

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            L D 2 Replies Last reply
                            0
                            • M Mark Salsbery

                              l a u r e n wrote:

                              does the getdoc() function return a copy of the data or a ptr to the data?

                              It returns a reference to doc_data. No copy is made. The reference "refers" to the same exact data the doc_data field "refers" to.

                              l a u r e n wrote:

                              i guess i was asking if the function returns a pointer to the data or a copy of the data
                              that's what i do not quite get in c#

                              In .NET, this is arguably THE fundamental thing that must be understood, especially if you come from a language like C/C++. Except for simple classes that are defined as value types (which are copied when you do an assignment), most classes are reference types, so any time you pass/return objects of those classes you pass/return references to an object - the SAME object. The implication is that if you alter that object, every other place there's a reference held on that same object is effected. This MUST be understood... Types (C# Reference)[^]

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              L Offline
                              L Offline
                              l a u r e n
                              wrote on last edited by
                              #14

                              thank you so much for the time you took to explain that much appreciated :)

                              "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                              M 1 Reply Last reply
                              0
                              • M Mark Salsbery

                                l a u r e n wrote:

                                does the getdoc() function return a copy of the data or a ptr to the data?

                                It returns a reference to doc_data. No copy is made. The reference "refers" to the same exact data the doc_data field "refers" to.

                                l a u r e n wrote:

                                i guess i was asking if the function returns a pointer to the data or a copy of the data
                                that's what i do not quite get in c#

                                In .NET, this is arguably THE fundamental thing that must be understood, especially if you come from a language like C/C++. Except for simple classes that are defined as value types (which are copied when you do an assignment), most classes are reference types, so any time you pass/return objects of those classes you pass/return references to an object - the SAME object. The implication is that if you alter that object, every other place there's a reference held on that same object is effected. This MUST be understood... Types (C# Reference)[^]

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                D Offline
                                D Offline
                                David1987
                                wrote on last edited by
                                #15

                                Mark Salsbery wrote:

                                Except for simple classes that are defined as value types

                                Aka structs?

                                M 1 Reply Last reply
                                0
                                • L l a u r e n

                                  thank you so much for the time you took to explain that much appreciated :)

                                  "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                                  M Offline
                                  M Offline
                                  Mark Salsbery
                                  wrote on last edited by
                                  #16

                                  You're welcome! Better explained at the link... :)

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  1 Reply Last reply
                                  0
                                  • D David1987

                                    Mark Salsbery wrote:

                                    Except for simple classes that are defined as value types

                                    Aka structs?

                                    M Offline
                                    M Offline
                                    Mark Salsbery
                                    wrote on last edited by
                                    #17

                                    Yes! Value Types (C# Reference)[^]

                                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                                    D 1 Reply Last reply
                                    0
                                    • M Mark Salsbery

                                      Yes! Value Types (C# Reference)[^]

                                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                                      D Offline
                                      D Offline
                                      David1987
                                      wrote on last edited by
                                      #18

                                      Ah I forgot enums.. thanks

                                      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