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. which is best option?

which is best option?

Scheduled Pinned Locked Moved C#
question
4 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.
  • T Offline
    T Offline
    Try
    wrote on last edited by
    #1

    I have 10 different classes in an application. I need to access a constant (int value = 125;) in all ten classes. Which is better way of following two options? 1. Declaring int value = 125; in all classes or 2. creating static class public static class Constants { public static int value = 125; } using it as Constants.value in all classes. Any other best option? Thank you in advance..

    M G 2 Replies Last reply
    0
    • T Try

      I have 10 different classes in an application. I need to access a constant (int value = 125;) in all ten classes. Which is better way of following two options? 1. Declaring int value = 125; in all classes or 2. creating static class public static class Constants { public static int value = 125; } using it as Constants.value in all classes. Any other best option? Thank you in advance..

      M Offline
      M Offline
      Malcolm Smart
      wrote on last edited by
      #2

      If you put it in a static class, this is accessible to all other classes, not just your ten classes. Putting it in each class is a maintenance nightmare - if you add an 11th class, you have to remember to add the int. If the int changes, you have to change it in all places. Do the classes all derive from a base class? If so, put it there and make it protected.

      We violated nature and our children have to pay the penalty Don't go near the water children... Johnny Cash - 1974

      T 1 Reply Last reply
      0
      • M Malcolm Smart

        If you put it in a static class, this is accessible to all other classes, not just your ten classes. Putting it in each class is a maintenance nightmare - if you add an 11th class, you have to remember to add the int. If the int changes, you have to change it in all places. Do the classes all derive from a base class? If so, put it there and make it protected.

        We violated nature and our children have to pay the penalty Don't go near the water children... Johnny Cash - 1974

        T Offline
        T Offline
        Try
        wrote on last edited by
        #3

        Thank you very much.

        1 Reply Last reply
        0
        • T Try

          I have 10 different classes in an application. I need to access a constant (int value = 125;) in all ten classes. Which is better way of following two options? 1. Declaring int value = 125; in all classes or 2. creating static class public static class Constants { public static int value = 125; } using it as Constants.value in all classes. Any other best option? Thank you in advance..

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          A static variable in a class is the best option (or similar solutions). Declaring the same value in several places largely defeats the purpose of declaring it at all. If you use a constant, the value will be inlined when compiling, so if you change the value you have to recompile all code to get the value to change everywhere. If you have a static variable, all classes will always read from the same physical variable.

          Experience is the sum of all the mistakes you have done.

          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