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. Why cant i declare this!

Why cant i declare this!

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
3 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    public const byte[] FOO = {0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF,0x0074, 0x0001, 0x0003,0x0075, 0x0003, 0x0000}; I get a complie error Array initializers can only be used in a variable or field initializer. Try using a new expression instead. And yes i have tried using new, i cant get that either, someone help me plz ?

    J 1 Reply Last reply
    0
    • L Lost User

      public const byte[] FOO = {0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF,0x0074, 0x0001, 0x0003,0x0075, 0x0003, 0x0000}; I get a complie error Array initializers can only be used in a variable or field initializer. Try using a new expression instead. And yes i have tried using new, i cant get that either, someone help me plz ?

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      hmmm, seems like a stupid problem; the compiler should be able to take care of that or so I would think. This does the trick though, hope it helps.

      public static readonly byte [] FOO = new byte[] {
      0x00FF, 0x00FF, 0x00FF,
      0x00FF, 0x00FF,0x0074,
      0x0001, 0x0003,0x0075,
      0x0003, 0x0000
      };

      James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

      P 1 Reply Last reply
      0
      • J James T Johnson

        hmmm, seems like a stupid problem; the compiler should be able to take care of that or so I would think. This does the trick though, hope it helps.

        public static readonly byte [] FOO = new byte[] {
        0x00FF, 0x00FF, 0x00FF,
        0x00FF, 0x00FF,0x0074,
        0x0001, 0x0003,0x0075,
        0x0003, 0x0000
        };

        James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

        P Offline
        P Offline
        Phil Wright 0
        wrote on last edited by
        #3

        You can only use 'const' on simple values that can be computed at compile time. For example, assigning a fixed value. Your code wants to do something more complex which involves creating a new object. Just because we can see it is fixed does not mean the compiler can. Therefore you need to use 'readonly' instead of 'const' to get the same effect. Phil Wright uk_phil_wright@hotmail.com

        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