I am about done with the assignment but it won't run, why?

I think it has something to do with how I named it, it used to be named "Account" but I changed it to "Exception".
35 Replies
AppleCyder (NovaSprite)
The code may be incorrect but I just want for it to run This is what I have...
AppleCyder (NovaSprite)
These are all of the errors I see I only have an hour and a half until it's due if I could just get it to run...
Angius
Angius7mo ago
Your class is outside of the namespace Also, a class called Exception already exists, it's built into .NET
AppleCyder (NovaSprite)
So I should delete the original?
Angius
Angius7mo ago
Idk Why is this class even called Exception in the first place?
AppleCyder (NovaSprite)
cuz the assignment was about that but I should've kept the name as is
Angius
Angius7mo ago
About using exceptions, I assume Not about naming your account class "exception" I assume, again, that what you were supposed to do was something like
class NegativeBalanceException : Exception
{ }

class Account
{
private decimal _balance = 0;
public decimal Balance {
get => _balance
set {
if (value < 0) throw new NegativeBalanceException(),
_balance = value;
}
}
}
class NegativeBalanceException : Exception
{ }

class Account
{
private decimal _balance = 0;
public decimal Balance {
get => _balance
set {
if (value < 0) throw new NegativeBalanceException(),
_balance = value;
}
}
}
thus, using the custom exception you made So, no, you shouldn't have renamed your account class
AppleCyder (NovaSprite)
What should I do so that it fixes everything? Should I make a new file named Account and change all of the "Exceptions" as that?
Angius
Angius7mo ago
Only NegativeNumberException should inherit Exception, yes Your current Exceptions class should just be named Account And, ideally, yes, each class would be in its own file
AppleCyder (NovaSprite)
let me try that out I made a whole new file named Account but I get this...
AppleCyder (NovaSprite)
the public class Exception is also underlined as red
AppleCyder (NovaSprite)
I have an hour left maybe the professor will count even 1/3 of it since everything is basically there Oh I see know...
AppleCyder (NovaSprite)
Now I rename them all to Account?
Anchy
Anchy7mo ago
It looks like what you are doing is re-creating or renaming your project. You should choose a fitting name for your project and should be creating individual source files for your C# classes. and like was mentioned previously, a class named Exception already exists in the BCL so you will run into a conflict. Instead you should think of a fitting name for your custom exception (which was also mentioned previously) Don't create a new project per class, create a new source file per class from what I can see you should have a project with these source files: - Program.cs (Your entrypoint for the application) - NegativeNumberException.cs - Account.cs you classes should be sitting inside of one namespace inside those files maybe look into File Scoped Namespaces to simplify things and Scoping in general
AppleCyder (NovaSprite)
Well I renamed them and reduced the errors to this...
AppleCyder (NovaSprite)
but that's why I still have that top error right?
Anchy
Anchy7mo ago
it sounds like you have your Account class defined twice its worth googling the error code (or clicking on the error code link) to read why you are getting these errors
AppleCyder (NovaSprite)
makes sense I don't do that often enough
Anchy
Anchy7mo ago
you should get used to doing that
AppleCyder (NovaSprite)
I'll do that in my next assignment
AppleCyder (NovaSprite)
public class should have a different name right? where it says Account and is underlined red
Anchy
Anchy7mo ago
you should do it in this assignment
AppleCyder (NovaSprite)
true
Anchy
Anchy7mo ago
try take all the information I provided further up on board and look into the things I mentioned because they directly relate to the issues you are having I am not going to do the work for you, its up to you to research and learn why you are having these problems that will teach you more than I can
AppleCyder (NovaSprite)
Ok I'll try
Anchy
Anchy7mo ago
namely: - What is a C# Project? - What is a source file? - What is scope? - What is a namespace? - What is a file scoped namespace? - What do these error codes mean? - What is inheritance in C#? And any other questions about words you may not understand during your reading If you are reading one of the MS Docs and you don't understand what it is saying, maybe research some of the key words specified in that doc
FusedQyou
FusedQyou7mo ago
Your Account namespace only contains the NegativeNumbersException class currently. Account class is now in the root namespace which conflicts with the namespace of the same name. Make sure you do not use duplicate names If you rename your namespace and then place the class in it it should get rid of most exceptions But idk how your class is made so those exceptions could very much be valid Fix that, and then look into what was mentioned above I understand this assignment is already due so it is probably too late but either way this would fix it
AppleCyder (NovaSprite)
It finally worked, it just took a few renames in the code, thank you all for the help! Hopefully I'll get some sort of grade even if it's late
Want results from more Discord servers?
Add your server