This question already has answers here:
CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
(9 answers)
Closed 3 years ago.
I'm still learning to code so if there are some issues, please tell me! This is for a school essay.
I'm writing a code that asks some information about a game: game name, the devs behind it, the publisher, and its cost.
The essay has multiple different tasks, like: make a program that uses get set, arrays, methods, or and so on. I made it all into a program, and I got stuck with the error
CS0120 An object reference is required for the non-static field, method, or property 'Program.gameInfoArray'
The code:
class Program
{
//the class array that stores the games
public gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
bool done = false;
// this is where i get the issue
for (int i = 0; i >= gameInfoArray.Length || done == false; i++)
{
//asks what the game is called
Console.WriteLine("What is the game called:");
string gameName = Console.ReadLine();
//asks who the devolopers are
Console.WriteLine("Who where the devolopers behind the game:");
string devoloper = Console.ReadLine();
//asks who published the game
Console.WriteLine("Who released the game:");
string publisher = Console.ReadLine();
//ask how much the game costs
Console.WriteLine("How much does the game cost:");
string cost = Console.ReadLine();
//inputs the information in to the class array, this is also where i get the issue
gameInfoArray[i].gameInformationGrabber(gameName, devoloper, publisher, cost);
//asks if the
Console.WriteLine("are there any more games? Y/N");
while(true)
{
ConsoleKeyInfo yesOrNo = Console.ReadKey();
if ((yesOrNo.KeyChar == 'Y') || (yesOrNo.KeyChar == 'y'))
{
done = true;
break;
}
else if ((yesOrNo.KeyChar == 'N') || (yesOrNo.KeyChar == 'n'))
{
break;
}
}
}
}
}
The script:
class gameInfo
{
private string gameName;
private string devoloper;
private string publisher;
private string cost;
public void gameInformationGrabber(string game, string dev, string publisher, string cost)
{
theGameName = game;
theDevs = dev;
thePublisher = publisher;
theCost = cost;
}
public string theGameName
{
get { return gameName; }
set { gameName = value; }
}
public string theDevs
{
get { return devoloper; }
set { devoloper = value; }
}
public string thePublisher
{
get { return publisher; }
set { publisher = value; }
}
public string theCost
{
get { return cost; }
set { cost = value; }
}
}
Thanks a ton in advance. Sorry if I messed up big time somewhere in the code.
You've got two comment, it is perfect solution for you.
I hope you spend a time to get a basic concept from it.
However, this will help you.
1. Static only
This is a solution mentioned comment.
class Program
{
// Add static keyword. Because Main() method is static.
// So, every variable inside it should be static.
// public gameInfo[] gameInfoArray = new gameInfo[10];
public static gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
......
}
}
2. Work with local variable
class Program
{
//public gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
// Declare as local variable.
// In static method will now complain using local variable.
gameInfo[] gameInfoArray = new gameInfo[10];
......
}
}
I've tried to run this code, plase don't forget initialize gameInfoArray to prevent null reference exception.
you may need this before entering for loop,
for (int index = 0; index < gameInfoArray.Length; index++)
{
gameInfoArray[index] = new gameInfo();
}
Related
I'm going to get another user and enter which month he wants and find the quarter.
Thought of writing the code inside a class as I need more training on how to use classes.
The program asks whose month it is and I can enter. But now when I type "January" only programs crash.
I assume that it should show which quarter "january" is in
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write a month");
var mc = new MyClass();
mc.month = Convert.ToString(Console.ReadLine());
}
public class MyClass
{
public string month;
public string Prop
{
get
{
return month;
}
set
{ if (Prop == "january")
{
Console.WriteLine("1.quarter");
}
}
}
}
}
}
Consider presenting a menu which in the case below uses a NuGet package Spectre.Console and docs. This gives you an opportunity to work with classes and ensures, in this case input is a valid month along with reties and how to exit.
First a class for the menu.
public class MonthItem
{
public int Index { get; }
public string Name { get; }
public MonthItem(int index, string name)
{
Index = index;
Name = name;
}
public override string ToString() => Name;
}
Class which creates the menu
class MenuOperations
{
public static SelectionPrompt<MonthItem> SelectionPrompt()
{
var menuItemList = Enumerable.Range(1, 12).Select((index) =>
new MonthItem(index, DateTimeFormatInfo.CurrentInfo.GetMonthName(index)))
.ToList();
menuItemList.Add(new MonthItem(-1, "Exit"));
SelectionPrompt<MonthItem> menu = new()
{
HighlightStyle = new Style(Color.Black, Color.White, Decoration.None)
};
menu.Title("[yellow]Select a month[/]");
menu.PageSize = 14;
menu.AddChoices(menuItemList);
return menu;
}
}
Present menu, get selection and show details or exit.
static void Main(string[] args)
{
while (true)
{
Console.Clear();
var menuItem = AnsiConsole.Prompt(MenuOperations.SelectionPrompt());
if (menuItem.Index != -1)
{
AnsiConsole.MarkupLine($"[b]{menuItem.Name}[/] index is [b]{menuItem.Index}[/]");
Console.ReadLine();
}
else
{
return;
}
}
}
I tested your code and your program is not crashing. The program is actually completing and therefore the console is closing due to execution completing. So you can see what I mean try changing your code to this. You will see your code loop and the console will not close. It will also display what the user types in for mc.month
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Write a month");
var mc = new MyClass();
mc.month = Convert.ToString(Console.ReadLine());
Console.WriteLine(mc.month);
}
}
On a side note, not really how I would use a class. You might want to also rethink that. Don't normally see writelines in class.
public Program()
{
amount_bike = new ArrayList();
}
public void push(int value)
{
this.amount_bike.Add(value);
}
public int amount_bike_pop()
{
if (this.amount_bike.Count == 0)
{
return -100000;
}
int lastItem = (int)this.amount_bike[this.amount_bike.Count - 1];
this.amount_bike.RemoveAt(this.amount_bike.Count - 1);
return lastItem;
}
public static void Bike_status()
{
bool exit = false;
Program available = new Program();
available.push(0);
available.push(0);
available.push(50);
WriteLine("E-bike available for rent is : " + available.amount_bike_pop() + " bikes.");
WriteLine("Rented E-bike is : " + available.amount_bike_pop() + " bikes.");
WriteLine("Broke E-bike is : " + available.amount_bike_pop() + " bikes.");
WriteLine("\n");
WriteLine("Please enter a number: 1 is back to pervoius menu or 0 to Exit");
int input = Convert.ToInt32(ReadLine());
while (exit == false)
{
if (input == 1)
{
Clear();
exit = true;
continue;
}
else if (input == 0)
{
Clear();
Exit();
}
else
{
Clear();
Bike_status();
}
}
}
public static void Add_bike()
{
}
I study data structures and Algorithms. In this code, I keep the value in an ArrayList named "available" in the Bike_status method. I need to pass a value in an ArrayList to the Add_bike method. How do I pass a value from one method to another? Actually, I need to pass valus such as 50 to plus some number that I push in Console.ReadLine.
Try to slow down.at starting point of programming sometimes it's confusing.
How do I pass a value from one method to another?
The simple answer is easy ,you want something to use in the function then in your method(s) you create parameter and pass the things you want to it.
like
//edit method to get int value -> public static void Bike_status()
public static void Bike_status(int value)
//edit when to call
else
{
Clear();
Bike_status(input);//send int value in
}
But really, what is that do you really want to learn?
if it OOP? I recommend you study this
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/
To put it simply you has bicycle shop class separate from main program then use the method in that class
e.g.
//bicycle class
public class Bicycles{
public int ID {get;set;}
pulibc string status {get;set; }
public Bicycles(int p_id, string p_status)
{
ID = p_id;
status=p_status;
}
}
//bicycle shop class
public class BicyclesShop{
public List<Bicycle> available {get;set;} // class member
public BicyclesShop() // go search keyword "constructor"
{
available = new List<Bicycle> ();
}
//other method
public void Add_bike()
{
// I can access available object !
// do anything with this class member(s) !
}
public void Bike_status(int inputVal)
{
// do something with inputVal , change some properties?
}
//other methods
public int amount_bike_pop()
{
return available.Count();
}
public int amount_bike_broken_pop()
{
return available.Where(o=>o.status =="Broken").Count(); // go search linq
}
}
//To use in Main program method
BicyclesShop bs =new BicyclesShop();
bs.available.Add( new Bicycle(1 ,"OK") ); //add bicycle #1 in list
bs.available.Add( new Bicycle(2),"Broken" ); //add bicycle #2 in list
WriteLine("Broke E-bike is : " + bs.amount_bike_broken_pop() + " bikes.");
I don't really understand arrays and I need to create a variable of type 'array of songs' then initialize it to a new Array so it can store 4 references to Songs. How would I then create a loop that would run enough times to fill the array whilst calling the InputSOngDetails() method and store the return value in that method?
namespace Songs
{
class Program
{
static void Main(string[] args) {
InputSongDetails();
}
static Song InputSongDetails()
{
Console.WriteLine("What is the name of your song");
string name = Console.ReadLine();
Console.WriteLine("What is the artists name");
string artist = Console.ReadLine();
int records;
Console.WriteLine("How many records did it sell");
while (!int.TryParse(Console.ReadLine(), out records) || records < 0)
{
Console.WriteLine("That is not valid please enter a number");
}
return new Song(name, artist, records);
}
}
}
This is my Songs class if needed
namespace Songs
{
class Song
{
string name;
string artist;
int copiesSold;
public Song(string name, string artist, int copiesSold)
{
this.name = name;
this.artist = artist;
this.copiesSold = copiesSold;
}
public Song()
{
}
public string GetArtist()
{
return artist;
}
public string GetDetails()
{
return $"Name: {name} Artist: {artist} Copies Sold: {copiesSold},";
}
public string GetCertification()
{
if (copiesSold<200000)
{
return null;
}
if (copiesSold<400000)
{
return "Silver";
}
if (copiesSold<600000)
{
return "gold";
}
return "Platinum";
}
}
}
Fist, initialize your array of songs with new Song[ length ], then a simple for-loop will suffice.
static void Main(string[] args)
{
Song[] songs = new Song[4];
for(int i = 0; i < songs.Length; i++)
{
songs[i] = InputSongDetails();
}
}
Or as the commenters suggest, just use a variable-length List<Song>.
static void Main(string[] args)
{
List<Song> songs = new List<Song>();
for(int i = 0; i < 4; i++)
{
songs.Add(InputSongDetails());
}
}
Once you've mastered the basics, you can also accomplish this with a bit of Linq (though I wouldn't actually recommend it in this case):
static void Main(string[] args)
{
var songs = Enumerable.Range(0, 4)
.Select(i => InputSongDetails())
.ToList();
}
This is not really an answer as much as it is a tip for getting input from the user in a console application which might be useful to you (well, the answer is in the last code snippet, but p.s.w.g has already covered that very well).
Since an interactive console session usually ends up with a lot of Console.WriteLine("Ask the user a question"); string input = Console.ReadLine();, and, as you've already done very well, include some validation on the input in some cases, I've found it handy to write the following methods below.
Each of them take in a string, which is the prompt for the user (the question), and return a strongly-typed variable that represents their input. Validation (when needed) is all done in a loop in the method (as you've done):
private static ConsoleKeyInfo GetKeyFromUser(string prompt)
{
Console.Write(prompt);
var key = Console.ReadKey();
Console.WriteLine();
return key;
}
private static string GetStringFromUser(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
public static int GetIntFromUser(string prompt = null)
{
int input;
int row = Console.CursorTop;
int promptLength = prompt?.Length ?? 0;
do
{
Console.SetCursorPosition(0, row);
Console.Write(prompt + new string(' ', Console.WindowWidth - promptLength - 1));
Console.CursorLeft = promptLength;
} while (!int.TryParse(Console.ReadLine(), out input));
return input;
}
With these methods in place, getting input is as simple as:
string name = GetStringFromUser("Enter your name: ");
int age = GetIntFromUser("Enter your age: ");
And it makes writing the method to get a Song from the user that much easier:
private static Song GetSongFromUser()
{
return new Song(
GetStringFromUser("Enter song name: "),
GetStringFromUser("Enter Artist name: "),
GetIntFromUser("Enter number of copies sold: "));
}
So now our main method just looks like (and this is the answer to your question):
private static void Main()
{
var songs = new Song[4];
for (int i = 0; i < songs.Length; i++)
{
songs[i] = GetSongFromUser();
}
Console.WriteLine("\nYou've entered the following songs: ");
foreach (Song song in songs)
{
Console.WriteLine(song.GetDetails());
}
GetKeyFromUser("\nDone! Press any key to exit...");
}
Additionally, here are some suggestions for improving the Song class.
I am having an issue passing properties from one class to another.
I'm getting an error saying that an object reference is required for all of the Game properties in the second class. They are highlighted at the bottom.
this is my first class (Game):
class Game
{
private string verb= "";
private string noun= "";
private string adjective= "";
private string panimal= "";
private string pnoun= "";
public string Verb
{
get {return verb; }
set {verb = value; }
}
public string Noun
{
get {return noun; }
set {noun = value; }
}
public string Adjective
{
get {return adjective;}
set {adjective = value; }
}
public string Panimal
{
get {return panimal; }
set {panimal = value; }
}
public string Pnoun
{
get {return pnoun; }
set {pnoun = value; }
}
public void InScreen()
{
Console.WriteLine("First, give me a past tense VERB: ");
Verb = Console.ReadLine();
Console.WriteLine("\nNow, give me a NOUN: ");
Noun = Console.ReadLine();
Console.WriteLine("\nNext, I will need an ADJECTIVE: ");
Adjective = Console.ReadLine();
Console.WriteLine("\nNow, I will need an ANIMAL(plural): ");
Panimal = Console.ReadLine();
Console.WriteLine("\nFinally, I neeed a plural NOUN: ");
Pnoun = Console.ReadLine();
}
My second class (InsertFunOOUI)
public void Poem()
{
Console.WriteLine("Humpty Dumpty " + **Game.Verb** + "on a " +
**Game.Noun**
);
Console.WriteLine("Humpty Dumpty had a " + **Game.Adjective** + "
fall"
);
}
...you get the picture.
Game is a Type. A class. You need to create an instance of it:
Game g = new Game();
and only then use:
g.Verb
etc.
You need to setup an instance of Game in order to use it, unless it you intended for it to be a static class.
Game game = new Game();
game.InScreen();
Something like that?
First create an instance of your game like #ispiro said. You need to do this once (in the main if possible). Then create an instance of your class poem. Use dependency injection and pass the properties of the game object in as parameters instead of the whole object if possible. I would even create a method inside your Poem class that handles the Console Output. Adapt this code to your liking:
public void main()
{
var game = new Game();
var poem = new Poem();
poem.Output(game.Verb, game.Adjective);
}
public class Poem()
{
public void Output(string verb, string adjective)
{
// your console writeline code
}
}
I am trying to create a console application that converts centimeters to meters
Here are my objectives
Store number of centimeters entered in an attribute
Use a default constructor to store zero in the attribute that stores the number of centimeters meters entered
Use a primary constructor to accept and store number of centimeters entered.
A function call getMeters to return the number of meters
A function called Remainder to get the number of centimeter remanding after conversion
A function called Printout that will display the results
The application should carry on accepting values for conversion until the user decides to end it.
What I have so far:
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
}
//primary const to be added
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Console.WriteLine("Do you want to continue? (y/n)");
//choice = Console.ReadLine();
choice = Console.ReadKey().KeyChar;
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
Printout();
}
while(choice.CompareTo(n) == 0);
}
public static void getMeterst()
{
int meters = c.cmvar / 100;
}
public static void Remainder ()
{
int cmremain = c.cmvar % 100;
}
public static void Printout()
{
Console.WriteLine("{0} Meters and {1} Centimeters", meters, cmremain);
}
}
}
I am getting errors regarding:
prog.cs(24,5): warning CS0168: The variable `meters' is declared but never used
prog.cs(41,11): error CS0103: The name `c' does not exist in the current context
prog.cs(41,2): error CS0103: The name `meters' does not exist in the current context
prog.cs(47,24): error CS0103: The name `c' does not exist in the current context
prog.cs(53,61): error CS0103: The name `meters' does not exist in the current context
prog.cs(53,69): error CS0103: The name `cmremain' does not exist in the current contex
Any help would be appreciated.
In some programming languages, a context is usually defined by { and }, meaning that given this:
{
int a = ...
}
a exists specifically within that block. Assuming that no other variable named a has been declared outside the braces, doing something like so:
{
int a = ...
}
print(a)
Will result in a fault, since a no longer exists.
In your case for instance, you are declaring the following variable: Centimeter c = new Centimeter();. Notice that this is enclosed within the do...while scope, so it exists only in there. Thus, when you try to access your variable from another method, which has its own scope, you get the exception you are getting.
To start solving the issue, you should move the 3 methods getMeterst, Remainder and Printout in their appropriate class, which is Centimeter.
I would recommend you start by looking at some tutorials, since you have other issues with your code.
As pointed out by #user2864740, different languages treat scopes differently. Taking Javascript in consideration:
function hello()
{
{
var i = 44;
}
{
alert(i);
}
}
Yields an alert with the value of 44.
However, the code below does not compile in C#:
private static void Hello()
{
{
int i = 0;
}
{
Console.WriteLine(i); //The name i does not exist in the current context.
}
}
you've got a lot of issues with scope in your code, when you declare something, it can (as a rule) only be accessed inside whatever set of brackets you declare it in, so when you try and access c and cmremainand things like that without specifying their location or accessing them correctly you get errors like this.
I have working code below, but feel free to ask any extra questions as to 'why' this works.
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
}
//primary const to be added
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
Printout(c);
Console.WriteLine("Do you want to continue? (y/n)");
choice = Console.ReadKey().KeyChar;
Console.WriteLine();
}
while (choice != n);
}
public static int getMeters(Centimeter c)
{
int meters = c.cmvar / 100;
return meters;
}
public static int Remainder(Centimeter c)
{
int cmremain = c.cmvar % 100;
return cmremain;
}
public static void Printout(Centimeter c)
{
Console.WriteLine("{0} Meters and {1} Centimeters", getMeters(c), Remainder(c));
}
}
}
you need to learn oriented object programming before programming any oop language.
Basic is good but polute your mind by not thinking object, but sequencial...
Here is your code fixed
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
public Centimeter(int mm)
{
cmvar = mm;
}
public int getMeterst()
{
return cmvar / 100;
}
public int Remainder()
{
return cmvar % 100;
}
public void Printout()
{
Console.WriteLine("{0} Meters and {1} Centimeters", this.getMeterst(), this.Remainder());
}
}
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Console.WriteLine("Do you want to continue? (y/n)");
choice = Console.ReadKey().KeyChar;
Console.WriteLine(); // for pure design needs
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
c.Printout();
}
while (choice != n);
}
}
}