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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Adding two types into an array?

Adding two types into an array?

Scheduled Pinned Locked Moved C#
questionwindows-adminlinuxdata-structureshelp
13 Posts 5 Posters 1 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.
  • B bbranded

    Hello, I'm attempting to enumerate the contents of a registry key: HKCU\Software\Microsoft\Windows\Shell\Bags\1\Desktop I am currently storing the Name in an array without a problem. However, how do I store both a REG_BINARY (byte) and a REG_DWORD (int) into an array? Here is the code, that is failing: RegistryKey myRegKey = Registry.CurrentUser; myRegKey = myRegKey.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Bags\\1\\Desktop\\"); String[] valueNames = myRegKey.GetValueNames(); string[] OriginalValueName = new string[valueNames.Length]; Byte[] OriginalValueData = new System.Byte[valueNames.Length]; int i = 0; foreach (String valuename in valueNames) { MessageBox.Show(valuename); OriginalValueName[i] = valuename; Object ovalue = myRegKey.GetValue(valuename); MessageBox.Show(ovalue.ToString()); if (ovalue.ToString() == "System.Byte[]") { OriginalValueData[i] = (System.Byte)ovalue; } else { OriginalValueData[i] = (System.Byte)ovalue; //Specified cast is invalid } } I'm trying to convert an integer to a byte, but it doesn't seem to work, claiming "Specified cast is invalid." Does anyone have any ideas? Thanks, Matt

    S Offline
    S Offline
    Samuel Cherinet
    wrote on last edited by
    #3

    try using ArrayList in System.Collection namespace you don't even have to check for a type because this collection stores them as objects for your case ArrayList OriginalValueData=new ArrayList(); for(...) { OriginalValueData.Add(OValue); } I hope that would solve ur probles Good Luck

    1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, how could you store an int value (32-bit) in a byte (8-bit) in a useful manner? You could: 1. do the opposite, i.e. store bytes and ints into an int[]. 2. better yet, define your own type ("RegistryValue") and derive two types from it ("RegistryByte" and "RegistryInt"), then store instances of either one of them in an array, or better yet a List< RegistryValue>. Making it a class/struct allows for additional information such as the key name. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      L Offline
      L Offline
      led mike
      wrote on last edited by
      #4

      Luc Pattyn wrote:

      how could you store an int value (32-bit) in a byte (8-bit) in a useful manner?

      By using the Obi Wan Kenobi Bit Cramer[^] It's that pretty blue thing he is holding. ;P

      L 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, how could you store an int value (32-bit) in a byte (8-bit) in a useful manner? You could: 1. do the opposite, i.e. store bytes and ints into an int[]. 2. better yet, define your own type ("RegistryValue") and derive two types from it ("RegistryByte" and "RegistryInt"), then store instances of either one of them in an array, or better yet a List< RegistryValue>. Making it a class/struct allows for additional information such as the key name. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        B Offline
        B Offline
        bbranded
        wrote on last edited by
        #5

        Ahh.... i see... Sorry, I'm new. Every post I post here is like an awakening. Appreciated, Matt

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, how could you store an int value (32-bit) in a byte (8-bit) in a useful manner? You could: 1. do the opposite, i.e. store bytes and ints into an int[]. 2. better yet, define your own type ("RegistryValue") and derive two types from it ("RegistryByte" and "RegistryInt"), then store instances of either one of them in an array, or better yet a List< RegistryValue>. Making it a class/struct allows for additional information such as the key name. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          B Offline
          B Offline
          bbranded
          wrote on last edited by
          #6

          Ahhh... much better... dang datatypes. RegistryKey myRegKey = Registry.CurrentUser; myRegKey = myRegKey.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Bags\\1\\Desktop\\"); String[] valueNames = myRegKey.GetValueNames(); string[] OriginalValueName = new string[valueNames.Length]; int[] OriginalValueData = new int[valueNames.Length]; int i = 0; foreach (String valuename in valueNames) { MessageBox.Show(valuename); OriginalValueName[i] = valuename; Object ovalue = myRegKey.GetValue(valuename); MessageBox.Show(ovalue.ToString()); OriginalValueData[i] = (int)ovalue; }

          L 1 Reply Last reply
          0
          • L led mike

            Luc Pattyn wrote:

            how could you store an int value (32-bit) in a byte (8-bit) in a useful manner?

            By using the Obi Wan Kenobi Bit Cramer[^] It's that pretty blue thing he is holding. ;P

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #7

            Extreme tools should be used in emergencies only; don't waste them on a couple of bits, spending some RAM is more than adequate. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            1 Reply Last reply
            0
            • B bbranded

              Ahhh... much better... dang datatypes. RegistryKey myRegKey = Registry.CurrentUser; myRegKey = myRegKey.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Bags\\1\\Desktop\\"); String[] valueNames = myRegKey.GetValueNames(); string[] OriginalValueName = new string[valueNames.Length]; int[] OriginalValueData = new int[valueNames.Length]; int i = 0; foreach (String valuename in valueNames) { MessageBox.Show(valuename); OriginalValueName[i] = valuename; Object ovalue = myRegKey.GetValue(valuename); MessageBox.Show(ovalue.ToString()); OriginalValueData[i] = (int)ovalue; }

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #8

              Hi, int may be sufficient, it depends on what you want to do with the data afterwards. One potential problem is you don't store the original type information, so a key (Paul, 123) may have been a REG_BINARY or a REG_DWORD, so you would not be able to restore registry entries lacking some information. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              B 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, int may be sufficient, it depends on what you want to do with the data afterwards. One potential problem is you don't store the original type information, so a key (Paul, 123) may have been a REG_BINARY or a REG_DWORD, so you would not be able to restore registry entries lacking some information. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                B Offline
                B Offline
                bbranded
                wrote on last edited by
                #9

                Ahh okay... I'll store that as well. Can I store different data types per-dimension in a multi-dimensional array?

                L 1 Reply Last reply
                0
                • B bbranded

                  Ahh okay... I'll store that as well. Can I store different data types per-dimension in a multi-dimensional array?

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  bbranded wrote:

                  Can I store different data types per-dimension in a multi-dimensional array?

                  No. (i.e. not in the simple way you probably are thinking of; you can as structs/class objects, but then you wouldn't need extra dimensions any more). Object oriented principles would tell you to store all information about one real-life object (such as a registry key) into one software object, that is why I suggested you make your own type (struct or class) in the first place. FWIW: It may be wise to buy and study a book on C#, that would give you a head start by teaching you all the relevant principles, explaining them well and illustrating them with examples. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  B 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    bbranded wrote:

                    Can I store different data types per-dimension in a multi-dimensional array?

                    No. (i.e. not in the simple way you probably are thinking of; you can as structs/class objects, but then you wouldn't need extra dimensions any more). Object oriented principles would tell you to store all information about one real-life object (such as a registry key) into one software object, that is why I suggested you make your own type (struct or class) in the first place. FWIW: It may be wise to buy and study a book on C#, that would give you a head start by teaching you all the relevant principles, explaining them well and illustrating them with examples. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    B Offline
                    B Offline
                    bbranded
                    wrote on last edited by
                    #11

                    Thanks very much for all of the info. People keep telling me that I need basics.... hah! Maybe one day... The data type/struct thing seems very interesting to me, and makes perfect sense. I'm always getting in over my head... my primary job (and my skill set) is a network and sys admin. I code on the side, and I feel like I'm slowly slipping into coding more... maybe I should buy a book. Thanks! :-D

                    L 1 Reply Last reply
                    0
                    • B bbranded

                      Thanks very much for all of the info. People keep telling me that I need basics.... hah! Maybe one day... The data type/struct thing seems very interesting to me, and makes perfect sense. I'm always getting in over my head... my primary job (and my skill set) is a network and sys admin. I code on the side, and I feel like I'm slowly slipping into coding more... maybe I should buy a book. Thanks! :-D

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      You're welcome. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                      1 Reply Last reply
                      0
                      • B bbranded

                        Hello, I'm attempting to enumerate the contents of a registry key: HKCU\Software\Microsoft\Windows\Shell\Bags\1\Desktop I am currently storing the Name in an array without a problem. However, how do I store both a REG_BINARY (byte) and a REG_DWORD (int) into an array? Here is the code, that is failing: RegistryKey myRegKey = Registry.CurrentUser; myRegKey = myRegKey.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Bags\\1\\Desktop\\"); String[] valueNames = myRegKey.GetValueNames(); string[] OriginalValueName = new string[valueNames.Length]; Byte[] OriginalValueData = new System.Byte[valueNames.Length]; int i = 0; foreach (String valuename in valueNames) { MessageBox.Show(valuename); OriginalValueName[i] = valuename; Object ovalue = myRegKey.GetValue(valuename); MessageBox.Show(ovalue.ToString()); if (ovalue.ToString() == "System.Byte[]") { OriginalValueData[i] = (System.Byte)ovalue; } else { OriginalValueData[i] = (System.Byte)ovalue; //Specified cast is invalid } } I'm trying to convert an integer to a byte, but it doesn't seem to work, claiming "Specified cast is invalid." Does anyone have any ideas? Thanks, Matt

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #13

                        Or use an object array.

                        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