|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C# Doesn't like my Else's
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Mobo Master
![]() |
So started working on a "Vending Machine" program in C# however All of my else's get flagged. I realise that you can use the switch function instead but before i start changing it I'd like to get to the bottom of why I am getting this error.
![]() Cheers
__________________
Electrical Engineering and Computer Science Student (Joint Bsc Hons) STUDENT FINANCE HURRY UP!!! Has far to many components for a 20 year old, and while you're at it forgive my spelling, the keyboard is dyslexic
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||
|
PC Gamer
![]()
Join Date: Apr 2009
Location: South-West, UK.
Posts: 1,233
Rep: 137
![]() ![]() Unique Rep: 123
Trader Rating: 0
|
= is used for writing
== is used for reading. Change the = in the Else's/If's to == and it should be fine and dandy, good luck ![]() Edit: You should also use "C" to check if it's C.
|
|||||||||
|
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Mobo Master
![]() |
Quote:
What do you meant by use "C"Edit: Tried it and the red "squiggle" is only at the end of the Order == bit like its expecting a ; My else's are still flagged with squiggles. It says "Invalid expression term 'else'" and "; expected"
__________________
Electrical Engineering and Computer Science Student (Joint Bsc Hons) STUDENT FINANCE HURRY UP!!! Has far to many components for a 20 year old, and while you're at it forgive my spelling, the keyboard is dyslexic
Last edited by Starbuck5000 : 10-12-09 at 03:40 PM |
||||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
.
![]() |
Also, you should be using 'else if's, not else's
Also, if you're checking for a string...ie. "C", you may need to use .equals("C") rather than ==. example: if c do this else if d do this else if e do this else do this That's extremely vague, but hopefully you get the idea.
__________________
Imaging with Windows PE Please keep the OCN Terms of Service in mind when posting.
Last edited by BiG O : 10-12-09 at 03:39 PM |
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||
|
PC Gamer
![]()
Join Date: Apr 2009
Location: South-West, UK.
Posts: 1,233
Rep: 137
![]() ![]() Unique Rep: 123
Trader Rating: 0
|
Quote:
But yeah; he's right (Don't rep me for this btw )
|
||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||||
|
WaterCooler
![]() |
You will have to use character literals for what you are equating to your variable Order:
Quote:
Also, instead of hardcoding 'C', T, O, H in there, you can declare constants at the top of your file and change what character the user may put in down the road: Quote:
__________________
|
|||||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
AMD Overclocker
![]() |
== checks if objects are equal. ObjectA == ObjectB. Do they use the same memory address.
.equals() for string values is checking to see if the characters match. Finally as posted above. = is used for assignment.
|
|||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||||
|
.
![]() |
Quote:
__________________
Imaging with Windows PE Please keep the OCN Terms of Service in mind when posting.
|
||||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||||
|
Mobo Master
![]() |
Tried adding "else if" however its now saying that I have a "use of unassigned local variables"
![]() Quote:
Like this ![]()
__________________
Electrical Engineering and Computer Science Student (Joint Bsc Hons) STUDENT FINANCE HURRY UP!!! Has far to many components for a 20 year old, and while you're at it forgive my spelling, the keyboard is dyslexic
|
||||||||||||||
|
|
|
|
|
#10 (permalink) | |||||||||||
|
nVidia Enthusiast
|
no, declare the constants at the class level not in the method.
__________________like this: Code:
public class VendingMachine
{
private const char COFFEE = 'C';
}
|
|||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|