+ 2

Object Oriented programming(is there a better way?)

Hi, Just started learning C# and learning about OOP, by making a cinema app. I am still trying to work my way around using classes and methods in C# but once I created a list inside the ticketrate() method which is to send data to the ‘Public class TicketPrices’ Now I have created another method called PrintOut() and with this method I want to be able to access my data from the TicketPrices class into this. But I cannot output the Prices.SA value? https://code.sololearn.com/cisezqmR4EVp/?ref=app

30th Dec 2022, 8:17 PM
Liam
2 odpowiedzi
+ 3
You can see the fixes i made https://code.sololearn.com/ch0QCiYT8d4D/?ref=app The fixes include: 1. Include theethods into the class braces .. It is not allowed to write a method inside a namespace .. It should be in a class 2. The methods should be public to be accesible when we call them them in main class 3. I changed the return type of the method "TicketRates" to a list. 4. In the main method i get the var list that contains the data returned and i put it in the Print method in a loop 5. I added a parameter "TicketPrice" in the print method
30th Dec 2022, 9:19 PM
Ab SH
+ 3
The biggest problem is how your code is structured. In C# a namespace may contain multiple classes, but it cannot contain any "dangling" methods. All your methods must be part of a class. Also a class can contain fields, which are shared across all the methods of the same class. These fields are typically initialized in the constructor. Once you fix these... we can talk about the other issues related to coding style, but those are less significant and don't block the execution of the program.
30th Dec 2022, 9:20 PM
Tibor Santa
Tibor Santa - avatar