Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Namespace in C#-I get an error every time.Please help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I would post this in C# forum,but it seems I would get an answer every three weeks or so.Anyway I hope somebody will help me here.I'm learning C# and my problem is that I can't define a namespace in a console app and I've tried everything.Here is an example:

namespace levelOne
{
//some code
}
What am I doing wrong?Thank you for helping me.
 
Can you give us more information:

-what error message (if any) are you getting?
-what are you putting in that //somecode part?

Jack
 
Ok,first I create console project,then when
it is opened I leave everything the way it
is and only add some code in Main() function.
Here is an example:


class Class1
{
static void Main(string args)
{ //line 11
namespace First{int x;}; //12
} //13
} //14

And than I run it and get an error.

Here's what it says in a task list:
- } expected //line 11
- } expected //11
-invalid token '}'in class,struct or
interface member... //12
-invalid token 'namespace'in class,
struct or interface ... //12
-type or namespace definition,
or end-of-file expected ... //14

Thank you for helping me.
 
oh, dude, namespaces can't be within classes.

You have to declare them outside of any class that you have in your file.
:)

Should be an easy fix. Why were you trying to put the namespace in your main sub?

Jack
 
that's right ^-^...

i'm just ready to answer him until i found out that you already answer him jack :)

anyway, are you also using visual studio.net jack?
 
Thank you.But don't blame me,blame stupid book 'Beginning C#' by Karli Watson.The guy didn't mention with one word that namespaces are to be written outside classes.Still,I have a question:
-can you define variables in a namespace?

-this is about SWITCH statement.According to book this should also work but it doesn't:

switch(3)
{
case 3:
case 1:
Console.WriteLine("oh my god");
break;
}

If instead of 3 the value of an expresion would be 1,than it would work but if it s 3 it doesn't.Again,I'm going by the book.Also,I have a VS.NET beta version.
Thank you.
 
variables can't be defined just in namespaces. For instance

Namespace Test
Dim i as integer

Class 1

End class

won't work. Variables always have to be defined within a class, or module, etc. So this will work:
Namespace Test
Class 1
Dim i as integer
End class

to your second question, if the value is 3, nothing is supposed to happen right? So are you getting an error and thats why it isn't working?

Hey ja: yeah, VS.NET Enterprise Architect
:)

Jack
 
Yes,here's what it says:
-Control cannot fall trough from one case label('case 3:')to another
-Control cannot fall trough from one case label('case 1:')to another

Thank you
 
hmmm...does c# support
Select Case
statements like VB does, or is the switch its equivalent? Have you looked online for other examples of switch statements, just in case the book has a typo?

Jack
 
of coz C# support select case (in VB), but the syntax is a little bit different...

switch (variable) {
case value :
// some code here
break; // if you don't put the break here, then it will continue to your next statement, even if your variable matches the value
case whatelse :
// some code here
break; // remember to put break
default :
// blabblalbal
}

hope this can help you ^0^
 
I know this example works,but so should the other.The whole purpose of it was that you don't need to write so much and so if any of cases matched that value,the sentances should be executed.Before moving to C# I did study C++for a month and that option was also present there and since C# is very similar to C++ and since the example was given in a book the author must've forgoten to add a bit of code or something.
Man,I need C# programers.I just hope they didn't study the same book.If so,we're all in a dark.
Thank you for helping me.
 
hah?? what do you mean it "this example works, but so should the other"? i've been using c# for the last 6 month (i've already used .net since beta 2), and i've also used the switch case syntax, and never found it as hard as you imagine... could you explain us why d u need the switch case syntax? can u explain us your problem? perhaps i could try that for you :)
 
I don't understand what upseted you so much.First of all I'm almost a complete beginner and if something should but doesn't work I naturaly get a bit upset.Unlike you,I don't know if and were the switch statements with that particular syntax will come in handy or even a must.
Second,I never said switch statements were anything difficult to comprehend.The switch syntax that doesn't work in C#(which by the way came from C# book)worked in c++,which I studied for a short time and didn't have any difficulty understanding it.
If that particular syntax doesn't work in c#,you could tell me,'dude,the author was wrong',but you never did that,did you?
Don't think I'm ungrateful,coz I'm not,but you've got a bit emotional.

PS I know syntax you showed works.That was never a question
 
Hey Afduyi,

I found this at
It might be the fix to your problem.

C# fixes this problem with a small change in the language definition: it requires that each case block in
a switch statement has to be left explicitly, e.g. with a break, return, continue or a goto statement.
Otherwise the compiler issues the error message "Control cannot fall through from one case label to
another".


According to this, the reason your code won't execute is because that first case doesn't have any ending statement (break, return, continue, goto), so try putting that in and see if it solves it.
:)

Jack
 
woww... it's getting hot here ^-^... hey afduyi, i apologize for making you mad, but i really don't intend to do so, sorry for that

by the way, i don't think i'm emotional man, i'm just trying to help (eventhough the words i used was sometimes a little bit confusing). or maybe i misunderstood your questions??? hope you understand, peace :)
 
Well,It goes both ways.I was playing some guns n roses while I was replieing to you and that's probably why words came out a bit harsh.Anyway it's cool.
To jfrost10:you mean this?

Switch(1)
{
case 1:break;
case 2:
Console.WriteLine("HEY");
break;
}

I've tried that,but 'HEY' statement is ignored.
Thank you both for helping me,I trully am grateful.

PS since I have beta version,I'll wait till i get an official release and see how it works and than maybe bother you some more with switch.Thank you!!!!!!!!!!!!

I bought me an illusion
An I put it on the wall
I let it fill my head with dreams
An I had to have them all
but oh the taste
Is never so sweet as...

Locomotive-W.Axl Rose //sorry guys,but I LOVE
GUNS N'ROSES
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top