C# question?
-
I am working on this particular part of code in c# and I keep receiving error messages and I dont know what I am doing wrong. Can anyone help me out?
public static void personalDetails()
{
string [,] books =
{"The Time Machine, Science Fiction, By H.G.Wells",3};
{"Ender's Game, Science Fiction, By Orson Scott Card",1};
{"Brave New World, Science Fiction, By Aldous Huxley",3};
{"Frankenstein, Horror, By Stephen King",1};
{"Misery, Horror, By Stephen King",2};
{"The Dark Half, Horror, By Stephen King",2};
{"Firestarter, Horror, By Stephen King",3};
{"Torment, Mystery, By Lauren Kate",3};
{"Before I fall, Mystery, By Lauren Oliver",3};
{"Entwined, Mystery, By Heather Dixon",2};
{"The Thirteenth Chime, Mystery, By Heather Dixon",1};
{"My Sister's Keeper, Mystery, By Jodi Picoult",3};
{"Harry Potter and the Deathly Hallows(Harry Potter,#7), Mystery,By J.K.Rowling",2};
{"The Da Vinci Code, Mystery, By Dan Brown",2};int book const MAX\_BOOK = 5; if(book >=5) Console.WriteLine("You are at your limit!")
}
-
I am working on this particular part of code in c# and I keep receiving error messages and I dont know what I am doing wrong. Can anyone help me out?
public static void personalDetails()
{
string [,] books =
{"The Time Machine, Science Fiction, By H.G.Wells",3};
{"Ender's Game, Science Fiction, By Orson Scott Card",1};
{"Brave New World, Science Fiction, By Aldous Huxley",3};
{"Frankenstein, Horror, By Stephen King",1};
{"Misery, Horror, By Stephen King",2};
{"The Dark Half, Horror, By Stephen King",2};
{"Firestarter, Horror, By Stephen King",3};
{"Torment, Mystery, By Lauren Kate",3};
{"Before I fall, Mystery, By Lauren Oliver",3};
{"Entwined, Mystery, By Heather Dixon",2};
{"The Thirteenth Chime, Mystery, By Heather Dixon",1};
{"My Sister's Keeper, Mystery, By Jodi Picoult",3};
{"Harry Potter and the Deathly Hallows(Harry Potter,#7), Mystery,By J.K.Rowling",2};
{"The Da Vinci Code, Mystery, By Dan Brown",2};int book const MAX\_BOOK = 5; if(book >=5) Console.WriteLine("You are at your limit!")
}
There are several problems here:
const int MAX\_BOOK = 5; public static void personalDetails() { string\[,\] books = { { "The Time Machine, Science Fiction, By H.G.Wells", "3" }, { "Ender's Game, Science Fiction, By Orson Scott Card", "1" } }; int book = 0; if (book >= MAX\_BOOK) Console.WriteLine("You are at your limit!"); }
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I am working on this particular part of code in c# and I keep receiving error messages and I dont know what I am doing wrong. Can anyone help me out?
public static void personalDetails()
{
string [,] books =
{"The Time Machine, Science Fiction, By H.G.Wells",3};
{"Ender's Game, Science Fiction, By Orson Scott Card",1};
{"Brave New World, Science Fiction, By Aldous Huxley",3};
{"Frankenstein, Horror, By Stephen King",1};
{"Misery, Horror, By Stephen King",2};
{"The Dark Half, Horror, By Stephen King",2};
{"Firestarter, Horror, By Stephen King",3};
{"Torment, Mystery, By Lauren Kate",3};
{"Before I fall, Mystery, By Lauren Oliver",3};
{"Entwined, Mystery, By Heather Dixon",2};
{"The Thirteenth Chime, Mystery, By Heather Dixon",1};
{"My Sister's Keeper, Mystery, By Jodi Picoult",3};
{"Harry Potter and the Deathly Hallows(Harry Potter,#7), Mystery,By J.K.Rowling",2};
{"The Da Vinci Code, Mystery, By Dan Brown",2};int book const MAX\_BOOK = 5; if(book >=5) Console.WriteLine("You are at your limit!")
}
Where to start? Well, you haven't initialised your array, so the first line needs to become:
string [,] books = {
Next, you are adding in an integer into each line but it's expecting 2 strings so you should make it:
{"The Time Machine, Science Fiction, By H.G.Wells","3"}, // Note the comman, and not a semi-colon
As you are using an array initializer here, you need to end it with another
}
because you are adding in multiple items. I've already taken care of the first one for you at the top, so you'd end your initialisation with:{"The Da Vinci Code, Mystery, By Dan Brown","2"}};
Note that the lists in the array must be separated by commas and not semi-colons.
int book
needs to be terminated with a semi-colon as in:int book;
Beyond that, you declare a constant but don't tell it what type the constant is, so try
const int MAX_BOOK = 5;
You haven't terminated your
Console.WriteLine
with a semi-colon. Make this:Console.WriteLine("You are at your limit!");
Now, some general comments. First of all, you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0. I assume you mean to assign it to be the length of the array, so you actually need to use that value in there (hint - you need to use books.Length). Secondly, you have declared a constant that you don't use - this is not a great idea; use the constant in place of the 5 in the if test. Honestly, this is fairly basic stuff. You have to learn to read the compiler output and figure this stuff out. I saw the problems in your code just by looking at it - I didn't have the advantage of the compiler errors here; you do, so learn to use them. If you're using Visual Studio, the errors will be underlined and they will tell you exactly what's wrong.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Where to start? Well, you haven't initialised your array, so the first line needs to become:
string [,] books = {
Next, you are adding in an integer into each line but it's expecting 2 strings so you should make it:
{"The Time Machine, Science Fiction, By H.G.Wells","3"}, // Note the comman, and not a semi-colon
As you are using an array initializer here, you need to end it with another
}
because you are adding in multiple items. I've already taken care of the first one for you at the top, so you'd end your initialisation with:{"The Da Vinci Code, Mystery, By Dan Brown","2"}};
Note that the lists in the array must be separated by commas and not semi-colons.
int book
needs to be terminated with a semi-colon as in:int book;
Beyond that, you declare a constant but don't tell it what type the constant is, so try
const int MAX_BOOK = 5;
You haven't terminated your
Console.WriteLine
with a semi-colon. Make this:Console.WriteLine("You are at your limit!");
Now, some general comments. First of all, you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0. I assume you mean to assign it to be the length of the array, so you actually need to use that value in there (hint - you need to use books.Length). Secondly, you have declared a constant that you don't use - this is not a great idea; use the constant in place of the 5 in the if test. Honestly, this is fairly basic stuff. You have to learn to read the compiler output and figure this stuff out. I saw the problems in your code just by looking at it - I didn't have the advantage of the compiler errors here; you do, so learn to use them. If you're using Visual Studio, the errors will be underlined and they will tell you exactly what's wrong.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Pete O'Hanlon wrote:
you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0.
Worse actually, it will fail to compile. It doesn't satisfy the definite assignment rules (it doesn't even try).