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. Visual Basic
  4. Which is the best way to set default value for properties

Which is the best way to set default value for properties

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 4 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.
  • A Offline
    A Offline
    andiyuniar
    wrote on last edited by
    #1

    Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.

    Private _AddFoto As Boolean = False
    Public Property AddFoto() As Boolean
    Get
    Return _AddFoto
    End Get
    Set(ByVal value As Boolean)
    _AddFoto = value
    End Set
    End Property

    Or 2.

    Private _AddFoto As Boolean
    <ComponentModel.DefaultValue(False)> _
    Public Property AddFoto() As Boolean
    Get
    Return _AddFoto
    End Get
    Set(ByVal value As Boolean)
    _AddFoto = value
    End Set
    End Property

    Thanks

    S T A D 4 Replies Last reply
    0
    • A andiyuniar

      Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.

      Private _AddFoto As Boolean = False
      Public Property AddFoto() As Boolean
      Get
      Return _AddFoto
      End Get
      Set(ByVal value As Boolean)
      _AddFoto = value
      End Set
      End Property

      Or 2.

      Private _AddFoto As Boolean
      <ComponentModel.DefaultValue(False)> _
      Public Property AddFoto() As Boolean
      Get
      Return _AddFoto
      End Get
      Set(ByVal value As Boolean)
      _AddFoto = value
      End Set
      End Property

      Thanks

      S Offline
      S Offline
      Steven J Jowett
      wrote on last edited by
      #2

      I always use the method of your first example.

      Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

      1 Reply Last reply
      0
      • A andiyuniar

        Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.

        Private _AddFoto As Boolean = False
        Public Property AddFoto() As Boolean
        Get
        Return _AddFoto
        End Get
        Set(ByVal value As Boolean)
        _AddFoto = value
        End Set
        End Property

        Or 2.

        Private _AddFoto As Boolean
        <ComponentModel.DefaultValue(False)> _
        Public Property AddFoto() As Boolean
        Get
        Return _AddFoto
        End Get
        Set(ByVal value As Boolean)
        _AddFoto = value
        End Set
        End Property

        Thanks

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

        I always use the first case. Does case 2 work? I tried your sample by setting

        <Componentmodel.Defaultvalue(True)>...

        but this doesn't seem to work.

        Tosch

        A 1 Reply Last reply
        0
        • T tosch

          I always use the first case. Does case 2 work? I tried your sample by setting

          <Componentmodel.Defaultvalue(True)>...

          but this doesn't seem to work.

          Tosch

          A Offline
          A Offline
          andiyuniar
          wrote on last edited by
          #4

          I used the second code, but i didn't realize until your reply. Yes, the 2nd code doesnt work if we change the value to 'true' I googling to msdn and find this statement: "A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code."

          Private MyVar as Boolean = False
          <DefaultValue(False)> _
          Public Property MyProperty() As Boolean
          Get
          Return MyVar
          End Get
          Set
          MyVar = Value
          End Set
          End Property

          and it make me more confuse,what is the function of this <DefaultValue(False)>

          1 Reply Last reply
          0
          • A andiyuniar

            Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.

            Private _AddFoto As Boolean = False
            Public Property AddFoto() As Boolean
            Get
            Return _AddFoto
            End Get
            Set(ByVal value As Boolean)
            _AddFoto = value
            End Set
            End Property

            Or 2.

            Private _AddFoto As Boolean
            <ComponentModel.DefaultValue(False)> _
            Public Property AddFoto() As Boolean
            Get
            Return _AddFoto
            End Get
            Set(ByVal value As Boolean)
            _AddFoto = value
            End Set
            End Property

            Thanks

            A Offline
            A Offline
            andiyuniar
            wrote on last edited by
            #5

            Apparently, i missunderstanding what i read before. componenmodel.defaultvalue is used for other purpose (designer). thanks

            1 Reply Last reply
            0
            • A andiyuniar

              Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.

              Private _AddFoto As Boolean = False
              Public Property AddFoto() As Boolean
              Get
              Return _AddFoto
              End Get
              Set(ByVal value As Boolean)
              _AddFoto = value
              End Set
              End Property

              Or 2.

              Private _AddFoto As Boolean
              <ComponentModel.DefaultValue(False)> _
              Public Property AddFoto() As Boolean
              Get
              Return _AddFoto
              End Get
              Set(ByVal value As Boolean)
              _AddFoto = value
              End Set
              End Property

              Thanks

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #6

              You have the other responses also, but now there is the new VS2010 way; Public Property theProperty as boolean = false Thats it, all the other Get, Set and local private variable are all taken care of under the hood. [which you can still use if you want, or depending on scenario] The local private variable will be named _theProperty by default, and accessible by that name.

              Dave Don't forget to rate messages!
              Find Me On: Web|Facebook|Twitter|LinkedIn
              Waving? dave.m.auld[at]googlewave.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