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. When changing a variable inside an object the similar variables in other objects do change to the same value

When changing a variable inside an object the similar variables in other objects do change to the same value

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 4 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.
  • A Offline
    A Offline
    alnatsheh
    wrote on last edited by
    #1

    hello, I have an usual problem ( a least for me :) ), where I created multiple objects (say b1, b2, b3 ,b4 ) from the same class (b) inside another class (class a), the object is a1. The variable for class b such as "value" is changed for every object, for example a1.b1.value = 10; also a1.b2.value = 20; .. .. .. now when the value of 20 is entered in b2 the value in b1 is also changed to 20. Did anyone see this problem earlier? thanks

    L S 2 Replies Last reply
    0
    • A alnatsheh

      hello, I have an usual problem ( a least for me :) ), where I created multiple objects (say b1, b2, b3 ,b4 ) from the same class (b) inside another class (class a), the object is a1. The variable for class b such as "value" is changed for every object, for example a1.b1.value = 10; also a1.b2.value = 20; .. .. .. now when the value of 20 is entered in b2 the value in b1 is also changed to 20. Did anyone see this problem earlier? thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, show actual code (in PRE tags!) and someone will explain to you what happened. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      1 Reply Last reply
      0
      • A alnatsheh

        hello, I have an usual problem ( a least for me :) ), where I created multiple objects (say b1, b2, b3 ,b4 ) from the same class (b) inside another class (class a), the object is a1. The variable for class b such as "value" is changed for every object, for example a1.b1.value = 10; also a1.b2.value = 20; .. .. .. now when the value of 20 is entered in b2 the value in b1 is also changed to 20. Did anyone see this problem earlier? thanks

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        At a guess, it sounds like when you create your objects you are ending up with them all pointing to the same reference but without code it's hard to explain what is happening.

        Scott Dorman

        Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


        Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

        S 1 Reply Last reply
        0
        • S Scott Dorman

          At a guess, it sounds like when you create your objects you are ending up with them all pointing to the same reference but without code it's hard to explain what is happening.

          Scott Dorman

          Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


          Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

          S Offline
          S Offline
          suresh_kumar_s
          wrote on last edited by
          #4

          sounds like u did a code by using reference type.. so that it copied 20 to another object also..But we need to examine the code

          sureshkumar.S

          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