Setting a boolean value in a class

How would I set a boolean value from the main program if the boolean is in a class example being bool CanCast = true/false?
jschmuffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SolutionsCSCommented:
Implement a property a follow.
class Sample {
  // private member
  private bool mCanCast = false;

  bool CanCast
 {
    get
    {
       return mCanCast;
    }
   set
   {
     mCanCast = value;
   }
 }
0
jschmuffAuthor Commented:
what would the main program look like that the user sets it to?
0
SolutionsCSCommented:
// Instantiate the class.
Sample Instance;
Get the user input from dialog or any other way you prefer.
Set the value  this way.

Instance.CanCast = ValueFromTheUser;

Also you could prefer to expose it from the class as a shared member in wich case you would not have to instantiate the class to access it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jschmuffAuthor Commented:
im a little confused since it is either a true or false setting? can you explain a little more
0
SolutionsCSCommented:
Ok, maybe i do not get it...

If the main program mean put a setting in app.cfg so you would access this property with
Instance.CanCast = properties.settings.default.YourSetting.

If from the main program mean get the value from the user you would do as preceeding.

If this do not answer your question.  Please give more detail about it please.
0
galar71Commented:
His variable ValueFromTheUser is going to be a bool, so it can only be true or false.

Essentially the same as either:

Instance.CanCast = true;

or...

Instance.CanCast = false;

depending on the value of the ValueFromTheUser variable.
0
SolutionsCSCommented:
That's it.  Very simple up to that point.
0
jschmuffAuthor Commented:
so I would accept normal user input whatever I want maybe throw in a if(answer == "true") { Instance.CanCast = true;}
0
SolutionsCSCommented:
exact;
0
jschmuffAuthor Commented:
I get an error:

Program.cs(65,17): error CS0029: Cannot implicitly convert type 'string' to 'bool'

this is why I typed for it


            Console.Write("Will you be a healer?(true or false): ");
            answer = Console.ReadLine();
            if (answer = "true")
            {
                character.Heal = true;
            }
            else
                character.Heal = false;

Open in new window

0
SolutionsCSCommented:
use == for the comparison
0
SolutionsCSCommented:
if(answer == "true")  or
for a safer way use answer.toupper() == "TRUE", Like this if the user input True or tRue, it will work
0
galar71Commented:
You also have to make sure that your "answer" variable is of a string type.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.