Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

Pointer to string in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 10 Dec 2007 04:44 GMT
WARNING: newbie to C#

Is there a way to do this:

WCHAR szString[] = L"This is your girlfriends string";
LPWSTR pszString = szString; // This is a pointer to her string
wcscpy(pszString, L"And now I touch it!");

in C#?

Something like this:

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)

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
}

I understand there is probably a better way to achieve what I want but
I'm too new to C# to think of it...

Thanks guys/girls!
Mattias Sjögren - 10 Dec 2007 05:37 GMT
>string strText = "This is C# string";
>object objText = strText; // I want this to be a pointer to that string

Yes, at this point you have two variables referring to the same
string.

>objText = "But this doesn't change strText";

The difference in C# is that you can't change a string (like you did
with wcscpy). .NET strings are immutable.

>     TextBox txt = (TextBox)sender;
>     if (txt.Tag.ToString() != txt.Text)
[quoted text clipped - 3 lines]
>     // In the case of a Leave event on txtCompanyAddress,
>     // customerClass.Company.Address should change

Well the eaasy solution would be to write

if (sender == txtCompany)
    customerClass.Company.Name = txt.Text;
else if (sender == txtCompanyAddress)
    customerClass.Company.Address = txt.Text;

But you may also want to look at data binding.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Peter Duniho - 10 Dec 2007 06:26 GMT
> [...]
> 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...
>
[quoted text clipped - 19 lines]
>      // 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
Mike - 10 Dec 2007 08:38 GMT
Very cool! Didn't know about databindings, thanks for the excellent
explanation, got it working perfectly with databindings now.

Mike.

>> [...]
>> string strText = "This is C# string";
[quoted text clipped - 95 lines]
>
> Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.