Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# Need help for my homework please
New Posts  All Forums:Forum Nav:

C# Need help for my homework please

post #1 of 23
Thread Starter 
Hello everyone,

I need you help. I have a homework which i should do form application in C#.
So I decided make a calculator. Everything was going well.
But I don't know how to say :
Code:
if (textBox1.Text == null || textBox2.Text == null || textBox1.Text == null && textBox2.Text == null)
{
      MessageBox.Show("x or/and y is/are missing")
}

I mean how can I show that box when user does not enter anything (free blank).
textBox1.Text == null is not working
textBox1.Text == "0" is not working (0 is working but I'm trying to make it when enter free blank)

Please if you know how to do that, help me.
I know that I have poor English xD

--
edit: Added code tags.
post #2 of 23
Quote:
Originally Posted by lonelyguy View Post

Hello everyone,

I need you help. I have a homework which i should do form application in C#.
So I decided make a calculator. Everything was going well.
But I don't know how to say :
if (textBox1.Text == null || textBox2.Text == null || textBox1.Text == null && textBox2.Text == null)
{
MessageBox.Show("x or/and y is/are missing")
}

I mean how can I show that box when user does not enter anything (free blank).
textBox1.Text == null is not working
textBox1.Text == "0" is not working (0 is working but I'm trying to make it when enter free blank)

Please if you know how to do that, help me.
I know that I have poor English xD


http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2


Also, your if statement is wrong or redundant...
IF1 or IF2 or IF1 and IF2

You probably meant:
IF1 or IF2 or (IF1 and IF2)

However, you could just do:
IF1 or IF2
Edited by DuckieHo - 5/14/14 at 10:20am
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #3 of 23
Thread Starter 
I tried IsNullOrEmpty. It gave error. Why does the program give this error?
http://i.hizliresim.com/oQ6DyX.png
post #4 of 23
Quote:
Originally Posted by lonelyguy View Post

I tried IsNullOrEmpty. It gave error. Why does the program give this error?
http://i.hizliresim.com/oQ6DyX.png

Because your code is currently not capable of catching the exception if someone passes a non-decimal value. If someone passed the value of "abc", you code would just crash. The compiler is telling you that is not acceptable.... you need to catch the exception and do something (or nothing) with it.

You need to use Try/Catch to handle exceptions: http://msdn.microsoft.com/en-us/library/0yd65esw.aspx
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #5 of 23
Thread Starter 
Quote:
Originally Posted by DuckieHo View Post

Because your code is currently not capable of catching the exception if someone passes a non-decimal value. If someone passed the value of "abc", you code would just crash. The compiler is telling you that is not acceptable.... you need to catch the exception and do something (or nothing) with it.

You need to use Try/Catch to handle exceptions: http://msdn.microsoft.com/en-us/library/0yd65esw.aspx
Wooow, Try and Catch worked man, thank you so much. You saved my life.
I owe you sir :3
post #6 of 23
I would also attempt to make the same program but without try-catch, especially if you're new to programming. Try to prevent the exception from happening in the first place.

If your input accepts doubles, consider using two fields for example, one for before the decimal, and one after. Then you can easily check that the input is exclusively ints.
Webcrawler
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k ASRock Z75 Pro3 Sapphire 7870 XT Boost Corsair Vengeance, DDR3 1600Mhz 
Hard DriveHard DriveOSMonitor
SpinPoint F1 1TB 64GB M4 SSD Windows 8.1 SyncMaster P2050 
MonitorKeyboardPowerMouse
Dell U2312HM Sidewinder X4 Be Quiet! Pure Power CM L8 430w Zowie FK 
AudioAudio
Xonar DG Sennheiser HD 555 
  hide details  
Reply
Webcrawler
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k ASRock Z75 Pro3 Sapphire 7870 XT Boost Corsair Vengeance, DDR3 1600Mhz 
Hard DriveHard DriveOSMonitor
SpinPoint F1 1TB 64GB M4 SSD Windows 8.1 SyncMaster P2050 
MonitorKeyboardPowerMouse
Dell U2312HM Sidewinder X4 Be Quiet! Pure Power CM L8 430w Zowie FK 
AudioAudio
Xonar DG Sennheiser HD 555 
  hide details  
Reply
post #7 of 23
Thread Starter 
[/quote]
Quote:
Originally Posted by .:hybrid:. View Post

I would also attempt to make the same program but without try-catch, especially if you're new to programming. Try to prevent the exception from happening in the first place.

If your input accepts doubles, consider using two fields for example, one for before the decimal, and one after. Then you can easily check that the input is exclusively ints.

You're right. I will try do that without try-catch, btw yeah, I'm very very new at programming. And I'm very willing to be a real programming.
Do you know stop code? For example I didn't enter first field or second field or both of them. It says you didn't enter first or second field.
Then program is continue. Then messagebox shown up. I wanna make it stop after warning and user can enter two fields again without that messagebox.

If you know what i mean, please tell me. I couldn't find in internet.
post #8 of 23
There isn't really a 'stop-code', your program should 'stop' logically via if statements.

I recommend learning your way around the debugger. Some teachers prefer to leave it until later in the course, but imo you should learn it immediately, as it is the best way to solve your own issues,

I assume you are using Visual Studio?

If you click in the grey bar next to the line numbers, you will see a red dot at that line. Now if you run the program, it will stop at that line.
While the program is running, press ctrl D L, to bring up the locals window.



You should see something similar to this.

So if you are wondering why (textBox1.Text == null) doesn't work, you can see that its default value is actually ""

You can use these buttons to step to the next line of code:


It's important to note that often you will have to step 1 line further then the line you are interested in, because the current line isn't being processed yet.
Edited by .:hybrid:. - 5/14/14 at 12:39pm
Webcrawler
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k ASRock Z75 Pro3 Sapphire 7870 XT Boost Corsair Vengeance, DDR3 1600Mhz 
Hard DriveHard DriveOSMonitor
SpinPoint F1 1TB 64GB M4 SSD Windows 8.1 SyncMaster P2050 
MonitorKeyboardPowerMouse
Dell U2312HM Sidewinder X4 Be Quiet! Pure Power CM L8 430w Zowie FK 
AudioAudio
Xonar DG Sennheiser HD 555 
  hide details  
Reply
Webcrawler
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k ASRock Z75 Pro3 Sapphire 7870 XT Boost Corsair Vengeance, DDR3 1600Mhz 
Hard DriveHard DriveOSMonitor
SpinPoint F1 1TB 64GB M4 SSD Windows 8.1 SyncMaster P2050 
MonitorKeyboardPowerMouse
Dell U2312HM Sidewinder X4 Be Quiet! Pure Power CM L8 430w Zowie FK 
AudioAudio
Xonar DG Sennheiser HD 555 
  hide details  
Reply
post #9 of 23
im a tad lazy to test this out... but could you do.... if ( textbox.text > 0)

Im not sure if it would complain about string to int comparison.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #10 of 23
Quote:
Originally Posted by Mrzev View Post

im a tad lazy to test this out... but could you do.... if ( textbox.text > 0)

Im not sure if it would complain about string to int comparison.

Can't do that in C#.

Use string.IsNullOrEmpty( textbox.Text )
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# Need help for my homework please