C# Coding Confusion01 - Derived Classes run aborting on previously working code

Art Hansen 566 Reputation points
2020-11-25T16:05:25.453+00:00

I’m following two MS tutorials (https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/intro-to-csharp/object-oriented-programming[1]) which use bank accounts to introduce Classes and Derived Classes. The first of the two tutorials created a single bank account as a new class. A 2nd new class was created to – I believe – define the banking transaction record layout (the tute’s a bit fuzzy regarding the specific function of the Transaction class). This ran successfully.

The 2nd tutorial built on the 1st . A new project was created (I’m using a Notepad++/Admin cmd window (I have installed VS Community but that’s for a different question)) using the “dotnet new console” command. The three files – BankAccount, Program and Transaction – were copied into the new project folder (OOProgramming) and the namespace declaration changed consistent with the new project folder’s name. Performing a “mid-dev” test run as instructed errors out. Below are the comparative project structures graphically identifying leveraged files.
42771-code-confusion-01.png

No changes were made to Transaction.cs except the namespace update. BankAccount.cs has two changes the updated namespace and an new method:

public virtual void PerformMonthEndTransactions() { } // Added for OOP derived from this BASE  

In addition to the namespace update Program.cs has a number of new transactions intended to utilize the InterestEarningAccount and LineOfCreditAccount derived classes:

        static void Main(string[] args)  
        {  
 var giftCard = new GiftCardAccount("gift card", 100, 50);  
 giftCard.MakeWithdrawal(20, DateTime.Now, "get expensive coffee");  
 giftCard.MakeWithdrawal(50, DateTime.Now, "buy groceries");  
 giftCard.PerformMonthEndTransactions();  
 // can make additional deposits:  
 giftCard.MakeDeposit(27.50m, DateTime.Now, "add some additional spending money");  
 Console.WriteLine(giftCard.GetAccountHistory());  
  
 var savings = new InterestEarningAccount("savings account", 10000);  
 savings.MakeDeposit(750, DateTime.Now, "save some money");  
 savings.MakeDeposit(1250, DateTime.Now, "Add more savings");  
 savings.MakeWithdrawal(250, DateTime.Now, "Needed to pay monthly bills");  
 savings.PerformMonthEndTransactions();  
 Console.WriteLine(savings.GetAccountHistory());  
  
 BaseClass();  
         }  
  
          static void BaseClass()  


BaseClass is unaltered – previously and theoretically still –  working code.  I’ve not yet dealt with the GiftCardAccount derived class.  

All modified files have been verified as containing only desired changes using the text file comparison utility WinMerge.

The culprit command line per the compiler generated error message is:

private List<Transaction> allTransactions = new List<Transaction>(); // List<T> function  

This is unaltered previously working code.

I realize this has become lengthy and I apologize but if troubleshooting C# code is similar to troubleshooting I’ve done in other area better too much is better than not enough.

Any help will be appreciated.
Art

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,276 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2020-11-27T03:09:35.197+00:00

    Hi @Art Hansen , if you want to use Visual Studio to run this project, please refer to the following steps:

    1. Open the project folder, there should be a projectName.csproj file, use Visual Studio to open it. 43135-1.png.
    2. You should see a toolbar like this at this time: 43203-2.png Click the Start button at label 1 to run the current project. If this button is not visible for some reason, you can click the button at label 2 and
      select Add or Remove Buttons -> Debug Target to open it. If there is more than one project in the current solution, a combobox will be added to the toolbar to switch projects. 43211-3.png In fact, if it’s just a namespace error, you don’t even need to run the project. After completing the first step, Visual Studio will remind you
      during the compilation phase that you only need to hover your mouse over the class name that has the error, and it will appear A light bulb-
      shaped button, click on it and a prompt will appear, like the screenshot in my last answer. When adding screenshots, such blank lines are required.
      43221-4.png

    The platform may mistakenly think that this is the text you entered without the line.

    In addition, you can see the preview options here:
    43174-5.png

    You might need this: Visual Studio documentation


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2020-11-26T08:21:58.8+00:00

    This error is generally caused by the lack of some references. Since these classes are written by yourself, the only explanation is the namespace error.

    Is it possible that you only modified the namespaces of BankAccount and Program and forgot the Transaction class, or accidentally misspelled the word when manually modifying the namespace?

    I suggest you download Visual Studio. For errors like this, Visual Studio’s IntelliSense can easily help you solve them. It is very useful when you encounter exceptions.
    42898-1.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    **Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.*

    0 comments No comments

  2. Art Hansen 566 Reputation points
    2020-11-26T11:00:21.923+00:00

    @Timon Yang-MSFT Thank you for responding.

    Is it possible that you only modified the namespaces of BankAccount and Program and forgot the Transaction class, or accidentally misspelled the word when manually modifying the namespace?

    Absolutely possible. I tried to double check all of those but since you've narrowed down the probable culprit it's worth re-double checking.

    I suggest you download Visual Studio.

    I intended to post this in the relevant tool forum but since you've brought it up here ... I have installed VS Community and had started following the tutorials using it. Unfortunately 1/2 way through the first tutorial I was instructed to "run what you've built so far. If you're using Visual Studio, Select Start without debugging <u>from the Run menu</u>." Since I was encountering an error and couldn't find a Run Menu (screenshot of VS Community in attached image):

    43190-vs-menubar.jpg

    I switched to NotePad++. Do you know where I find the Run Menu or is it not available on the VS Community version?*

    PS: Not sure why but I'm having terrific problems getting an image to be displayed inline. I've tried "inset image" and "attachment" from the toolbar plus good ole paste. Yesterday I spent over an hour before one was accepted & I still don't know why the one in my OP worked. All of the images are png/jpg in the kb range so I'm definitely not bumping into the size ceiling. I see you've been successful; is there something obvious I'm missing???

    PPS: humm, seems like inline html formatting is also problematic ... maybe teething pains.

    0 comments No comments

  3. Art Hansen 566 Reputation points
    2020-11-28T06:58:52.727+00:00

    Hello @Timon Yang-MSFT as you suspected it was a missing character in the namespace name. I thought the compiler error was pointing to a namespace problem in BankAccount class file but I needed to actually go up-stream a level to the Transaction class file. So now I have gained experience in deciphering compiler error messages...

    There was a puzzling aspect to what I found, however, and it appears to be based on an invalid assumption I had that namespace must be consistent with the name of the folder containing the project:

    project folder name: OOProgramming
    namespace in Program: OOProgamming <-- note missing "r"
    namespace in BankAccount: OOProgamming <-- note missing "r"
    namespace in GiftCardAccount: OOProgamming <-- note missing "r"
    namespace in InterestEarningAccount: OOProgamming <-- note missing "r"
    namespace in LineOfCreditAccount: OOProgamming <-- note missing "r"
    namespace in Transaction: OOProgramming

    When I removed the 2nd "r" from the namespace declaration in the Transaction class file -- so that it was consistent with its peer files -- the Run was successful. When "namespace OOProgamming" is replaced by "namespace NonsenseName" in all project files the Run was again successful. Obviously my assumption regarding the relationship between the project folder named IS invalid (at least from the compiler's perspective although it's still logical from an organizing perspective) and what's essential is consistency of the name used in the namespace declaration for files which are mutually dependent.

    A follow-on assumption I had is that -- once I reach more complex scenarios where files from Project "A" residing in a project folder called A would be calling functionality from Project "B" residing in a folder called B -- folder names would be used in order for Project A to find Project B. Since it doesn't appear that this will be the case how will Project A find Project B functionality?

    PS - I'm still having problems inserting images inline despite ensuring blank lines preceding and following the insertion point. I attempted using Edge (I'm a long time Firefox guy) but that didn't seem to make a difference. I belong to a number of hardware and software forums, know how Preview works and have successfully inserted images on those forums using the process required by a given particular forum. Quite frustrating.

    0 comments No comments