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. Referencing a reference

Referencing a reference

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
11 Posts 3 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.
  • M Offline
    M Offline
    Martin Cross
    wrote on last edited by
    #1

    Yello, I'm suffering from user stupidity at the moment and am hoping someone here can show me the way. I'm attempting to build a class that contains a collection. I have a method in the class called Bind(), which takes a textbox reference and an object reference as it's parameters. These two passed references are then stored into the collection. Simple enough I thought, but the problem appears to be, that I cannot make a reference to a reference. The objects contained within the collection no longer reference the original objects. As as an example, I should be able to pull out one of the textboxes from the collection, change the .text property and see the textbox change on the form. Alas, this is not the case. I have tried just assigning the references to two member variables within the class instead of a collection. Same problem, once the second assigment occurs, I get a copy of the textbox and object and not another reference. Is there something blindingly simple that I'm missing here? Surely in .NET you can reference a reference? Or is there some default behaviour that I don't understand that forces an object to copy itself in this situation. Thanks. Martin

    F H 2 Replies Last reply
    0
    • M Martin Cross

      Yello, I'm suffering from user stupidity at the moment and am hoping someone here can show me the way. I'm attempting to build a class that contains a collection. I have a method in the class called Bind(), which takes a textbox reference and an object reference as it's parameters. These two passed references are then stored into the collection. Simple enough I thought, but the problem appears to be, that I cannot make a reference to a reference. The objects contained within the collection no longer reference the original objects. As as an example, I should be able to pull out one of the textboxes from the collection, change the .text property and see the textbox change on the form. Alas, this is not the case. I have tried just assigning the references to two member variables within the class instead of a collection. Same problem, once the second assigment occurs, I get a copy of the textbox and object and not another reference. Is there something blindingly simple that I'm missing here? Surely in .NET you can reference a reference? Or is there some default behaviour that I don't understand that forces an object to copy itself in this situation. Thanks. Martin

      F Offline
      F Offline
      Furty
      wrote on last edited by
      #2

      It's hard to help with a code sample, but here's some code from a quick form I threw together which I hope shows how to do what you want. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ReferenceAReference { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private MyCustomObjectCollection myCustomObjectCollection; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); this.myCustomObjectCollection = new MyCustomObjectCollection(); // fill the form with some textboxes for(int x=0; x < 3; x++) { for(int y=0; y < 7; y++) { TextBox tb = new TextBox(); tb.Location = new Point(8 + (x*108), 8 + (y*24)); this.Controls.Add(tb); this.myCustomObjectCollection.Add(new MyCustomObject(tb, null)); } } } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(112, 240); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 23); this.button1.TabIndex = 0; this.button1.Text = "Fill TextBoxes"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 240); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = "Test String"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 266); this.Controls.AddRange(new

      M 1 Reply Last reply
      0
      • F Furty

        It's hard to help with a code sample, but here's some code from a quick form I threw together which I hope shows how to do what you want. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ReferenceAReference { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private MyCustomObjectCollection myCustomObjectCollection; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); this.myCustomObjectCollection = new MyCustomObjectCollection(); // fill the form with some textboxes for(int x=0; x < 3; x++) { for(int y=0; y < 7; y++) { TextBox tb = new TextBox(); tb.Location = new Point(8 + (x*108), 8 + (y*24)); this.Controls.Add(tb); this.myCustomObjectCollection.Add(new MyCustomObject(tb, null)); } } } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(112, 240); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 23); this.button1.TabIndex = 0; this.button1.Text = "Fill TextBoxes"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 240); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = "Test String"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 266); this.Controls.AddRange(new

        M Offline
        M Offline
        Martin Cross
        wrote on last edited by
        #3

        Thanks for that. I've compared it against my own code, and yes, they're extremely similar. Have you tried to run yours yet and see if it's making copies instead of references? Because it doesn't allow me to reference a reference of a textbox and let modifications to the textbox ripple through to the original control!! It's all very bizarre, and I'm throwing the toys out of my pram right now.

        F 1 Reply Last reply
        0
        • M Martin Cross

          Yello, I'm suffering from user stupidity at the moment and am hoping someone here can show me the way. I'm attempting to build a class that contains a collection. I have a method in the class called Bind(), which takes a textbox reference and an object reference as it's parameters. These two passed references are then stored into the collection. Simple enough I thought, but the problem appears to be, that I cannot make a reference to a reference. The objects contained within the collection no longer reference the original objects. As as an example, I should be able to pull out one of the textboxes from the collection, change the .text property and see the textbox change on the form. Alas, this is not the case. I have tried just assigning the references to two member variables within the class instead of a collection. Same problem, once the second assigment occurs, I get a copy of the textbox and object and not another reference. Is there something blindingly simple that I'm missing here? Surely in .NET you can reference a reference? Or is there some default behaviour that I don't understand that forces an object to copy itself in this situation. Thanks. Martin

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You say you're updating text in a TextBox? Is this happening in a separate thread? In any case, you should invoke Control.Refresh() or Control.Update() on the control to get the value to be repainted, expecially if changing the text in a separate thread (since UI changes are supposed to be done in the main UI thread and not doing so results in "onforeseen" errors. Have you actually stepped through this in the debugger and made sure that the changes are correct, assuring that the references are equal? You could also use Object.Equals() on the two references to see if the references are equal (instead of if the objects are equal).

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          M 1 Reply Last reply
          0
          • H Heath Stewart

            You say you're updating text in a TextBox? Is this happening in a separate thread? In any case, you should invoke Control.Refresh() or Control.Update() on the control to get the value to be repainted, expecially if changing the text in a separate thread (since UI changes are supposed to be done in the main UI thread and not doing so results in "onforeseen" errors. Have you actually stepped through this in the debugger and made sure that the changes are correct, assuring that the references are equal? You could also use Object.Equals() on the two references to see if the references are equal (instead of if the objects are equal).

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            M Offline
            M Offline
            Martin Cross
            wrote on last edited by
            #5

            It's all happening in the same thread, yes the debugger shows copies are made when added into the collection, 'no' I don't know what's going on, 'yes' I'm throwing my toys out of the pram. All I want to do, is bind textbox's to member variables in a class. And I just want the member variables updated each time the controls .text property is changed - preferably via an event. It's sounds simple enough - but I can't manage it.

            H 1 Reply Last reply
            0
            • M Martin Cross

              It's all happening in the same thread, yes the debugger shows copies are made when added into the collection, 'no' I don't know what's going on, 'yes' I'm throwing my toys out of the pram. All I want to do, is bind textbox's to member variables in a class. And I just want the member variables updated each time the controls .text property is changed - preferably via an event. It's sounds simple enough - but I can't manage it.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              What collection class are you using? I do this quite a bit in our application using provided classes and custom classes and have never had problems. So long as I add the TextBox object's reference to the collection itself, it's no big deal. Are you by change passing a reference to the string in TextBox.Text? Also, what you're describing is property data-binding. You can bind properties of objects to properties of other objects, or even to collections of objects (happens a lot in ASP.NET). You could use textBox1.DataBindings.Add("Text", this, "Text"). If the Text property of your control is supposed to modify the TextBox's Text property, why not just do this?

              public class MyControl : UserControl
              private TextBox textBox1;
              // ...
              public string Text
              {
              get { return this.textBox1.Text; }
              set { this.textBox1.Text = value; }
              }
              }

              ...although maybe I'm not understanding you right.

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              1 Reply Last reply
              0
              • M Martin Cross

                Thanks for that. I've compared it against my own code, and yes, they're extremely similar. Have you tried to run yours yet and see if it's making copies instead of references? Because it doesn't allow me to reference a reference of a textbox and let modifications to the textbox ripple through to the original control!! It's all very bizarre, and I'm throwing the toys out of my pram right now.

                F Offline
                F Offline
                Furty
                wrote on last edited by
                #7

                The code I pasted is a demos the concept, and yes it works here - try copying & pasting it to a new form and give it a try.

                M 1 Reply Last reply
                0
                • F Furty

                  The code I pasted is a demos the concept, and yes it works here - try copying & pasting it to a new form and give it a try.

                  M Offline
                  M Offline
                  Martin Cross
                  wrote on last edited by
                  #8

                  It has been copied to a new form. Sorry, it's not working here.

                  F 1 Reply Last reply
                  0
                  • M Martin Cross

                    It has been copied to a new form. Sorry, it's not working here.

                    F Offline
                    F Offline
                    Furty
                    wrote on last edited by
                    #9

                    If that's the case, I can understand why you're throwing your toys out of the pram right now. Without exagerration, I've coded this exact style of reference literally dozens if not hundreds of times, but notwithstanding this I compiled it and ran it by our beta test team. It was ran on 27 different beta testing systems, and all of them ran it without fault. The only conclusion I can come to at this stage is that something is frazzled on your system, or if you put this in your existing project, something is frazzled in it. If you can, try it again on a different system, preferably with a fresh OS & .Net framework install. If it fails get back to me because the fault your seeing could effect literally thousands of our clients and we'd certainly need to get to the bottom of it.

                    M 1 Reply Last reply
                    0
                    • F Furty

                      If that's the case, I can understand why you're throwing your toys out of the pram right now. Without exagerration, I've coded this exact style of reference literally dozens if not hundreds of times, but notwithstanding this I compiled it and ran it by our beta test team. It was ran on 27 different beta testing systems, and all of them ran it without fault. The only conclusion I can come to at this stage is that something is frazzled on your system, or if you put this in your existing project, something is frazzled in it. If you can, try it again on a different system, preferably with a fresh OS & .Net framework install. If it fails get back to me because the fault your seeing could effect literally thousands of our clients and we'd certainly need to get to the bottom of it.

                      M Offline
                      M Offline
                      Martin Cross
                      wrote on last edited by
                      #10

                      Mmmmmmmmmm, this is interesting. My two development machines are .NET framework 1.0. Your code, and my code do not work on it. Move the same two lots of code onto a machine with .NET 1.1 on it, and hey presto your code works. Mine sadly does not - this however I will put down to duff coding. But I find it had to believe that there's significant enough difference between the two versions for something as fundamental as this to act differently. My range of machines to test on is limited to these three for the moment. But it's definitely food for thought. Would you have been running on 1.1 by any chance?

                      F 1 Reply Last reply
                      0
                      • M Martin Cross

                        Mmmmmmmmmm, this is interesting. My two development machines are .NET framework 1.0. Your code, and my code do not work on it. Move the same two lots of code onto a machine with .NET 1.1 on it, and hey presto your code works. Mine sadly does not - this however I will put down to duff coding. But I find it had to believe that there's significant enough difference between the two versions for something as fundamental as this to act differently. My range of machines to test on is limited to these three for the moment. But it's definitely food for thought. Would you have been running on 1.1 by any chance?

                        F Offline
                        F Offline
                        Furty
                        wrote on last edited by
                        #11

                        I originally compiled it on framework 1.0, and the subsequent tests were run on 1.0, 1.0 SP1, 1.0 SP2 and 1.1, with varying OS's. I have just now compiled it on framework 1.1, and it works fine on my dev machines (I didn't send it out for beta testing again). Just to be clear, what should be happening is that any text located in textBox1 should be copied to each TexBox in the collection when button1 is clicked. Like I noted previously, I have coded far deeper references than this that are in production systems, without problems. Maybe there's a problem with the way your system/s are compiling the code? I know that's a bizarre scenario, but this is a bizarre behavior! I you like, I can send you a compliled version for testing, just send me an email address to use.

                        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