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. Generics Problem Question

Generics Problem Question

Scheduled Pinned Locked Moved C#
helpquestioncsharpvisual-studiodata-structures
6 Posts 2 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.
  • P Offline
    P Offline
    PaulPrice
    wrote on last edited by
    #1

    Good afternoon gentlemen, I was hoping somebody could explain what is wrong with the following implementation of a simple Generic function protected void SetValue<>(T value, int length) { byte[] data = BitConverter.GetBytes(value); if (SrbData.Length < (length + data.Length)) throw new InvalidOperationException(); Array.Copy(data, 0, SrbData, length, data.Length); } ps, the <> is because I could not work out how to get a single greater than to display and SrbData is a byte array The Visual Studio error is Error 1 The best overloaded method match for 'System.BitConverter.GetBytes(bool)' has some invalid arguments C:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Comet\SrbLibrary\SrbHeader.cs 17 27 SrbLibrary I do not have much experience with Generics so this is most likely a silly problem, but thanks for the help anyway Paul

    G 1 Reply Last reply
    0
    • P PaulPrice

      Good afternoon gentlemen, I was hoping somebody could explain what is wrong with the following implementation of a simple Generic function protected void SetValue<>(T value, int length) { byte[] data = BitConverter.GetBytes(value); if (SrbData.Length < (length + data.Length)) throw new InvalidOperationException(); Array.Copy(data, 0, SrbData, length, data.Length); } ps, the <> is because I could not work out how to get a single greater than to display and SrbData is a byte array The Visual Studio error is Error 1 The best overloaded method match for 'System.BitConverter.GetBytes(bool)' has some invalid arguments C:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Comet\SrbLibrary\SrbHeader.cs 17 27 SrbLibrary I do not have much experience with Generics so this is most likely a silly problem, but thanks for the help anyway Paul

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      BitConverter.GetBytes() accepts only several types. It seams that the type that you are substituting T with cannot be expected by BitConverter.GetBytes(). Msdn has more information about this function

      #region signature my articles #endregion

      P 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        BitConverter.GetBytes() accepts only several types. It seams that the type that you are substituting T with cannot be expected by BitConverter.GetBytes(). Msdn has more information about this function

        #region signature my articles #endregion

        P Offline
        P Offline
        PaulPrice
        wrote on last edited by
        #3

        Thanks for the response However this I understand, but I was hoping to overcome the need to duplicate a function for each type that I need to add to the bit array. Hence the attempt with Generics. My understanding is that the type T will become what ever T i pass in to it! Is the problem because the base function will only accept 9 types and generics potentially means that far more types could be passed in??

        Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh

        G 1 Reply Last reply
        0
        • P PaulPrice

          Thanks for the response However this I understand, but I was hoping to overcome the need to duplicate a function for each type that I need to add to the bit array. Hence the attempt with Generics. My understanding is that the type T will become what ever T i pass in to it! Is the problem because the base function will only accept 9 types and generics potentially means that far more types could be passed in??

          Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          Yes, T will become the type that you pass but GetBytes can accept only 9 types. So either the type of T should be one of them or you should write a new class like that acts like bitconverter and has a function called GetBytes that accepts many types but that means quite a lot of work

          #region signature my articles #endregion

          P 1 Reply Last reply
          0
          • G Giorgi Dalakishvili

            Yes, T will become the type that you pass but GetBytes can accept only 9 types. So either the type of T should be one of them or you should write a new class like that acts like bitconverter and has a function called GetBytes that accepts many types but that means quite a lot of work

            #region signature my articles #endregion

            P Offline
            P Offline
            PaulPrice
            wrote on last edited by
            #5

            Is it possible to limit the types that T can become??

            Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh

            G 1 Reply Last reply
            0
            • P PaulPrice

              Is it possible to limit the types that T can become??

              Ta Paul Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh

              G Offline
              G Offline
              Giorgi Dalakishvili
              wrote on last edited by
              #6

              Yes you can by adding the where clause and specifying the restriction you want to apply but I'm not sure if you can limit T to only those nine types. For the constraint that you can apply have a look at this: Types of generic constraints[^]

              #region signature my articles #endregion

              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