473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

custom operator++ yields InvalidProgramException

The following code yields error CS0131: "The left-hand side of an assignment
must be a variable, property or indexer": (new int())++;

However, when using a class with a custom operator++, an
InvalidProgramException is thrown. I assume this is a compiler bug of some
sort, and that error CS0131 should be raised instead.

Here's a complete program that illustrates the issue:

namespace ErrorTest
{
class Program
{
public Program(int _) { i = _; }

public static Program operator ++(Program b) {
Program tmp = new Program(b.i);
tmp.i++;
return tmp;
}

public int i;

static void Main(string[] args) {
(new Program(0))++;
}
}
}

Running PEVerify on the above code yields a stack underflow in Main at
Offset 0x1. The dissassembly of Main is as follows:

..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 7 (0x7)
.maxstack 8
IL_0000: nop
IL_0001: call class ErrorTest.Program
ErrorTest.Program::op_Increment(class ErrorTest.Program)
IL_0006: ret
} // end of method Program::Main

This was all experienced using VC# 2005 Standard.

DZJr
Oct 4 '06 #1
2 1912
Dennis Zickefoose wrote:
The following code yields error CS0131: "The left-hand side of an
assignment
must be a variable, property or indexer": (new int())++;

However, when using a class with a custom operator++, an
InvalidProgramException is thrown. I assume this is a compiler bug of
some sort, and that error CS0131 should be raised instead.

Here's a complete program that illustrates the issue:

namespace ErrorTest
{
class Program
{
public Program(int _) { i = _; }

public static Program operator ++(Program b) {
Program tmp = new Program(b.i);
tmp.i++;
return tmp;
}

public int i;

static void Main(string[] args) {
(new Program(0))++;
}
}
}

Running PEVerify on the above code yields a stack underflow in Main at
Offset 0x1. The dissassembly of Main is as follows:

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 7 (0x7)
.maxstack 8
IL_0000: nop
IL_0001: call class ErrorTest.Program
ErrorTest.Program::op_Increment(class ErrorTest.Program)
IL_0006: ret
} // end of method Program::Main

This was all experienced using VC# 2005 Standard.

DZJr
Hi,

It certainly looks like that, since there's no new object initialisation
before the call to op_Increment, which means that the stack would be empty
when the call to op_Increment was made.

Of course, I must ask, why would you do such a thing?

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 5 '06 #2
That's a gross simplification of the original code, which involved a factory
of sorts. Sometimes, I'm not interested in what the factory provides, but in
the incremented version of what the factory provides. I thought I could just
wrap it all in one line, but apparently ++ doesn't work that way. None of
that really matters, though...I can work around it easily enough.

I marked my original post as a question, but I'm not sure why. I really
just wanted to bring the issue up, since it appears like the compiler is
acting up and not emmitting an error when it's supposed to.

DZJr
Oct 5 '06 #3

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

Similar topics

1
by: Ryan M | last post by:
Hi, I have a major roadblock , where while debugging/running I get and InvalidProgramException (detected by the CLR) . This occurs when the program flow goes into a very large method with maybe...
3
by: Neil Zanella | last post by:
Hello, I would like to ask the following question concerning the C# as operator. I would like to know whether the difference between using a C-style cast such as double x = 0; float y =...
0
by: Ivana | last post by:
Hi. I'm having trouble with my PocketPC program. I'm getting an InvalidProgramException when the SqlCeDataAdapter.Fill() method is called, and I have no clue why! Is there anybody who can tell me...
5
by: Dave | last post by:
I am a VB programmer tring to learn C# I am stuck on a bit operator "&" in VB: ' checkLogin = 3 means "Login exists" and "Employee Number exists" If (checkLogin And 1) Then lbError.Text =...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
11
by: Kerneltrap | last post by:
Hi, I have a type: public struct Value { public static Value operator ++(Value v) { return new Value(); }
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
13
by: jehugaleahsa | last post by:
Hello: Today I learned that the + operator cannot be passed a delegate. I get an error from the CLR saying I have an invalid program. With that, I was wondering if someone could tell me what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.