how to create class instance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André

    #1

    how to create class instance?

    Hi,

    i defined a class "myclass" in the App_Code directory of my asp.net
    application.

    i tried both syntaxes and both work.
    My question is: is there a difference between both lines and what's the
    best?

    dim x as new myclass
    dim x as new myclass()

    Thanks
    André


  • Noodle

    #2
    Re: how to create class instance?

    On 15 Mar, 09:21, "André" <a...@dre.tywro te:
    Hi,
    >
    i defined a class "myclass" in the App_Code directory of my asp.net
    application.
    >
    i tried both syntaxes and both work.
    My question is: is there a difference between both lines and what's the
    best?
    >
    dim x as new myclass
    dim x as new myclass()
    >
    Thanks
    André
    I would say without the brackets unless you have arguments that need
    to be entered in the constructor then you need the brackets.

    But at the end of the day its just personal preference.

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: how to create class instance?

      "André" <an@dre.tyschri eb:
      i defined a class "myclass" in the App_Code directory of my asp.net
      application.
      >
      i tried both syntaxes and both work.
      My question is: is there a difference between both lines and what's the
      best?
      >
      dim x as new myclass
      dim x as new myclass()

      Both lines are semantically identical. Personally I prefer the second one
      for consistency. VB does not enforce empty parentheses on method
      (constructor) calls.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Mike Hofer

        #4
        Re: how to create class instance?

        On Mar 15, 10:00 am, "Herfried K. Wagner [MVP]" <hirf-spam-me-
        h...@gmx.atwrot e:
        "André" <a...@dre.tysch rieb:
        >
        i defined a class "myclass" in the App_Code directory of my asp.net
        application.
        >
        i tried both syntaxes and both work.
        My question is: is there a difference between both lines and what's the
        best?
        >
        dim x as new myclass
        dim x as new myclass()
        >
        Both lines are semantically identical. Personally I prefer the second one
        for consistency. VB does not enforce empty parentheses on method
        (constructor) calls.
        >
        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
        Agreed.

        However, it's worth noting that if your class only provides a default
        (parameterless) constructor, and you're using Visual Studio .NET
        2002/2003, the IDE will remove the empty parentheses automatically.
        It's annoying. I prefer to put them in there. (A call to the default
        constructor is still, technically, a method call.)


        Comment

        • André

          #5
          Re: how to create class instance?

          Thanks

          "Mike Hofer" <kchighland@gma il.comschreef in bericht
          news:1173972170 .148651.172520@ n76g2000hsh.goo glegroups.com.. .
          On Mar 15, 10:00 am, "Herfried K. Wagner [MVP]" <hirf-spam-me-
          h...@gmx.atwrot e:
          "André" <a...@dre.tysch rieb:
          >
          i defined a class "myclass" in the App_Code directory of my asp.net
          application.
          >
          i tried both syntaxes and both work.
          My question is: is there a difference between both lines and what's the
          best?
          >
          dim x as new myclass
          dim x as new myclass()
          >
          Both lines are semantically identical. Personally I prefer the second one
          for consistency. VB does not enforce empty parentheses on method
          (constructor) calls.
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
          Agreed.

          However, it's worth noting that if your class only provides a default
          (parameterless) constructor, and you're using Visual Studio .NET
          2002/2003, the IDE will remove the empty parentheses automatically.
          It's annoying. I prefer to put them in there. (A call to the default
          constructor is still, technically, a method call.)



          Comment

          Working...