Re: Pointer to string in C#

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Sun, 09 Dec 2007 20:44:02 -0800, Mike <who@xxxxxxxxx> wrote:

[...]
string strText = "This is C# string";
object objText = strText; // I want this to be a pointer to that string
objText = "But this doesn't change strText";

but then working please! ;o)

Let's get the whole "pointer to string" thing out of the way. There's not really any way to do that in C#, except possibly with "unsafe" code. But given your description so far, I doubt you really want a pointer to a string. Surely we can come up with some way to do what you want without actually needing a reference to a string reference.

So, let's look at your code:

From the description in MSDN strText.Clone() looked promising, but no....

Reason I want to do this is prevent having to duplicate "Leave" eventcode for every textbox on my form:

this.txtCompany.Text = customerClass.Company.Name;
this.txtCompany.Tag = customerClass.Company.Name;
this.txtCompany.Leave += new EventHandler(txtOnLostFocus);

this.txtCompanyAddress.Text = customerClass.Company.Address;
this.txtCompanyAddress.Tag = customerClass.Company.Address;
this.txtCompanyAddress.Leave += new EventHandler(txtOnLostFocus);

private void txtOnLostFocus(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
if (txt.Tag.ToString() != txt.Text)
txt.Tag = txt.Text;
// In the case of a Leave event on txtCompany,
// customerClass.Company.Name should change
// In the case of a Leave event on txtCompanyAddress,
// customerClass.Company.Address should change
}

So, I have the impression you want changes in the Text property to be reflected in the associated property in a given class instance.

I can think of at least three different ways to do that. One involves reflection, which I try to stay away from so I'm going to ignore that. :) The second involves data binding. I'm no expert, so there might be a better way to implement data binding than how I show it here. But you can do something like this:

this.txtCompany.DataBindings.Add("Text", customerClass.Company, "Name");
this.txtCompanyAddress.DataBindings.Add("Text", customerClass.Company, "Address");

By default, the property should be updated on validation, so as long as the control is validating (which it would be by default) the property will get set properly (validation usually happens when focus leaves the control). Using this method, you don't even need to initialize the control, nor provide an event handler for the Leave event.

The third way uses an anonymous method. In this particular scenario, I think it's the clumsy way to do it. But seeing the technique you may find it useful for other things in the future (just keep in mind that as was the case here, there might be an easier way :) ):

// This creates an anonymous method that will copy the control's Text property to
// class's corresponding property.
this.txtCompany.Tag = delegate() { customerClass.Company.Name = this.txtCompany.Text; }

// Alternatively, if you really want to check the previous value first:
this.txtCompany.Tag = delegate() { if (customerClass.Company.Name != this.txtCompany.Text) customerClass.Company.Name = this.txtCompany.Text; }

then in the Leave handler:

private void txtOnLostFocus(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
MethodInvoker setter = (MethodInvoker)txt.Tag;

// This line of code executes the anonymous method you created before. Since each
// control would have a different anonymous method, assigning the control's Text
// property to the appropriate data property, executing it here accomplishes what
// you want.
setter();
}

This sort of thing can work pretty much anywhere there's some specific code you want to execute every time for a specific situation, but you don't want to have to create a whole new named method each time. It's especially common when using Control.Invoke() or predicates for the various generic collection search, sorting, etc. methods. Using it here is kind of contrived. :)

Pete
.



Relevant Pages

  • Re: Pointer to string in C#
    ... Didn't know about databindings, thanks for the excellent explanation, got it working perfectly with databindings now. ... By default, the property should be updated on validation, so as long as the control is validating the property will get set properly. ... The third way uses an anonymous method. ... // This line of code executes the anonymous method you created before. ...
    (microsoft.public.dotnet.framework)
  • Re: Need help with multi threading, got error "Cross-thread operation.
    ... Cross-thread operation not valid: Control 'cboFromEvent accessed from a ... method - and certainly not in an anonymous method. ...
    (microsoft.public.dotnet.languages.csharp)
  • Prevent a record being saved after an error occurs
    ... "cmdSaveRecord" sub which, amongst other things, executes this command: ... Within here, some validation is ... If the control fails ... Cancel = True ...
    (microsoft.public.access.formscoding)
  • Re: C# Command to Wait a Specified Period of Time
    ... How would defining a method elsewhere and using that to instantiate a delegate have helped that situation? ... but a simple iterative method that executes when it is called would. ... It's a completely orthogonal issue to the question of using an anonymous method. ... This is a question of temporal proximity, and it has nothing to do with anonymous methods whatsoever. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Command to Wait a Specified Period of Time
    ... Wherever you can use an anonymous method, you can always declare a named method elsewhere that does the same thing. ... The question of spacial and temporal proximity is precisely what made your original code "less readable". ... You have a code block sitting in-line with the current execution path, ... And in fact, it executes much, much later - on a timer tick. ...
    (microsoft.public.dotnet.languages.csharp)