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. How to create a "reference" to an object?

How to create a "reference" to an object?

Scheduled Pinned Locked Moved C#
questioncsharpc++tutorial
3 Posts 2 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.
  • K Offline
    K Offline
    Kuang Cao
    wrote on last edited by
    #1

    Hi, Suppose I have a function which takes in 4 objects as parameters like: methodA(classA obj1, classA obj2, classA obj3, classA obj4) and inside the method, a check will be performed and depending on the result, either the first three objects will be used, or the last three. The way I did this in C++ is basically by defining three pointers, and have those pointers point to either the first three objects or the last three depending on the check. And later those three pointers are used to access the data. But how can I achieve this in C#? Thanks a lot.

    G 1 Reply Last reply
    0
    • K Kuang Cao

      Hi, Suppose I have a function which takes in 4 objects as parameters like: methodA(classA obj1, classA obj2, classA obj3, classA obj4) and inside the method, a check will be performed and depending on the result, either the first three objects will be used, or the last three. The way I did this in C++ is basically by defining three pointers, and have those pointers point to either the first three objects or the last three depending on the check. And later those three pointers are used to access the data. But how can I achieve this in C#? Thanks a lot.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      When you are using objects, you are actually using references to the objects. The four method parameters are references. To get another three references, you just have to declare them in the method:

      // Declare the references
      classA use1, use2, use3;

      // Then set the references
      if (some condition) {
      use1 = obj1;
      use2 = obj2;
      use3 = obj3;
      } else {
      use1 = obj2;
      use2 = obj3;
      use3 = obj4;
      }

      K 1 Reply Last reply
      0
      • G Guffa

        When you are using objects, you are actually using references to the objects. The four method parameters are references. To get another three references, you just have to declare them in the method:

        // Declare the references
        classA use1, use2, use3;

        // Then set the references
        if (some condition) {
        use1 = obj1;
        use2 = obj2;
        use3 = obj3;
        } else {
        use1 = obj2;
        use2 = obj3;
        use3 = obj4;
        }

        K Offline
        K Offline
        Kuang Cao
        wrote on last edited by
        #3

        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