User input question [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

User input question [SOLVED]

Post by mattykins »

I started working on the farm sim game I mentioned in a previous post, and I have encountered a problem.
So i want to call the class CropLocations.(user input defines what method to call).

The code I have for that right now is

Code: Select all

 int typeofcrop = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Where would you like to plant it?");
            string locationofplant = Convert.ToString(Console.ReadLine());
            CropLocations.(locationofplant) = typeofcrop;
However it gives me an error, "Error Identifier expected"

They have already entered the variable type of crop.



Thanks in advanced! :)

EDIT: I was thinking of doing if statements for every single one of the crop locations..but that would be over 80 if statements because of how big my farm-grid is. I would do the if statement way, but somethings telling me there is a MUCH easier way..haha :
Last edited by mattykins on Fri Jan 20, 2012 6:03 am, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: User input question

Post by Jackolantern »

Provided this is C#, there is a problem here:

Code: Select all

CropLocations.(locationofplant) = typeofcrop;
This is not correct syntax. Maybe you meant to reference a property or class field?

Code: Select all

CropLocations.locationofplant = typeofcrop;
Or maybe you meant a method call?

Code: Select all

CropLocations.locationofplant(typeofcrop);
If you are unsure, post the definition of the class that "CropLocations" is an object of.

EDIT: Oh, and about the IF statements, there probably is an easier way, but without knowing more about your application, I cannot be sure what to suggest. Although probably a method would be the best way to do it. You could make a method that accepts the grid number (or a reference of a grid object) as one of its arguments, and then the other arguments would be what you want to set. Then you just set up a FOR loop that loops through all of the grid cells and calls the method for each one, sending in the iteration variable (the "x" or "i" variable, etc.) to make sure you get the correct cell. For example, maybe it could be like this:

Code: Select all

for (int x = 0; x < 80; x++) {
    gameGrid.setCell(x, cellStatus, cellContents);
}
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: User input question

Post by mattykins »

Ah, yeah I guess I wasn't very clear in my question :P.
So here is what I want to do, I have a class called CropLocations. Which basically holds the information for what crops are planted where. And I want user input to define what method in that class to call and what to set it to.

For example if the user wants to plant something in slot a1
I want the program to say

Code: Select all

CropLocations.a1 = typeofcrop;
My CropLocations class looks similar to

Code: Select all

        static private int A1;
        static private int A2;
        static private int A3;
       // .....More static private int
        static private int F14;

        static public int a1
        {
            get { return A1; }
            set { A1 = value; }
        }

        //Same method but repeated for different crop locations.

User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: User input question

Post by Jackolantern »

Yes, then the version you just mentioned and the first option I gave on how to fix it will work. But you would probably want to make an all-purpose method to set each cell so you can iterate through each cell in a FOR loop. 80 IF statements would be an absolute nightmare to maintain or change later if needed. Get rid of the properties, and write it into one method that accepts the value to store in the cell, and the cell number itself.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: User input question

Post by mattykins »

How would you recommend doing that? Like i said, I am still really new to C#.

EDIT: I still get an error even with the first one you mentioned

Code: Select all

Console.Clear();
            Console.WriteLine("What kind of crop would you like to plant?");
            Console.WriteLine("1 = potatoe, 2 = lettuce, 3 = corn, 4 = tomatoe, 5 = carrot");
            int typeofcrop = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Where would you like to plant it?");
            string locationofplant = Convert.ToString(Console.ReadLine());
            CropLocations.locationofplant = typeofcrop;
Error 1 'FarmingSim.CropLocations' does not contain a definition for 'locationofplant'
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: User input question

Post by Jackolantern »

There is an error here:

Code: Select all

string locationofplant = Convert.ToString(Console.ReadLine());
            CropLocations.locationofplant = typeofcrop;
You store a string, and then you try to access that string inside the CropLocations object (that is what the "." operator does: it looks for that item inside of the object variable). If you have a method (a function inside of the class) called "locationofplant" inside your CropLocations class, you could do it like this:

Code: Select all

string typeofcrop = Convert.ToString(Console.ReadLine());
            CropLocations.locationofplant = typeofcrop;
As far as writing the general-use method, I would need to know what you were going to be doing inside of those 80 IF statements you were talking about before.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: User input question

Post by mattykins »

Hm I still don't think you understand my question, it is most likely my fault because I'm not clear enough :)

But about those 80 if statements, I was gonna have it check like

Code: Select all

if(locationofplant = a1)
{
croplocations.a1
}

if(locationofplant = a2) 
{
croplocations.a2
}

To see which method to call. And my question is: I want user input to determine what method to call inside of my CropLocations class.
Like if the user inputs a1, I want the program to call Croplocations.a1, and set it to the crop they want to plant.
CropLocations.a1 looks like

Code: Select all

static private int A1

static public int a1
{
get { return A1; }
set {A1 = value; }
}
So if the user wants to plant corn on a1, I want it to set a1 inside of the CropLocations class to 4(or whatever number i use to represent corn)

I hope that is more clear then what I had said before lol :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: User input question

Post by Jackolantern »

This code doesn't do anything:

Code: Select all

if(locationofplant = a1)
{
croplocations.a1
}

if(locationofplant = a2) 
{
croplocations.a2
}
(ignore this paragraph now)You can't call methods dynamically without using reflection, which is something you definitely should not use here. Outside of reflection, all method calls are static, or the proper term is "early-binding". You cannot take input and make it dynamically call a method at runtime ("late-binding"), because method calls are resolved at compile-time (again, outside of reflection).

You need one method that can take any of the cell numbers and any of the values that need to be stored in those cell values. That is the way this problem needs to be solved :)

EDIT: Oops! I see what you were trying to do, which was take the value input and call a method based on that value. I am tired and for some reason I thought you meant dynamically calling methods at runtime.

Still though, this problem needs to be solved with 1 method. If you do it like this, with all of the IF statements and separate methods, you are creating massive amounts of duplicate code which will be extremely difficult to maintain. Not duplicating code is a prime tenant of programming, called DRY ("Don't Repeat Yourself").

Your cells need to be stored in an array or other collection, not as separate properties or class fields, so that you can programmatically access them. Change the cells to an array stored within the CropLocations class. Then you will be able to make 1 method that can set anything you need for all of the cells! :)
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: User input question

Post by mattykins »

That is so much more simple! I never thought of using an array to handle all that. *facepalm* *facepalm* *facepalm*. Hm, I know how to code arrays in BASIC but not in C#. I will have to look into that. But i'm tired and dealing with a sprained wrist, so i'll have to look into that tomorrow. Thanks so much man :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: User input question

Post by Jackolantern »

Its ok! We all start somewhere. I would never, ever show my first DarkBASIC project code to anywhere here LOL. It makes me embarrassed just thinking about it!

EDIT: Oh, and definitely read up a bit on arrays. There is too much to know about them for me to tell you enough here. But if you have any questions afterwards, let me know!
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”