Try the below simple program.
It give expected result.
1
1
2
2
Then toggle the statements in private static int sinc(), to use
Test._num ++ .
The result:
0
0
1
1
is this result acceptable? Its not expected by me.
static void Main(string[] args)
{
Test obj1 = new Test();
Console.WriteLi ne("obj1.Inc() : {0}", obj1.Inc());
Console.WriteLi ne("obj1.Num: {0}", obj1.Num);
Test obj2 = new Test();
Console.WriteLi ne("obj2.Inc() : {0}", obj2.Inc());
Console.WriteLi ne("obj2.Num: {0}", obj2.Num);
}
public class Test
{
public int Num = 0;
private static int _num = 0;
public int Inc()
{
this.Num = Test.sinc();
return this.Num;
}
private static int sinc()
{ // toggle below statements
//return Test._num ++;
return ++ Test._num;
}
} 5 1306 ke****@hotmail. com wrote: Try the below simple program. It give expected result.
1 1 2 2
Then toggle the statements in private static int sinc(), to use Test._num ++ . The result:
0 0 1 1
is this result acceptable? Its not expected by me.
Can I ask you what you were expecting, and why? What is your
understanding of the difference between prefix ++ and postfix ++ ?
[code quoted for reference] static void Main(string[] args) { Test obj1 = new Test(); Console.WriteLi ne("obj1.Inc() : {0}", obj1.Inc()); Console.WriteLi ne("obj1.Num: {0}", obj1.Num);
Test obj2 = new Test(); Console.WriteLi ne("obj2.Inc() : {0}", obj2.Inc()); Console.WriteLi ne("obj2.Num: {0}", obj2.Num); }
public class Test { public int Num = 0;
private static int _num = 0;
public int Inc() { this.Num = Test.sinc(); return this.Num; }
private static int sinc() { // toggle below statements //return Test._num ++; return ++ Test._num; } }
--
Larry Lard
Replies to group please
It is exactly what I would expect to see, the pre increment operator
increments and then returns the incremented value, while the post increment
operator returns the original value and then increments it. What did you
expect.
"ke****@hotmail .com" wrote: Try the below simple program. It give expected result.
1 1 2 2
Then toggle the statements in private static int sinc(), to use Test._num ++ . The result:
0 0 1 1
is this result acceptable? Its not expected by me.
static void Main(string[] args) { Test obj1 = new Test(); Console.WriteLi ne("obj1.Inc() : {0}", obj1.Inc()); Console.WriteLi ne("obj1.Num: {0}", obj1.Num);
Test obj2 = new Test(); Console.WriteLi ne("obj2.Inc() : {0}", obj2.Inc()); Console.WriteLi ne("obj2.Num: {0}", obj2.Num); }
public class Test { public int Num = 0;
private static int _num = 0;
public int Inc() { this.Num = Test.sinc(); return this.Num; }
private static int sinc() { // toggle below statements //return Test._num ++; return ++ Test._num; } }
I know the pre and post increment stuff.
But what pickle me is the logic and flow:
- the increment is done within a routine -- Test.sinc()
- this.Num value should be from the routine, so in my logic, regardless
its pre or post, the value returned should be incremented already.
- to me, the effect of the pre and post should only be affective inside
the routine, should not affect the result returning to the calling.
I did a debug trace, and put on a watch on the this.Num and Test._num.
And you will be very surprise of the findout:
- during the line [return Test._num ++], Num is 0, _num is 0
- next step, back to the calling line, _num is 1 (red), Num is still 0
- next step, still same _num is 1 (black), Num is still 0
(red)...????!!! !
funny?
tony lock wrote: It is exactly what I would expect to see, the pre increment operator increments and then returns the incremented value, while the post increment operator returns the original value and then increments it. What did you expect.
"ke****@hotmail .com" wrote:
Try the below simple program. It give expected result.
1 1 2 2
Then toggle the statements in private static int sinc(), to use Test._num ++ . The result:
0 0 1 1
is this result acceptable? Its not expected by me.
static void Main(string[] args) { Test obj1 = new Test(); Console.WriteLi ne("obj1.Inc() : {0}", obj1.Inc()); Console.WriteLi ne("obj1.Num: {0}", obj1.Num);
Test obj2 = new Test(); Console.WriteLi ne("obj2.Inc() : {0}", obj2.Inc()); Console.WriteLi ne("obj2.Num: {0}", obj2.Num); }
public class Test { public int Num = 0;
private static int _num = 0;
public int Inc() { this.Num = Test.sinc(); return this.Num; }
private static int sinc() { // toggle below statements //return Test._num ++; return ++ Test._num; } }
What is happening is exactly what should happen, Num is only set by the value
returned and that is returned as 0, only when the routine is called a second
time do you see the new value, that was generated after the original number
was returned.
"ke****@hotmail .com" wrote: I know the pre and post increment stuff.
But what pickle me is the logic and flow: - the increment is done within a routine -- Test.sinc() - this.Num value should be from the routine, so in my logic, regardless its pre or post, the value returned should be incremented already. - to me, the effect of the pre and post should only be affective inside the routine, should not affect the result returning to the calling.
I did a debug trace, and put on a watch on the this.Num and Test._num. And you will be very surprise of the findout: - during the line [return Test._num ++], Num is 0, _num is 0 - next step, back to the calling line, _num is 1 (red), Num is still 0 - next step, still same _num is 1 (black), Num is still 0 (red)...????!!! !
funny?
tony lock wrote: It is exactly what I would expect to see, the pre increment operator increments and then returns the incremented value, while the post increment operator returns the original value and then increments it. What did you expect.
"ke****@hotmail .com" wrote:
Try the below simple program. It give expected result.
1 1 2 2
Then toggle the statements in private static int sinc(), to use Test._num ++ . The result:
0 0 1 1
is this result acceptable? Its not expected by me.
static void Main(string[] args) { Test obj1 = new Test(); Console.WriteLi ne("obj1.Inc() : {0}", obj1.Inc()); Console.WriteLi ne("obj1.Num: {0}", obj1.Num);
Test obj2 = new Test(); Console.WriteLi ne("obj2.Inc() : {0}", obj2.Inc()); Console.WriteLi ne("obj2.Num: {0}", obj2.Num); }
public class Test { public int Num = 0;
private static int _num = 0;
public int Inc() { this.Num = Test.sinc(); return this.Num; }
private static int sinc() { // toggle below statements //return Test._num ++; return ++ Test._num; } }
<ke****@hotmail .com> wrote: I know the pre and post increment stuff.
I don't think you do.
But what pickle me is the logic and flow: - the increment is done within a routine -- Test.sinc() - this.Num value should be from the routine, so in my logic, regardless its pre or post, the value returned should be incremented already.
No, you're confused about what pre/post increments do.
Pre-increment means "change the value of the variable, and return the
new value"
Post-increment means "remember the value of the variable, change it,
then return the original value".
So when you use a post increment, the value of the expression is the
value before any increment occurs.
If you changed your code to:
Test._num++
return Test._num
*then* you would see the same results whether you used pre or post
increment.
- to me, the effect of the pre and post should only be affective inside the routine, should not affect the result returning to the calling.
I did a debug trace, and put on a watch on the this.Num and Test._num. And you will be very surprise of the findout: - during the line [return Test._num ++], Num is 0, _num is 0 - next step, back to the calling line, _num is 1 (red), Num is still 0 - next step, still same _num is 1 (black), Num is still 0 (red)...????!!! !
Well, that's an issue with watching a property which changes values
while you're in the debugger.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dominik B. |
last post by:
I'm executing these commands:
$text="example text";
$parts=explode(" ", $text);
$result=$parts;
$result becomes a weird array.
echo "$result" prints 'example',
|
by: redneck_kiwi |
last post by:
All:
I have an application that has been working well for 3-4 months now
without any issues (that I am aware of). A day or two ago, one of our
employees that use this application called to say she was getting a
weird message:
"Warning: Cannot modify header information - headers already sent by
(output started at /www/htdocs/sys36/viewhist.php:2) in
/www/htdocs/sys36/viewhist.php on line 5"
|
by: redneck_kiwi |
last post by:
Hi all:
I have a really weird problem. I am developing a customer catalog
system for my company and as such have delved into sessions for
authentication and access levels. So far, I have managed to get a
working system just about finished.
I am building an interface for our customer service folks to use to
manage registered customers and am seeing some weird behavior.
|
by: Skybuck Flying |
last post by:
This is the source code for nrand48 from gnuwin32 libgw32c
long int nrand48 (xsubi) unsigned short int xsubi;
{
long int result;
(void) __nrand48_r (xsubi, &__libc_drand48_data, &result);
return result;
}
|
by: Dees |
last post by:
Hi,
I am facing a weird problem with HTTPS and Request.Url.AbsoluteUri in my
ASP.NET application. Here is the scenario -
1. I have a menu page (Default.aspx), which has the following anchor -
<a href="ApplicationHost.aspx">Open Application</a>
2. The ApplicationHost.aspx has the following code in its Load event handler
-
'ASSUMPTION -
| |
by: Bo Peng |
last post by:
Dear list,
I spent the last 12 hours in catching this bug (?) and what I found out
is very difficult to explain:
Basically, I need to call a user-provided function many times, with a
tuple as parameter.
C/C++ side: (class A, constructed using a python function m_func)
|
by: Mick_fae_Glesga |
last post by:
OK, the solution to this is probably blindingly obvious to everyone,
but... surely it can't be right.
I am compiling with borland bcc32 free compiler
this piece of code is designed to identify the most significant bit in
a given element in an array of unsigned longs. Now I realise there may
be a more efficient way to do this, and if you know a better way please
let me know.
|
by: jerrygarciuh |
last post by:
Hi all,
The following script is giving me weird problems. I have in this
directory an index.php and hurricane.php.
If the script gets $i = 'on' it is supposed to back up the current
index into a file called normal.php and then copy hurricane.php into
index.php. This should create a backup of the index and then put the
hurrican alert in place.
|
by: dtschoepe |
last post by:
Hi,
I am working on a project for school and I am trying to get my head
around something weird. I have a function setup which is used to get
an input from stdin and a piece of memory is created on the heap using
malloc. This input is then run through a regex and another test. The
result is passed back as a char pointer to the program for further
processing
The first time I call this function, it performs normally. However,
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |