10 Replies - 407 Views - Last Post: 24 June 2013 - 07:18 AM Rate Topic: -----

#1 aaronoafc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-June 13

c# Beginners Question - Switch Statement

Posted 23 June 2013 - 06:59 AM

Hi Guys,

Bit of a noob question here. Im currently going through the lynda.com c# course, I've watched a few videos so I thought I would have a crack at my own little console application.

All Im trying to do is a simple "how old are you" then the user puts in their age, then it checks if it is the same as my Age and responds accordingly.

I'm clearly making a rookie error but rather than re-watch the videos (just yet) I thought I would discuss on here.

int myAge = 20;
            
            Console.WriteLine("How old are you?");

            string choice = Console.ReadLine(); // input from the user // 

            switch (myAge) 
            {
                case20:
                Console.WriteLine("You are the same age as me!");
                break;
            
                default:
                Console.WriteLine("You are NOT the same age as me!");
                break;
            
            }


So I've declared the variable myAge = 20;

then I ask the question in console, user input, then I want to look through the simple switch statement (is this the correct terminology, is it a statement) and respond accordingly.

Cheers Guys

never mind guys! Turns out all it was was a space between the case and 20!

Actually I've just seen something else -

I seemed to have mixed 2 apps here. So it works when the myAge is set to 20 however the user input is something completely different.

Anyway i'll look at it! Just wanted to keep anyone updated incase they thought "WTF is he doing" haha

Is This A Good Question/Topic? 0
  • +

Replies To: c# Beginners Question - Switch Statement

#2 aaronoafc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-June 13

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:05 AM

Right now I've cleaned it up a little.

What do you guys think is the best way to do something? I don't want you to necessarily give me the code but possibly how you would do it.

Like in the original post im wanting to ask the user their age - then based on the answer respond with a certain line:

Console.WriteLine("How old are you?");

            inputAge = Console.ReadLine(); // input from the user //


So after that, depending on the input, is it best to use an IF statement?

Thanks
Aaron
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:06 AM

You're missing a space. You're also mixing types.

Where do you actually use choice? Given case 20: (note the space) or case "20": (note the type), you don't need myAge.

I'd recommend avoiding switch in general. An if-then would make more sense for your program.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:09 AM

Notice in line 07 you are switching based on your pre-set variable. It doesn't matter what the user enters as you aren't doing anything with it.

Your age is an int.
The user input is a string.
You need to convert the string to an int so you can compare the two.

Also, a switch construct really isn't the right choice here. A simple if does the job.

If (userage == myage) // This type of logic


But I think you are trying to build a bit early. Work on the learning a bit more. You don't have a big enough vocabulary in this language yet, to write a story.



My standard beginner resources post - Updated Apr 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.



I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

This post has been edited by tlhIn`toq: 23 June 2013 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#5 aaronoafc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-June 13

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:16 AM

Cheers chaps - Think I am just trying to get stuck in too fast!

I'll continue with the videos and hopefully pick up little things here and there. Im making more notes on it too to look back on at a later date.

Thanks
Aaron
Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:25 AM

Quote

Also, a switch construct really isn't the right choice here. A simple if does the job.

A property would be better, if we are talking about OOP concepts, which C# is

Something like this
Public class AgeCheck
{
  private int age;

        public int Age 
        {
            get
            {
                return age;
            } 
            set
            {
                if (value < 18)
                {
                    Console.WriteLine("You are underage");
                    return;
                }
                else
                {
                    age = value;
                }
            } 
        }
}


This post has been edited by Michael26: 23 June 2013 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:38 AM

While I like the idea of a property such as
bool IsSameAge
{
   get
   {
      return myAge == UserAge;
   }
}

I think it's clearly ahead of where the OP is learning at the moment
Was This Post Helpful? 0
  • +
  • -

#8 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:42 AM

I think it would be even better if OP learned OOP concepts before learning OOP language.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 07:50 AM

View PostMichael26, on 23 June 2013 - 08:25 AM, said:

Public class AgeCheck
{
  private int age;

        public int Age 
        {
            get
            {
                return age;
            } 
            set
            {
                if (value < 18)
                {
                    Console.WriteLine("You are underage");
                    return;
                }
                else
                {
                    age = value;
                }
            } 
        }
}



I'm not a fan of the logic here. Its not the job of the Age property to decide if the user is underage. That would be a different property.

bool IsUnderage
{
    get {return UserAge < LegalDrinkingAge;}
}


Its important to remember to stick with a separation of responsibilities and not have side effects. Setting the age shouldn't automatically cause a console.writeline. Its not the job of the property to do that. Its the job of some logic elsewhere to decide if there needs to be a notification or not. The change in age property would raise an event. A subscriber to that would then decide what to do: Console.writeline, update the GUI, ChangeTemplate for the for the drivers license etc.

Properties should not have side effects. Their job is to hold, and sometimes validate values, and notify subscribers of changes if any. That's it.

int age
{
    get{ return _age;}
    set
    {
      if (value == _age) return; // No real change so don't act as if there was
      if (value < 0) value = 0; // Negative age is invalid
      _age = value;
      RaisePropertyChanged("age"); // INotifyPropertyChanged interface handler for subscribers
}


View PostMichael26, on 23 June 2013 - 08:42 AM, said:

I think it would be even better if OP learned OOP concepts before learning OOP language.


It would. But I don't know if properties are necessarily OOP. They aren't objects after all. And some OOP languages don't really have properties: They have getter and setter methods instead.

Besides, unless you are willing to take the OP completely under your wing and be their tutor, you have to accept they they are already on a path with the Lydia videos. You can't just say "There is a better way, don't do what you're doing." Then walk away. If you're going to tell them to take a different route you're pretty much volunteering to be their private tutor.
Was This Post Helpful? 2
  • +
  • -

#10 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: c# Beginners Question - Switch Statement

Posted 23 June 2013 - 08:09 AM

Good info as always :)
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: c# Beginners Question - Switch Statement

Posted 24 June 2013 - 07:18 AM

I'll just throw my agreement with tlhIn`toq here. While you can write code like that, I'd suggest not doing so. Properties shoudn't have "side effects" if you can avoid it. Properties are great for a lot of things, but they shouldn't be used for outputting validation messages. What they really excel at is when you need to take some action when you get or set a value, or if you need to get a value from some other thing, but want it to appear to be like a field.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1