473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Something strange with TextBox.Text property when I use it within a function.

A beginner in C# ..

I wrote 2 classes like below.. (psuedo code)

public class FormUI : System.Windows.Forms.Form
{
public TextBox tbResult = new TextBox();

...

private void OnClickTest(...)
{
tbResult.Text += "A";
tbResult.Text += SomeFunc();
tbResult.Text += "C";
}

private string SomeFunc()
{
tbResult.Text += "B";

return "X";
}
}

I expected the result would be "ABXC";
But the real was
"AXC"

I don't know why the result is like that..
I've never met such a result in C++ brefore... I'm very confusing...

Could someone give me some advise about that symptom??

Aug 24 '06 #1
2 1427
"AXC"
That sounds reasonable, as it would (logically) expand to:
tbResult.Text = tbResult.Text + "A";
tbResult.Text = tbResult.Text + SomeFunc();
tbResult.Text = tbResult.Text + "C";

Assuming it evaluates LTR, the the second line assigns the text "A" + "X"
(with the "A" evaluated first); we lose the "B" en-route.

I think the real message here is to avoid functions with (non-obvious)
consequences, particularly when that consequence directly affects what you
are currently doing (which this patently does).

Marc
Aug 24 '06 #2
korean44 wrote:
tbResult.Text += "A";
First - properties are not like variables, so this gets expanded into:

tbResult.Text = tbResult.Text + "A";
tbResult.Text += SomeFunc();
Again:
tbResult.Text = tbResult.Text + SomeFunc();

You should see here that the addition of 'B' to tbResult.Text inside
SomeFunc will be discarded, because C# evaluates expressions from left
to right.

For example, ECMA 334, 8.5:

"Except for the assignment operators, all binary operators are
left-associative, meaning that operations are performed from left to
right. For example, x + y + z is evaluated as (x + y) + z."
I've never met such a result in C++ brefore... I'm very confusing...
You could have - the equivalent in C++ is undefined behaviour. The order
of evaluation of values in an expression in C++ isn't defined.

For example, see clause 5.17 (expr.ass) paragraph 7 of ISO 14882(1998):

"The behavior of an expression of the form E1 op= E2 is equivalent to E1
= E1 op E2 except that E1 is evaluated only once."

And clause 5 (expr) paragraph 4:

"Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions, and the order in
which side effects take place, is unspecified."

There is no such note for additive expressions, so this means that the
function call could happen before or after the read of E1, so E1 could
have the before or after value.

-- Barry

--
http://barrkel.blogspot.com/
Aug 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control,...
1
by: JJ | last post by:
We have an ASP.NET web form with 1 TextBox, 2 LinkButtons, and 1 RequireFieldValidator. The situation is: the user needs to enter a folder name in the TextBox and click the first LinkButton (the...
2
by: sosh | last post by:
Hi, I'm having a strange problem with the update function of a datagrid: I have a datagrid, the first column lists item names from a database. Second column is an EditCommandColumn, and then I...
3
by: Jim in Arizona | last post by:
I'm going insane! I don't know if it's just that the .net 2.0 framework is buggy or if it really is my code. This is pretty hard to explain since I can't even begin to nail down why this is...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers...
7
by: Tim_Mac | last post by:
hi, using .net 2.0, i have a web form with lots of textboxes, drop-down-lists etc. There are lots of required field validators and regular expression validators. When i click the 'save' button,...
1
by: rn5a | last post by:
I want to create a custom control that encapsulates a Button & a TextBox. When the Button is clicked, the user is asked a question using JavaScript confirm (which shows 2 buttons - 'OK' &...
2
by: zacks | last post by:
I am developing an app in VS2005 (actually in VB.NET but this question, I believe, would apply to any .NET language) that is used to design the contents of an XML file. One of potential items that...
5
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
I'm sorry, but I've read your code a couple of times and just don't see where the Form1 is initialized. Form1 also sounds like a class name, and this would be how you could do some form operations...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.