Thread: Overflow exception unhandles

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question Overflow exception unhandles

    hi all,



    Code:
    private void FocusOver(object sender, System.EventArgs e)
            {
                this.lvItem.SubItems[2].Text = editBox.Text;
                //Insert into registry [IMPORTANT]
                ListViewItem selectedItem = this.lvFiles.SelectedItems[0];
                string nodeFullPath = "";
                string keyName = selectedItem.Text.ToString();
                nodeFullPath = nodePath(this.tvFolders.SelectedNode.FullPath.ToString());
                          
                if (this.lvItem.SubItems[1].Text == "STRING")
                {
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyString(nodeFullPath, keyName, editBox.Text);
                }
                if (this.lvItem.SubItems[1].Text == "INTEGER")
                {
                    int intValue = 0;
                    intValue = (int)Convert.ToInt32(editBox.Text);
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyInteger(nodeFullPath, keyName, intValue);
    
                }
                if (this.lvItem.SubItems[1].Text == "BINARY")
                {
                    byte[] binaryValue = new byte[10];
                   
                   
                    for(int i = 0 ; i <10 ; i++)
                    {
                        binaryValue[i] = Convert.ToByte(editBox.Text)); 
                   
    
                    }
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyBinary(nodeFullPath, keyName, binaryValue);
                    
                } 
                
                InitListView(this.tvFolders.SelectedNode);
                EnumerateHBRegNode(this.tvFolders.SelectedNode);
                this.lvFiles.Refresh();
                editBox.Hide();
               
               
            }
    line which gives overflow exception unhandled is binaryValue[i] = Convert.ToByte(editBox.Text));

    wen i built it and run in MSC#2008 express edition, it shows following error. Also it says cannot implicity convert 'int'. use explicit (did u miss casting?)

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

    ************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Registry.RegMainWin.subMenuModify_Click(Object sender, EventArgs e) in C:\Documents and Settings\leo\Desktop\RegEdit\RegEdit.cs:line 2216
    at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
    at System.Windows.Forms.MenuItem.MenuItemData.Execute ()
    at System.Windows.Forms.Command.Invoke()
    at System.Windows.Forms.Command.DispatchID(Int32 id)
    at System.Windows.Forms.Control.WmCommand(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ListView.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)








    PLS help me with an optimal solution, as am new to c# programming.
    Last edited by leo2008; 11-22-2008 at 06:28 AM.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    You have a loop going through the individual parts of what is a string.

    What if the string isn't 10 characters long? The loop will try and read past the end of the string, which doesn't exist, and there you get an overflow error. Your best bet is to use:

    Code:
    list<byte> binaryValue = new List<byte>();
    
    foreach(byte x in Convert.ToByte(yourText))
    {
      binaryValue.add(x);
    }
    And use a switch instead of a huge list of If statements, much cleaner.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question

    Hi DanFraser


    Thankyou very much for your reply. Will it be an optimal solution if i use switch cases?

    Can i convert string to hexadecimal directly in c sharp?

    Code:
    if (this.lvItem.SubItems[1].Text == "BINARY")
                {
                    byte[] binaryValue = new byte[10];
                   
                   
                    for(int i = 0 ; i <10 ; i++)
                    {
                        binaryValue[i] = Convert.ToByte(editBox.Text)); 
                   
    
                    }
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyBinary(nodeFullPath, keyName, binaryValue);
                    
                }

    Pls edit the code and reply me with the corrected code as i am unable to understand your logic. Pls help me as am a beginner to C# programming.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    when i change the value of string of characters to 5, it still shows

    OverFlow exception was Unhandled
    Value was either too large or too small for an unsigned byte.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    why don't you use try-catch to see what you have? Is the value in the string from 0-255?

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Code:
    if (this.lvItem.SubItems[1].Text == "BINARY")
    {
      List<byte> binaryValue = new List<byte>();
                 
      foreach(byte x in Convert.ToByte(editbox.text))
      {
        binaryValue.Add(x);
      }
      ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
      ManagedRegistry.WriteKeyBinary(nodeFullPath, keyName, binaryValue);
    }
    That's your code as if you took what I wrote earlier and put it in your code.

    Using your code as an example, read this... You are given a word, could be any word, but let's use "coding" as an example. You now separate the word into it's individual letters, so now you have six letters, all represented as a byte : "c" "o" "d" "i" "n" "g". You then try to go through each letter, but you're trying to get ten letters, what happens after you have read the g? Nothing exists! That's why you are getting that error. Generics makes this a little easier, but let's not touch that for now, aside from add "using System.Collections.Generic" at the top of your code. To put it in the short version, you're trying to get more than what you can, stop being greedy
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    if (this.lvItem.SubItems[1].Text == "BINARY")
                {
                    byte[] binaryValue = new byte[editBox.Text.Length];               
                   
                    for(int i = 0 ; i < editBox.Text.Length ; i++)
                    {
                        binaryValue[i] = Convert.ToByte(editBox.Text));                
    
                    }
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyBinary(nodeFullPath, keyName, binaryValue);
                    
                }
    This might work, since you have the right size of array. If you don't wan more than 10 you can add that also at the for loop. Lists<> is clearly better though. Generally, always use List<> instead of array, except if you want something really specific.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Hi danfraser

    i tried the way and modified my code,but it still gives errors :-(

    Code:
    if (this.lvItem.SubItems[1].Text == "BINARY")
                {
                   
                    List<byte> binaryValue = new List<byte>();
    
                    foreach (byte x in Convert.ToByte(editBox.Text))
                    {
                        binaryValue.Add(x);
                    }
    
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyBinary(nodeFullPath, keyName, binaryValue);
                    
                }

    errors while building are as follows:-

    1)The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
    2)foreach statement cannot operate on variables of type 'byte' because 'byte' does not contain a public definition for 'GetEnumerator'


    Please help me resolve the issue.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    yes the value in the string from 0-255

  11. #11
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You need to make sure to include the right references to assemblies, specifically the one that includes List. So Include System.Collections.Generic. You can only use foreach on a class that iimplements IEnumerable or IEnumberable<T>, or on a function that returns an IEnumerable. So you are probably intending to iterate over your list of bytes I would assume.

    Code:
    foreach(byte x in Encoding.ASCII.GetBytes(editbox.Test))
       binaryValue.Add(x);
    Last edited by valaris; 11-24-2008 at 02:29 AM.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question

    the binary data is by default shown as some binary value (for example see the image in the document attached here, it shows binary value as '0'. i need hexadecimal value to be displayed instead of binay value. how can this be done?do i need to convert binary to hexadecimal?is it possible to directly convert string into hexadecimal?

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question

    when i try to modify the binary value\key, it will lead to crash. why is that so?

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question

    Very long string values or binaries should be truncated in the key view pane after 1000 characters or hex values. any idea for an optimal solution?

  15. #15
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    if(Encoding.ASCII.GetByteCount(SomeString) > 1000)
    //do stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM