Initializing Member Variables of Class ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe HM

    Initializing Member Variables of Class ...

    Hello -

    I was wondering what the proper way is to initialize member variables
    of a Class. Here is the code ...

    Public Class
    Private mX As Single = Single.NaN
    ...

    Public Sub New()
    mX = Single.NaN
    End Sub
    End Class

    Do I need the New in this case or is it fine to just initialize them
    with the declaration? Which is the better style?

    Thanks!
    Joe
  • zacks@construction-imaging.com

    #2
    Re: Initializing Member Variables of Class ...

    On Sep 17, 8:18 am, Joe HM <unixve...@yaho o.comwrote:
    Hello -
    >
    I was wondering what the proper way is to initialize member variables
    of a Class.  Here is the code ...
    >
    Public Class
      Private mX As Single = Single.NaN
      ...
    >
      Public Sub New()
        mX = Single.NaN
      End Sub
    End Class
    >
    Do I need the New in this case or is it fine to just initialize them
    with the declaration?  Which is the better style?
    That question has been asked several times already, and the range of
    answers generally boil down to "have it your way".

    But if you expect to serialize or deserialize the class it is my
    impression that you must have a constructor with no parameters.

    Personally, I prefer to initialize class members in the constructor.

    Comment

    • rowe_newsgroups

      #3
      Re: Initializing Member Variables of Class ...

      On Sep 17, 8:18 am, Joe HM <unixve...@yaho o.comwrote:
      Hello -
      >
      I was wondering what the proper way is to initialize member variables
      of a Class.  Here is the code ...
      >
      Public Class
        Private mX As Single = Single.NaN
        ...
      >
        Public Sub New()
          mX = Single.NaN
        End Sub
      End Class
      >
      Do I need the New in this case or is it fine to just initialize them
      with the declaration?  Which is the better style?
      >
      Thanks!
      Joe
      One thing to remember is that you must chain constructors when you
      have multiple ways of instantiating the class. This is pretty simple
      to do, you just have to remember to do it.

      Just in case you or others readers don't know, the pattern is:

      ///////////
      Public Sub New()
      Me.Foo = "bar"
      End Sub

      Public Sub New(barValue as String)
      Me.New()

      Me.Bar = barValue
      End Sub
      ///////////

      Thanks,

      Seth Rowe [MVP]

      Comment

      • Joe HM

        #4
        Re: Initializing Member Variables of Class ...

        On Sep 17, 8:26 am, za...@construct ion-imaging.com wrote:
        On Sep 17, 8:18 am, Joe HM <unixve...@yaho o.comwrote:
        >
        >
        >
        >
        >
        Hello -
        >
        I was wondering what the proper way is to initialize member variables
        of a Class.  Here is the code ...
        >
        Public Class
          Private mX As Single = Single.NaN
          ...
        >
          Public Sub New()
            mX = Single.NaN
          End Sub
        End Class
        >
        Do I need the New in this case or is it fine to just initialize them
        with the declaration?  Which is the better style?
        >
        That question has been asked several times already, and the range of
        answers generally boil down to "have it your way".
        >
        But if you expect to serialize or deserialize the class it is my
        impression that you must have a constructor with no parameters.
        >
        Personally, I prefer to initialize class members in the constructor.- Hide quoted text -
        >
        - Show quoted text -
        Hello -

        Makes sense ... thanks for the feedback!

        Joe

        Comment

        • Joe HM

          #5
          Re: Initializing Member Variables of Class ...

          On Sep 17, 9:11 am, rowe_newsgroups <rowe_em...@yah oo.comwrote:
          On Sep 17, 8:18 am, Joe HM <unixve...@yaho o.comwrote:
          >
          >
          >
          >
          >
          Hello -
          >
          I was wondering what the proper way is to initialize member variables
          of a Class.  Here is the code ...
          >
          Public Class
            Private mX As Single = Single.NaN
            ...
          >
            Public Sub New()
              mX = Single.NaN
            End Sub
          End Class
          >
          Do I need the New in this case or is it fine to just initialize them
          with the declaration?  Which is the better style?
          >
          Thanks!
          Joe
          >
          One thing to remember is that you must chain constructors when you
          have multiple ways of instantiating the class. This is pretty simple
          to do, you just have to remember to do it.
          >
          Just in case you or others readers don't know, the pattern is:
          >
          ///////////
          Public Sub New()
            Me.Foo = "bar"
          End Sub
          >
          Public Sub New(barValue as String)
            Me.New()
          >
            Me.Bar = barValue
          End Sub
          ///////////
          >
          Thanks,
          >
          Seth Rowe [MVP]http://sethrowe.blogsp ot.com/- Hide quoted text -
          >
          - Show quoted text -
          Hello -

          I didn't even think about that case ... Thanks!

          Joe

          Comment

          • eBob.com

            #6
            Re: Initializing Member Variables of Class ...

            (For some reason OE is no longer putting a in col. 1 of the post I am
            replying to. You'll find my reply appended below following the ===s.)

            "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
            news:5a769163-0948-457a-abdc-e77d8d8c0ad1@25 g2000hsx.google groups.com...
            On Sep 17, 8:18 am, Joe HM <unixve...@yaho o.comwrote:
            Hello -
            >
            I was wondering what the proper way is to initialize member variables
            of a Class. Here is the code ...
            >
            Public Class
            Private mX As Single = Single.NaN
            ...
            >
            Public Sub New()
            mX = Single.NaN
            End Sub
            End Class
            >
            Do I need the New in this case or is it fine to just initialize them
            with the declaration? Which is the better style?
            >
            Thanks!
            Joe
            One thing to remember is that you must chain constructors when you
            have multiple ways of instantiating the class. This is pretty simple
            to do, you just have to remember to do it.

            Just in case you or others readers don't know, the pattern is:

            ///////////
            Public Sub New()
            Me.Foo = "bar"
            End Sub

            Public Sub New(barValue as String)
            Me.New()

            Me.Bar = barValue
            End Sub
            ///////////

            Thanks,

            Seth Rowe [MVP]


            ========== Reply from eBob.com =============== =========

            Hi Seth,

            Can you elaborate a bit. When you say "must" ... does that mean that I will
            see a compiler error if I violate the rule? Also, when you say "chain" do
            you mean what I'll call a strict chain in which if I have n+1 constructors,
            0-n, the sequence will always be 0 or 1,0 or 2,1,0 or 3,2,1,0 or ... or
            n,n-1,...,0?

            Thanks, Bob


            Comment

            Working...