New Posts  All Forums:Forum Nav:

C# basic program help?

post #1 of 14
Thread Starter 
Hey everyone,

I'm currently working through some C# lecture notes and I just can't get one of the programs to work properly. Been at all day so just need a fresh pair of eyes as I know the answers right in front of me but I just can't see it!

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dayOfTheWeek
{
    public partial class Form1 : Form
    {
        int day = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            day = int.Parse(textBox1.Text);
            
            if (day == 1)
            {
                label1.Text = "The day is Monday!";
            }
                   
            if (day == 2)
            {
                 label1.Text = "The day is Tuesday!";
            }

            if (day == 3)
            {
                 label1.Text = "The day is Wednesday!";
            }

            if (day == 4)
            {
                 label1.Text = "The day is Thursday!";
            }
            
            if (day == 5)
            {
                 label1.Text = "The day is Friday!";
            }

            if (day == 6)
            {
                 label1.Text = "The day is Saturday!";
            }
            
            if (day == 7)
            {
                label1.Text = "The day is Sunday!";
            }               
            else
            {
                label1.Text = "Mad day!";
            }
        }

    }
}
That's the bit of the code I'm currently stuck with.

Ever number I input gives me "Mad day".

So what have I done wrong?
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 14
if you put a breakpoint what value do the variable day then get?

Maybe try to do (int32)textbox.text instead of the int parse, not sure it works
post #3 of 14
Thread Starter 
sorry I'm quite new to programming especially C# how would I go about putting in a break point? I only know what a break point is because I just wiki'd it right now lol
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #4 of 14
Use elseif or switch/case.

The problem that you're having here is that it's evaluating each if statement separately. When the execution reaches the bottom if, it will only look at the if and the else. If it's not 7, then it'll just run the else path and set it to Mad Day.

Your code should be

if == 1
else if == 2
else if == 3
else if == 4
else if == 5
else if == 6
else if == 7
else

OR

switch (day)

case 1:
//stuff
case 2:
//stuff
case 3:
//stuff
case 4:
//stuff
//case 5 to 7
default:
text = "Mad day";

etc.
Edited by RallyMaster - 10/16/11 at 1:47pm
Malignance
(24 items)
 
  
CPUMotherboardGraphicsRAM
i5-2500K Asus P8Z68-V eVGA GTX670 FTW 16GB Mushkin DDR3-2100 1.5V 
Hard DriveHard DriveHard DriveOptical Drive
2 x 128GB Crucial M4 2 x 1TB Samsung F3 RAID0 1.5TB Seagate Asus 22X DVDRW 
CoolingOSMonitorMonitor
Noctua NH-D14 Windows 7 Ultimate 64-bit Dell Ultrasharp U2410 Asus ProArt PA248Q 
KeyboardPowerCaseMouse
Rosewill RK-9000BR PCP&P Silencer MKIII 600W Lian Li PC-A05NB Logitech G500 
Mouse PadAudioAudioAudio
Rocketfish Dual Sided Asus Xonar Essence STX Infinity Primus P153 | Dayton DTA-120 Amplifier... Shure SRH840 
AudioAudioAudio
Audio Technica ATH-AD700X Objective DAC Audio-Technica ATR2500-USB 
  hide details  
Reply
Malignance
(24 items)
 
  
CPUMotherboardGraphicsRAM
i5-2500K Asus P8Z68-V eVGA GTX670 FTW 16GB Mushkin DDR3-2100 1.5V 
Hard DriveHard DriveHard DriveOptical Drive
2 x 128GB Crucial M4 2 x 1TB Samsung F3 RAID0 1.5TB Seagate Asus 22X DVDRW 
CoolingOSMonitorMonitor
Noctua NH-D14 Windows 7 Ultimate 64-bit Dell Ultrasharp U2410 Asus ProArt PA248Q 
KeyboardPowerCaseMouse
Rosewill RK-9000BR PCP&P Silencer MKIII 600W Lian Li PC-A05NB Logitech G500 
Mouse PadAudioAudioAudio
Rocketfish Dual Sided Asus Xonar Essence STX Infinity Primus P153 | Dayton DTA-120 Amplifier... Shure SRH840 
AudioAudioAudio
Audio Technica ATH-AD700X Objective DAC Audio-Technica ATR2500-USB 
  hide details  
Reply
post #5 of 14
Quote:
Originally Posted by kidshenlong View Post
sorry I'm quite new to programming especially C# how would I go about putting in a break point? I only know what a break point is because I just wiki'd it right now lol
Right the left of your code you can click which will leave a red circle, if you then do a debug run (F5) then when the code you put the breakpoint at will stop your app and you can see what all your different variables has of values

post #6 of 14
Rally is spot-on.
Code:
        
day = int.Parse(textBox1.Text);             
if (day == 1) label1.Text = "The day is Monday!";
else if (day == 2) label1.Text = "The day is Tuesday!";
else if (day == 3) label1.Text = "The day is Wednesday!";
else if (day == 4) label1.Text = "The day is Thursday!";
else if (day == 5) label1.Text = "The day is Friday!";
else if (day == 6) label1.Text = "The day is Saturday!"; 
else if (day == 7) label1.Text = "The day is Sunday!";
else if { label1.Text = "Mad day!"; }
Edit - He might not be using Visual Studio, so why do you assume he is?
Edited by csm725 - 10/16/11 at 1:52pm
Turbulence
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5-2500k 4.4GHz Asus P8P67 Pro 3.1 HIS HD 7870 2GB G.SKILL Ripjaws X 16GB 1600MHz 
Hard DriveHard DriveOptical DriveCooling
Crucial m4 128GB Samsung Spinpoint F3 1TB Asus DRW-24B1ST Noctua NH-D14 
CoolingOSMonitorKeyboard
Accelero Twin Turbo II Windows 7 Professional N Dell U2311H Realforce 87U White 55cN 
PowerCaseMouseMouse Pad
Corsair TX650V2 Fractal Design R3 Black Pearl Razer DeathAdder SteelSeries 4HD 
AudioAudioAudio
Audinst HUD-mx1 Beyerdynamic DT990 Premium 600 Ω Recabled M-Audio AV40 
  hide details  
Reply
Turbulence
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5-2500k 4.4GHz Asus P8P67 Pro 3.1 HIS HD 7870 2GB G.SKILL Ripjaws X 16GB 1600MHz 
Hard DriveHard DriveOptical DriveCooling
Crucial m4 128GB Samsung Spinpoint F3 1TB Asus DRW-24B1ST Noctua NH-D14 
CoolingOSMonitorKeyboard
Accelero Twin Turbo II Windows 7 Professional N Dell U2311H Realforce 87U White 55cN 
PowerCaseMouseMouse Pad
Corsair TX650V2 Fractal Design R3 Black Pearl Razer DeathAdder SteelSeries 4HD 
AudioAudioAudio
Audinst HUD-mx1 Beyerdynamic DT990 Premium 600 Ω Recabled M-Audio AV40 
  hide details  
Reply
post #7 of 14
Thread Starter 
Just found the solution! I feel like such an idiot lol forgot to use else if for all the other conditions before the last and after the first.
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #8 of 14
Thread Starter 
Thanks for the help guys. Would've used a switch statement but the exercise specifically asked for the use of if statements. Thanks for the help anyway... I imagine I'll be posting a lot more over the next few weeks Rep +
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #9 of 14
remove
my system
(13 items)
 
  
CPUMotherboardGraphicsRAM
intel q9450 @ 3.72 ghz evga 750i a1 ftw two evga superclocked gtx 470's in sli gskill 4gb ddr2 1200 5-5-5-15-2T 
Hard DriveOptical DriveOSMonitor
2x western digital black 1tb raid 0 random dvd-rw/cd-rw 22x which somehow uses ata windows 7 64 + linux ubuntu spectre 24inch 
PowerCase
silverstone op1000-e (1000 watts) Silverstone Raven 2 
  hide details  
Reply
my system
(13 items)
 
  
CPUMotherboardGraphicsRAM
intel q9450 @ 3.72 ghz evga 750i a1 ftw two evga superclocked gtx 470's in sli gskill 4gb ddr2 1200 5-5-5-15-2T 
Hard DriveOptical DriveOSMonitor
2x western digital black 1tb raid 0 random dvd-rw/cd-rw 22x which somehow uses ata windows 7 64 + linux ubuntu spectre 24inch 
PowerCase
silverstone op1000-e (1000 watts) Silverstone Raven 2 
  hide details  
Reply
post #10 of 14
Thread Starter 
Quote:
Originally Posted by Mech0z View Post
Right the left of your code you can click which will leave a red circle, if you then do a debug run (F5) then when the code you put the breakpoint at will stop your app and you can see what all your different variables has of values

http://www.youtube.com/watch?v=0-TS8SDUmMY
Thanks for that. Trust youtube to explain everything
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming