Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Updating portion of byte array not working

Updating portion of byte array not working

Scheduled Pinned Locked Moved C#
databasedata-structuresquestionannouncement
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Keith Barrow

    You have a try without a catch or finally, so I don't see how your code is compiling. Simplifying your problem:

    private int convertStringToByte(String theString, ref byte theByte)
    {
    theByte = System.Convert.ToByte(theString);
    return 1;
    }

    public void RunSnippet()
    {
        byte\[\] tempByteArr = new byte\[\]{1};
        convertStringToByte("3", ref tempByteArr\[0\]);
        Console.WriteLine(tempByteArr\[0\]);
    }
    

    The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:

    public static int ConvertStringToByte(string s, out byte result)
    {
    try
    {
    theByte = System.Convert.ToByte(theString);

        }
        catch
        {
            return 0;
            //Or whatever - don't catch "all exceptions" like this!
        }
        return 1;
    }
    

    Sort of a cross between Lawrence of Arabia and Dilbert.[^]
    -Or-
    A Dead ringer for Kate Winslett[^]

    M Offline
    M Offline
    MichCl
    wrote on last edited by
    #3

    My code actually has a catch for my try. I just didn't include it here to simplify. So it works for you basically as-is?

    K 1 Reply Last reply
    0
    • K Keith Barrow

      You have a try without a catch or finally, so I don't see how your code is compiling. Simplifying your problem:

      private int convertStringToByte(String theString, ref byte theByte)
      {
      theByte = System.Convert.ToByte(theString);
      return 1;
      }

      public void RunSnippet()
      {
          byte\[\] tempByteArr = new byte\[\]{1};
          convertStringToByte("3", ref tempByteArr\[0\]);
          Console.WriteLine(tempByteArr\[0\]);
      }
      

      The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:

      public static int ConvertStringToByte(string s, out byte result)
      {
      try
      {
      theByte = System.Convert.ToByte(theString);

          }
          catch
          {
              return 0;
              //Or whatever - don't catch "all exceptions" like this!
          }
          return 1;
      }
      

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-
      A Dead ringer for Kate Winslett[^]

      M Offline
      M Offline
      MichCl
      wrote on last edited by
      #4

      I wonder if it's my data. My strings are "00", "55", "20", "00", and "99".

      1 Reply Last reply
      0
      • M MichCl

        My code actually has a catch for my try. I just didn't include it here to simplify. So it works for you basically as-is?

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #5

        It is working. Given what you have said, the problem could well be in the catch. You need to step through your code to determine what is happening exactly, unless someone here come up with a full solution.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-
        A Dead ringer for Kate Winslett[^]

        M 1 Reply Last reply
        0
        • K Keith Barrow

          It is working. Given what you have said, the problem could well be in the catch. You need to step through your code to determine what is happening exactly, unless someone here come up with a full solution.

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
          -Or-
          A Dead ringer for Kate Winslett[^]

          M Offline
          M Offline
          MichCl
          wrote on last edited by
          #6

          Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.

          K L 2 Replies Last reply
          0
          • M MichCl

            Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #7

            Glad you found it!

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-
            A Dead ringer for Kate Winslett[^]

            1 Reply Last reply
            0
            • M MichCl

              Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              Lesson learnt: Never write an empty catch block.

              K M 2 Replies Last reply
              0
              • L Lost User

                Lesson learnt: Never write an empty catch block.

                K Offline
                K Offline
                Keith Barrow
                wrote on last edited by
                #9

                void Get_NameChangedToProtectTheInnocent_()
                {
                try
                {
                ...
                }
                catch
                {
                //Bury the error, like the classic site
                }
                }

                There you go - not empty! Written by yours truly, only 10mins ago :-O . In my defence, it's important that the site doesn't throw errors and this is what the rest of the site does. :~

                Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                -Or-
                A Dead ringer for Kate Winslett[^]

                D 1 Reply Last reply
                0
                • L Lost User

                  Lesson learnt: Never write an empty catch block.

                  M Offline
                  M Offline
                  MichCl
                  wrote on last edited by
                  #10

                  Actually, my catch block had a lot in it. It was just not included in my post!

                  L 1 Reply Last reply
                  0
                  • M MichCl

                    Actually, my catch block had a lot in it. It was just not included in my post!

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    In Visual Studio, You can enable breaking at an exception even when it is caught and handled. This will be very useful during debugging.

                    1 Reply Last reply
                    0
                    • K Keith Barrow

                      void Get_NameChangedToProtectTheInnocent_()
                      {
                      try
                      {
                      ...
                      }
                      catch
                      {
                      //Bury the error, like the classic site
                      }
                      }

                      There you go - not empty! Written by yours truly, only 10mins ago :-O . In my defence, it's important that the site doesn't throw errors and this is what the rest of the site does. :~

                      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                      -Or-
                      A Dead ringer for Kate Winslett[^]

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #12

                      Fair enough, but it should at least be logged for investigation. If it's unimportant and not worth logging then it should be handled in code so the exception never arises.

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      K 1 Reply Last reply
                      0
                      • D DaveyM69

                        Fair enough, but it should at least be logged for investigation. If it's unimportant and not worth logging then it should be handled in code so the exception never arises.

                        Dave
                        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                        K Offline
                        K Offline
                        Keith Barrow
                        wrote on last edited by
                        #13

                        I was being a bit (OK, very) toungue-in-cheek. 99.99% of the time I really wouldn't do this. In this case he exception is very unimportant, for various reasons I'd have to write my own logging infrastructure (which would take longer than the rest of the change, and my code is slated for replacement by an MVC app very soon) and I can't handle in code as the try-block is calling out to an asmx service and I'm guarding against technical errors. I could, I suppose tighten which errors are being caught.

                        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                        -Or-
                        A Dead ringer for Kate Winslett[^]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups