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. Optional property c#

Optional property c#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
8 Posts 7 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.
  • M Offline
    M Offline
    Member 10836284
    wrote on last edited by
    #1

    I want to create a class that has an optional property. I don't want to create a subclass
    Example
    public class MyClass
    {
    public MyClass( int extraPoperty)
    {
    // Something like this
    public int ExtraPoperty { get; set; }

        }
    
        public int EntityId { get;  set; }
    }
    

    Is this possible ?

    OriginalGriffO L P R B 6 Replies Last reply
    0
    • M Member 10836284

      I want to create a class that has an optional property. I don't want to create a subclass
      Example
      public class MyClass
      {
      public MyClass( int extraPoperty)
      {
      // Something like this
      public int ExtraPoperty { get; set; }

          }
      
          public int EntityId { get;  set; }
      }
      

      Is this possible ?

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      No - you can't define a property within a method or constructor, only within the class body. What exactly are you trying to do? Why do you think you need this?

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M Member 10836284

        I want to create a class that has an optional property. I don't want to create a subclass
        Example
        public class MyClass
        {
        public MyClass( int extraPoperty)
        {
        // Something like this
        public int ExtraPoperty { get; set; }

            }
        
            public int EntityId { get;  set; }
        }
        

        Is this possible ?

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

        An optional property is the same as any other property; the optional bit depends on how you use it. Maybe you should consider a nullable type[^].

        1 Reply Last reply
        0
        • M Member 10836284

          I want to create a class that has an optional property. I don't want to create a subclass
          Example
          public class MyClass
          {
          public MyClass( int extraPoperty)
          {
          // Something like this
          public int ExtraPoperty { get; set; }

              }
          
              public int EntityId { get;  set; }
          }
          

          Is this possible ?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I would suggest that what you are asking about is going to violate the S in SOLID.

          This space for rent

          1 Reply Last reply
          0
          • M Member 10836284

            I want to create a class that has an optional property. I don't want to create a subclass
            Example
            public class MyClass
            {
            public MyClass( int extraPoperty)
            {
            // Something like this
            public int ExtraPoperty { get; set; }

                }
            
                public int EntityId { get;  set; }
            }
            

            Is this possible ?

            R Offline
            R Offline
            Ralf Meier
            wrote on last edited by
            #5

            It depends on what you mean with "optional Property". Do you mean something like this : You have a Property "BorderStyle" and a Property "BorderSize". If you select BoderStyle = None then you want that BorderSize isn't to be seen (invisible).

            1 Reply Last reply
            0
            • M Member 10836284

              I want to create a class that has an optional property. I don't want to create a subclass
              Example
              public class MyClass
              {
              public MyClass( int extraPoperty)
              {
              // Something like this
              public int ExtraPoperty { get; set; }

                  }
              
                  public int EntityId { get;  set; }
              }
              

              Is this possible ?

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

              Check out the "ExpandoObject". [ExpandoObject Class (System.Dynamic)](https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx) (Always makes me think of "Plastic Man" for some reason).

              "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

              K 1 Reply Last reply
              0
              • L Lost User

                Check out the "ExpandoObject". [ExpandoObject Class (System.Dynamic)](https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx) (Always makes me think of "Plastic Man" for some reason).

                "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                K Offline
                K Offline
                Karthik_Mahalingam
                wrote on last edited by
                #7

                5 :thumbsup:

                1 Reply Last reply
                0
                • M Member 10836284

                  I want to create a class that has an optional property. I don't want to create a subclass
                  Example
                  public class MyClass
                  {
                  public MyClass( int extraPoperty)
                  {
                  // Something like this
                  public int ExtraPoperty { get; set; }

                      }
                  
                      public int EntityId { get;  set; }
                  }
                  

                  Is this possible ?

                  B Offline
                  B Offline
                  BenScharbach
                  wrote on last edited by
                  #8

                  I would add the optional property in an Interface. Then you inherit the Interface you want to apply optional methods or properties. For example;

                  public interface IExtraProperty
                  {
                  // Something like this
                  public int ExtraPoperty { get; set; }
                  }

                  public class MyClass : IExtraProperty

                  Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach

                  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