Could someone explain why I have to convert _one to string but the other 3 variables in the following C#...
up vote
0
down vote
favorite
I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);
//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}
c#
New contributor
add a comment |
up vote
0
down vote
favorite
I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);
//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}
c#
New contributor
Take another look at your own code:Convert.ToString(_one)
. You are only converting_one
to a string here, then trying to add the numeric variables to it.
– Martin Parkin
Nov 19 at 13:14
1
Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16
What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20
Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);
//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}
c#
New contributor
I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);
//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}
c#
c#
New contributor
New contributor
New contributor
asked Nov 19 at 13:11
HELLHOUND0606
34
34
New contributor
New contributor
Take another look at your own code:Convert.ToString(_one)
. You are only converting_one
to a string here, then trying to add the numeric variables to it.
– Martin Parkin
Nov 19 at 13:14
1
Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16
What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20
Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46
add a comment |
Take another look at your own code:Convert.ToString(_one)
. You are only converting_one
to a string here, then trying to add the numeric variables to it.
– Martin Parkin
Nov 19 at 13:14
1
Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16
What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20
Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46
Take another look at your own code:
Convert.ToString(_one)
. You are only converting _one
to a string here, then trying to add the numeric variables to it.– Martin Parkin
Nov 19 at 13:14
Take another look at your own code:
Convert.ToString(_one)
. You are only converting _one
to a string here, then trying to add the numeric variables to it.– Martin Parkin
Nov 19 at 13:14
1
1
Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16
Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16
What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20
What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20
Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46
Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
There's nothing special about _one
, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did
Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);
If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
There's nothing special about _one
, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did
Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);
If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.
add a comment |
up vote
1
down vote
accepted
There's nothing special about _one
, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did
Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);
If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
There's nothing special about _one
, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did
Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);
If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.
There's nothing special about _one
, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did
Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);
If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.
answered Nov 19 at 13:19
Robin Bennett
1,23312
1,23312
add a comment |
add a comment |
HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.
HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.
HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.
HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375398%2fcould-someone-explain-why-i-have-to-convert-one-to-string-but-the-other-3-varia%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Take another look at your own code:
Convert.ToString(_one)
. You are only converting_one
to a string here, then trying to add the numeric variables to it.– Martin Parkin
Nov 19 at 13:14
1
Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16
What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20
Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46