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

left add unary operator ( += )


Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

....writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #1
8 3786
On 2005-01-29, Elementary Penguin <si*****@hare.krishna> wrote:

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?


No.

--
Tom Shelton
Jul 21 '05 #2
Tom Shelton wrote:
No.


Thanks, that's what I thought.

So I'm just going to do:

s1 = "A";
s1 = "B" + s1;

--
Texeme
http://texeme.com

Jul 21 '05 #3
No. And no, you can't overload "=+" because "=+" isn't a legal operator.

You can do

string s2 = "B";
s2 += s1;

instead.

-vJ

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #4
No there is no =+ operator
No you cannot create a brand new operator
Not in C# anyway
Cheers,

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #5

AFAIK there is no operator like this.
You'd need to create a new string class, just to expose this operator. Why
not just write
s1 = "B" + s1;

Alternatively you could create a new class with the operator

public class XString : System.String // not sure you can inherit from string
though?
{
public static void operator =+ ( XString xs1, XString xs2 )
{
xs1 = xs2 + xs1;
}
}

The final thing on this is that for string concatenation, it's better to use
the System.Text.StringBuilder object.
http://www.c-sharpcorner.com/Code/20...uilderComp.asp

Hope that helps.

Dan.

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #6
Dan Bass wrote:
public class XString : System.String // not sure you can inherit from string
though?
{
public static void operator =+ ( XString xs1, XString xs2 )
{
xs1 = xs2 + xs1;
}
}


This won't work because:
1) String is a sealed class
2) The =+ operator will result in a "Overloadable unary operator
expected" compiler error.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Jul 21 '05 #7
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:

<snip>
The final thing on this is that for string concatenation, it's better to use
the System.Text.StringBuilder object.
http://www.c-sharpcorner.com/Code/20...uilderComp.asp


That entirely depends on the context. If you actually need all of the
intermediate strings (ie the result of each concatenation) then you're
better off using straight string concatenation. Similarly, for a small
number of concatenations, the copy hit of string concatenation can
often be smaller than the hit of creating a StringBuilder.

Where StringBuilder really pays off is where you have string
concatenation in a loop.

You might find this article useful - it's about Java's StringBuffer,
but the principles are exactly the same:
http://www.pobox.com/~skeet/java/stringbuffer.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
Someone please correct me if I'm wrong.

As of CSharp 2003 it not possible to define a new operator ie; =+ also some
existing operators cannot be overloaded ie; +=.

Courtesy MSDN Section 7.2.2 Operator Overloading

<<<<<<<< Start Here>>>>>>>>>>

The overloadable unary operators are:

+ - ! ~ ++ -- true false
Although true and false are not used explicitly in expressions, they are
considered operators because they are invoked in several expression
contexts: Boolean expressions (Section 7.16) and expressions involving the
conditional (Section 7.12), and conditional logical operators (Section
7.11).

The overloadable binary operators are:

+ - * / % & | ^ << >> == != > < >= <=
Only the operators listed above can be overloaded. In particular, it is not
possible to overload member access, method invocation, or the =, &&, ||, ?:,
checked, unchecked, new, typeof, as, and is operators.

<<<<<<<< End Here>>>>>>>>>>

So you could choose from any of the above but you might find then a little
meaningless to the next programmer, who might just happen to be you in 6
months time.
My suggestion define a method called EqualsPlus until such a time you can
create custom defined operators.
Regards

Ian
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #9

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

Similar topics

3
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't...
7
by: John J | last post by:
To write a unary operator- operation that returns the negative of a value is it just as simple as subtracting the value from itself? ie. { return (value - value); } Thanks
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
8
by: Elementary Penguin | last post by:
Is there a left add unary operator ( or do I have to overload one ) ? For example: string s1 = "A"; s1 += "B"; Console.WriteLine( s1 )
13
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even...
6
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
16
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.