"-" operator cannot be applied to a string

Asked

Viewed 318 times

4

I’m trying to make some changes to a ready code, but I came across a problem with a line of code written in C#:

string[] textArray1 = new string[] { -899253461.ToString() };

In this case, Visual Studio tells me that the "-" operator cannot be applied to the type operand string. I wonder if this is a reference problem as the DLL this code is part of is fully functional.

  • 1

    { "-899253461".ToString() };

  • 5

    @Cypherpotato If surrounded with quotation marks you do not need to use the method ToString.

2 answers

10

The problem is precedence of operators having their table seen in the documentation. The numerical negation operator has a lower precedence than the member access operator.

Applying precedence the code can be read like this:

var textArray1 = new string[] { -(899253461.ToString()) };

When you really wanted it:

var textArray1 = new string[] { (-899253461).ToString() };

So if writing like this works.

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

This makes more sense if you use a variable:

var textArray1 = new string[] { (-numero).ToString() };

If you are actually using a literal it makes no sense to use this form of syntax, you can use a *direct string:

var textArray1 = new string[] { "-899253461" };

It’s also unlikely that it makes sense to have a array Then he only has one element and he can’t be expanded, so why do this? If it were only to demonstrate the case it would be ok, but it is clear from the comments that the code is used in larger context. Of course there is some case where it makes sense and be passed as an argument of a method that expects a array, but in this case probably should only create the value for the argument and not a variable.

Note that this is not a problem of Visual Studio or the lost compiler, it is a well established rule by the language and enforced by the compiler. The programmer should know this rule and apply the code necessary to better show his intention, which in this case is to apply the parentheses to the sign to stay next to the number before to make the conversion to string. The way you wrote the normal number is converted to string and then try to apply the negative in this text, so it makes the mistake. And so it is not necessary to separate the number in another line, only put parentheses.

And C# does not use the concepts of primitive types.

  • Thanks to you and everyone else for the answers, but unfortunately no method presented worked, not in the sense that the VS considered error, but rather in the functionality itself. dll after compilation as it presents MISSING.STRING.VALUE exactly where the referent element should be displayed. I had already tried some things before my question, as some of the answers suggested, however, all gave me in the same error after the compilation and execution of . dll.

  • 2

    There is already another problem, what was answered here was the mistake you put in the question. Even in the presented code there is no other type of error. For another problem ask another question,.

  • Exactly why I made if this would be a reference error, because it seemed to me to be another problem unrelated to the code, since all the answers presented here did not work. I know that the original code was made in VS, because it uses certain modules that only exist in VS, and I know that is correct, because I checked other dlls similar to the one I am working, and all, without exception, present the same structure in their strings.

  • 2

    My answer worked and I showed it to you in two Ides online. There is no reference error by any of its presented code has no external reference to the language, so there is no way to give this error. The error you are reporting now is something other than a piece of code that is not in the question. The problem with this question has nothing to do with DLL. If you have any other problem elsewhere the code involves post DLL in a new question. And be clear on the new question, here’s being vague.

  • I was able to solve the problem without changing the code, very simple in fact, and in fact it was a reference that was not on the list, and it worked in the form that is presented. Well I’ll try to thank you, because in a way it was answered, but I don’t think I’ll come back here no more.

  • Friend, the community is unfortunately still a little intolerant... But... The forum is wonderful, I recommend you come back here whenever you have problems... After all in most cases the problem is solved ;p &#Xa precisely pq are not primitive.

  • 3

    @Marccuszavadzki putting something wrong in quotes doesn’t make it right. What he is talking about is neither type by reference but reference to the external package, but if he were to speak the opposite of type by reference would be a type by value and not a primitive type since a string is a type by reference and is primitive (where this concept is adopted). Now you say that the compiler is lost and he is not lost, he is adopting a rule established by language, as it is in my answer. And it says that language has a problem and it doesn’t. Note that I didn’t even mention int because...

  • 4

    ...in this case it is even true, but another number might not be a int, When we’re trying to teach something to the person and not just solve the person’s problem we need to tell them how things really are. And this isn’t a forum, it’s a question and answer site. Unfortunately many people do not understand this and do not think that quality matters because in forums quality does not usually matter anyway. Here it is different, and people like it because it has more quality than is found there (at least it was like this, it has gotten much worse lately).

  • 3

    This was only possible because people are intolerant of poor quality, and vote rigging. Those who are committed to error do not like this, very typical in Brazil. But we still try, until tired, and then the quality goes away and is almost like a forum and goes out saying that Sopt already had quality, worth for the old content.

Show 4 more comments

3

Dude, no referral error is no, because he’s wearing dudes 'primitivos'.

I believe the problem is that the compiler gets lost in the signals, it first converts the inteiro for string and then apply the signal, so the error.

If you use a () to first create the int and then convert to string already resolves

Example:

string[] textArray1 = new string[] { (-899253461).ToString() };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.