Referencing a reference
-
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
-
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
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
-
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
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.
-
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
You say you're updating text in a TextBox? Is this happening in a separate thread? In any case, you should invoke
Control.Refresh()
orControl.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 useObject.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-----
-
You say you're updating text in a TextBox? Is this happening in a separate thread? In any case, you should invoke
Control.Refresh()
orControl.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 useObject.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-----
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.
-
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.
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 inTextBox.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 usetextBox1.DataBindings.Add("Text", this, "Text")
. If theText
property of your control is supposed to modify theTextBox
'sText
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-----
-
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.
-
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.
It has been copied to a new form. Sorry, it's not working here.
-
It has been copied to a new form. Sorry, it's not working here.
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.
-
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.
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?
-
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?
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.