collapse collapse

* Who's Online

  • Dot Guests: 154
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: Exception Handling  (Read 7753 times)

0 Members and 1 Guest are viewing this topic.

Offline topazfae

  • Newbie
  • *
  • Posts: 16
  • Reputation 22
    • View Profile
Exception Handling
« on: July 12, 2012, 06:53:33 pm »
Code: C#
  1.  
  2. using System;
  3.  
  4. using System.Collections.Generic;
  5.  
  6. using System.ComponentModel;
  7.  
  8. using System.Data;
  9.  
  10. using System.Drawing;
  11.  
  12. using System.Linq;
  13.  
  14. using System.Text;
  15.  
  16. using System.Windows.Forms;
  17.  
  18. namespace ExceptionHandlingDemoGUI
  19. {
  20.  
  21.     public partial class Exception : Form
  22.     {
  23.  
  24.         public Exception()
  25.         {
  26.  
  27.             InitializeComponent();
  28.  
  29.         }
  30.  
  31.         private void SubmitButton_Click(object sender, EventArgs e)
  32.         {
  33.  
  34.             int integer;
  35.  
  36.             try
  37.             {
  38.  
  39.                 integer = Convert.ToInt32(Input);
  40.  
  41.             }
  42.  
  43.             catch (FormatException f)
  44.             {
  45.  
  46.                 SuccessFailure.Text = "Input is not a sequence of digits.";
  47.  
  48.             }
  49.  
  50.         }
  51.  
  52.         private void Input_TextChanged(object sender, EventArgs e)
  53.         {
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59. }
  60.  
  61.  

keeps giving me this...

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.
   at System.Convert.ToInt32(Object value)
   at ExceptionHandlingDemoGUI.Exception.SubmitButton_Click(Object sender, EventArgs e) in C:\Users\Owner\Documents\South University\ITS 2105\ExceptionHandlingDemoGUI\Form1.cs:line 38
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.269 (RTMGDR.030319-2600)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
ExceptionHandlingDemoGUI
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Owner/Documents/South%20University/ITS%202105/ExceptionHandlingDemoGUI/bin/Debug/ExceptionHandlingDemoGUI.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.278 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.282 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.269 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.233 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.



« Last Edit: July 12, 2012, 06:55:48 pm by Celestialkey »

Offline Shishka

  • - Secret Permissions Level 1 -
  • Full Member
  • ***
  • Posts: 175
  • Reputation 218
  • Gender: Male
    • View Profile
Re: Exception Handling
« Reply #1 on: July 12, 2012, 06:56:54 pm »
integer = Convert.ToInt32(Input);

Where is "Input" defined?
"Everything will be gone long before me. When the first living thing was born, I was here, waiting. When the last living thing dies, my job is finished. I'll put the chairs on the tables, turn out the lights and lock the universe behind me when I leave."

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Exception Handling
« Reply #2 on: July 12, 2012, 06:56:59 pm »
Quote
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.

You cannot convert type "TextBox" into type "Int32"

Code: C#
  1. integer = Convert.ToInt32(Input);  

Try to get the value into a string, then using atoi, or the CSharp equivalent.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline topazfae

  • Newbie
  • *
  • Posts: 16
  • Reputation 22
    • View Profile
Re: Exception Handling
« Reply #3 on: July 12, 2012, 06:59:02 pm »
integer = Convert.ToInt32(Input);

Where is "Input" defined?

Input is the name of the text box where the integer is entered.

Topazfae

Offline Shishka

  • - Secret Permissions Level 1 -
  • Full Member
  • ***
  • Posts: 175
  • Reputation 218
  • Gender: Male
    • View Profile
Re: Exception Handling
« Reply #4 on: July 12, 2012, 07:01:32 pm »
Ah ok, thats what I thought.

Remember in C# when you want to get or set the text from inside a textbox, or anything like that you can't just call the object, the object has a lot of different things to it. For this case you want the TEXT from the textbox to then convert to an integer. So you would do this..

Code: C#
  1. integer = Convert.ToInt32(Input.Text);  
  2.  
"Everything will be gone long before me. When the first living thing was born, I was here, waiting. When the last living thing dies, my job is finished. I'll put the chairs on the tables, turn out the lights and lock the universe behind me when I leave."

Offline topazfae

  • Newbie
  • *
  • Posts: 16
  • Reputation 22
    • View Profile
Re: Exception Handling
« Reply #5 on: July 12, 2012, 07:02:38 pm »
Quote
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.

You cannot convert type "TextBox" into type "Int32"

Code: C#
  1. integer = Convert.ToInt32(Input);  

Try to get the value into a string, then using atoi, or the CSharp equivalent.

ok not sure I know how to change an entered integer into a string?

Offline Shishka

  • - Secret Permissions Level 1 -
  • Full Member
  • ***
  • Posts: 175
  • Reputation 218
  • Gender: Male
    • View Profile
Re: Exception Handling
« Reply #6 on: July 12, 2012, 07:22:20 pm »
pseudo code

Code: C#
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.  
  10. namespace ExceptionHandlingDemoGUI  
  11. {  
  12.     public partial class Exception : Form  
  13.     {  
  14.         public Exception()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.  
  19.         private void SubmitButton_Click(object sender, EventArgs e)  
  20.         {  
  21.             int integer;
  22.                        
  23.             bool result = Int32.TryParse(Input.Text, out integer);
  24.             if (result)
  25.             {
  26.                                 SuccessFailure.Text = "Converted";        
  27.             }
  28.             else
  29.             {
  30.                SuccessFailure.Text = "Attempted conversion failed";
  31.             }
  32.         }  
  33.  
  34.         private void Input_TextChanged(object sender, EventArgs e)  
  35.         {    
  36.         }  
  37.     }  
  38. }
  39.  


"Everything will be gone long before me. When the first living thing was born, I was here, waiting. When the last living thing dies, my job is finished. I'll put the chairs on the tables, turn out the lights and lock the universe behind me when I leave."

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Exception Handling
« Reply #7 on: July 12, 2012, 08:54:12 pm »
Quote
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.

You cannot convert type "TextBox" into type "Int32"

Code: C#
  1. integer = Convert.ToInt32(Input);  

Try to get the value into a string, then using atoi, or the CSharp equivalent.

ok not sure I know how to change an entered integer into a string?
I think all you were really missing was the following:
integer = Convert.ToInt32(Input.Text);

What Shishka wrote is the .Net way of doing it. The difference:
ToInt32 will throw an exception if it couldn't convert it (and it is up to you to catch it)
TryParse will not and instead returns a bool determining if the conversion was successful
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline Shishka

  • - Secret Permissions Level 1 -
  • Full Member
  • ***
  • Posts: 175
  • Reputation 218
  • Gender: Male
    • View Profile
Re: Exception Handling
« Reply #8 on: July 12, 2012, 09:24:23 pm »
yeah I already gave the .text solution. After reading the assignment I determined that was the wrong way to go with this. That way he had it, it would throw every time it wasn't an integer. He needed a way to do the conversion and check it. So tryparse is the way to go
"Everything will be gone long before me. When the first living thing was born, I was here, waiting. When the last living thing dies, my job is finished. I'll put the chairs on the tables, turn out the lights and lock the universe behind me when I leave."

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]