Here is my coding, but I get an error saying that I can not change the varrible more then one time, how do I set it so that I can change it more then once rather then making 3 new different varribles? Also can I get some help with the math because I put A + B (A is 1 and B is 2) When i debug it it will come up as 12 and not 3 etc here is the full code with all the errors too..
Hello Ellis,
Your code/errors didn't show up in your post, but I am assuming since you are doing A + B (A being 1, and B being 2) and getting "12", it sounds like you have created string type variables instead of integer. If you haven't already I would start at RB's C# crashcourse: http://rbwhitaker.wikidot.com/c-sharp-tutorials, most notably tutorial #5 on variables as it goes over all the types, declaring them, resetting them, etc.
Have fun.
It's always nice to see I'm not the only one answering questions here! Thanks ff8jake!
Where's the code? :)
Without seeing your code, it's hard to say for sure. A couple of things come to mind, when I see your description. Have you marked a variable as readonly? If so, it can only be assigned to once, and then, only in certain places (like a constructor). If you're making lots of changes, you don't want a readonly variable. So get rid of that, if that's your problem. Readonly is for making constants (one way to do it, anyway) or for making classes or structs that are "immutable"—that is, they are uneditable once they've been set up. If you're not trying to do that, you don't want readonly.
Your 1 + 2 = 12 thing is kind of odd. I suspect that whatever you're doing, it is turning A and B into string form before the addition, and then doing string addition instead of integer addition. That means it will append the text together instead of doing actual math.
string text = "1" + "2"; // results in the string "12". int number = 1 + 2; // results in the number 3.
It is probably more realistic that what's happening is something like this:
int a = 1; int b = 2; Console.WriteLine("Your answer is " + a + b);
In this kind of a case, the error is a lot more subtle. Like with math, when the computer is working through this, it obeys order of operations. Order of operations means it does some operations (like addition) before others. If you remember from math classes, the basic order of operations is something like "do parentheses first, powers and roots second, multiplication and division third, and then addition and subtraction last." If you have more than one of one type of operation, you work left to right (though in math, order doesn't usually matter, because 3 + 4 is the same as 4 + 3).
Looking back at that line, we're working only with addition. There's no parentheses, powers, or multiplication. So we start on the left and work our way to the right. We have a string ("Your answer is " and a number to add. Of course, it doesn't make sense to add a word and a number together, so we need to change one type to another. The compiler knows how to convert numbers to text, but not the other way around, so it will convert the value in the a variable to text: "1". Then it does string addition (called concatenation) and gets "Your answer is 1". The same thing then happens with our second addition, giving us a final result of "Your answer is 12".
This can be resolved in a couple of ways. First, we can do the addition operation separately, in a way that the computer will not try to convert to strings:
int a = 1; int b = 2; int result = a + b; Console.WriteLine("The answer is " + result);
Or we can use parentheses to do the numeric addition first, before it converts things to a string:
int a = 1; int b = 2; Console.WriteLine("The answer is " + (a + b));
In this case, because of the order of operations, we do the parentheses first, so a and b will get added together first. Since they are both numbers, the computer will do real, numerical addition with them, giving us a result of 3. Then it does the second addition, adding the string "The answer is " with the number we got as a result previously, 3. The final output will be "The answer is 3."
Let me know if that doesn't make sense…
it wouldn't let me copy and paste the code but the code went a little something like
int a = 1;
int b = 2;
int c = 3;
int d = 4;
Console.WriteLine(a);
int a = b + c
Console.Writeline(a);
It went something like that without all the fancy brackets ofcourse but I hope you see what im trying to say is how can I change the value of a again after using it before hand?
I think I see the problem! (Assuming it isn't a typo.)
So, before you use a variable, you have to "declare" it. This tells the computer, "Hey, I need a place to stick some information." To do that, you need to tell the computer two things: the type of information you're going to store there, and a name/label for the place. After that, any time you want to use that variable, you can refer to it by that name. You can assign a value to that spot, or you can retrieve the current value in that spot. But after the first time, you don't need to declare it again.
C# (and many other similar languages) allow you to both declare a variable and assign an initial value to it all at once, though that's not required.
So going back to the code you wrote. On the first line, you declare a variable named a, with the type int. This means that the compiler will make a spot for you to stick integer numbers up to a certain size (32 bits or 4 bytes) that you can refer to as a. You could have done this with the code int a; followed by a = 1; but you're using the thing I just mentioned about declaring a variable and assigning an initial value to it all at once.
You then do the same thing with b, c, and d.
You're then calling the Console.WriteLine method, handing off the contents of a to that method to print out.
Next (and here's where the problem is) you're trying to put the sum of b and c in the variable a. But because you've got that //int on the line there, the compiler thinks you're trying to create a second variable called a, and it knows you can't name two different variables the same thing.// After the initial spot, you do not need to use the variable's type. You can just use it by name.
So while int a = b + c; doesn't work, if you just said a = b+ c; it would.
On your first line, you're declaring the variable, but later on, you don't need to declare it again, so you don't need to indicate the variable's type at that point.
Does that make sense?

