473,385 Members | 1,958 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,385 software developers and data experts.

Syntax error: Overloading the = operator?

Hello,

Thanks for reviewing my question. I am trying to override the = operator
for my UserControl class; However, I am getting a syntax error.

public class MyClass
{
private int x = 0;

public static UserControl1 operator =(int y)
{
x= y;
return this;
}
}

My goal is to offer an assignment similar to this:

MyClass B;
b = 1;

Many Thanks
Peter
Nov 16 '05 #1
2 1287
> I am trying to override the = operator

This is not allowed in C#. However, you can provide an implicit type
conversion operator instead that will do what you want:

public class MyClass
{
private int x = 0;

public MyClass(int y)
{
x = y;
}

public static implicit operator MyClass(int y)
{
return new MyClass(y);
}
}

This should work now:

MyClass B;
b = 1;

Mark
Nov 16 '05 #2
Peter,

You can not override the = operator in C#. You will have to find
another way. You might want to look into overloading an implicit cast
operator. Check out the documentation on the implicit keyword for more
information.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
Hello,

Thanks for reviewing my question. I am trying to override the = operator
for my UserControl class; However, I am getting a syntax error.

public class MyClass
{
private int x = 0;

public static UserControl1 operator =(int y)
{
x= y;
return this;
}
}

My goal is to offer an assignment similar to this:

MyClass B;
b = 1;

Many Thanks
Peter

Nov 16 '05 #3

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: Laxman | last post by:
==C.H <== #include <fstream.h> class C { public: C(int i); friend ofstream& operator<<(ofstream& os, const C&); }; ------------------------------------------------------------ ==B.H <==
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
8
by: andrew browning | last post by:
I am getting an istream overloading error that looks like this: error: no match for 'operator>>' in 'ins >> target->abrowning_rational::Rational::numerator' Below is the .h file: #ifndef...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
19
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.