Stopwatch Query

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

    #1

    Stopwatch Query

    Why doesn't the following work?

    Dim MyStopwatch() as Stopwatch

    ...Later in the code
    redim preserve MyStopwatch(10)

    ...Later in the code

    MyStopwatch(1). start

    The problem is that all the instances of MyStopwatch are set to nothing and
    when I try doing anything to them it results in an error:
    Object reference not set to an instance of an object

    Normally of course you would specify 'New':

    Dim Mystopwatch as new Stopwatch

    But you can't do a 'New' with an array. The strange thing is that I don't
    get the same error thrown when I do the same with all the other objects in
    my program - just the Stopwatch.

    -Jerry


  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Stopwatch Query

    PlusNet wrote:
    Why doesn't the following work?
    >
    Dim MyStopwatch() as Stopwatch
    >
    ..Later in the code
    redim preserve MyStopwatch(10)
    >
    ..Later in the code
    >
    MyStopwatch(1). start
    >
    The problem is that all the instances of MyStopwatch are set to nothing and
    when I try doing anything to them it results in an error:
    Object reference not set to an instance of an object
    No, the problem is that you don't have any instances at all. You have
    created an array of references, but all references are null. You have to
    create each StopWatch instance that you want to use.
    Normally of course you would specify 'New':
    >
    Dim Mystopwatch as new Stopwatch
    >
    But you can't do a 'New' with an array.
    Exactly. You have to create each and every instance of StopWatch. There
    is no magic multi-constructor.
    The strange thing is that I don't
    get the same error thrown when I do the same with all the other objects in
    my program - just the Stopwatch.
    Yes, you do. Any array of references works the same.

    If you create an array of value types, integers for example, that works
    differently. A value type is a value in itself, so when the array is
    initialised with values, the integers are set to zero.

    Reference type arrays are also initialised, but the difference is that
    the value is the reference, so they get initialised to null.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Spam Catcher

      #3
      Re: Stopwatch Query

      "PlusNet" <Jerry.Spence@s omewhere.comwro te in
      news:4674f6f4$0 $8714$ed2619ec@ ptn-nntp-reader02.plus.n et:
      Why doesn't the following work?
      >
      Dim MyStopwatch() as Stopwatch
      >
      ..Later in the code
      redim preserve MyStopwatch(10)
      >
      ..Later in the code
      >
      MyStopwatch(1). start
      You need to instantiate EACH instance of the array.

      So Dim MyStopwatch() as Stopwatch

      For Each Item as StopWatch in MyStopWatch
      If Item is nothing then
      Item = new StopWatch
      end if
      Next

      The problem is that all the instances of MyStopwatch are set to
      nothing and when I try doing anything to them it results in an error:
      Object reference not set to an instance of an object
      Yup - as expected.
      But you can't do a 'New' with an array. The strange thing is that I
      don't get the same error thrown when I do the same with all the other
      objects in my program - just the Stopwatch.
      Because other objects are NOT arrays.

      And if they are arrays, chances are they're arrays of primatives (and
      thus don't need instantiation).

      Comment

      • PlusNet

        #4
        Re: Stopwatch Query

        Very many thanks for both these answers. That has helped my understanding
        enormously. That has also explained other strange effects I have had in
        other programs!

        -Jerry

        "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
        news:Xns99529FE 68EFA8usenethon eypotrogers@127 .0.0.1...
        "PlusNet" <Jerry.Spence@s omewhere.comwro te in
        news:4674f6f4$0 $8714$ed2619ec@ ptn-nntp-reader02.plus.n et:
        >
        >Why doesn't the following work?
        >>
        >Dim MyStopwatch() as Stopwatch
        >>
        >..Later in the code
        >redim preserve MyStopwatch(10)
        >>
        >..Later in the code
        >>
        >MyStopwatch(1) .start
        >
        You need to instantiate EACH instance of the array.
        >
        So Dim MyStopwatch() as Stopwatch
        >
        For Each Item as StopWatch in MyStopWatch
        If Item is nothing then
        Item = new StopWatch
        end if
        Next
        >
        >
        >The problem is that all the instances of MyStopwatch are set to
        >nothing and when I try doing anything to them it results in an error:
        >Object reference not set to an instance of an object
        >
        Yup - as expected.
        >
        >But you can't do a 'New' with an array. The strange thing is that I
        >don't get the same error thrown when I do the same with all the other
        >objects in my program - just the Stopwatch.
        >
        Because other objects are NOT arrays.
        >
        And if they are arrays, chances are they're arrays of primatives (and
        thus don't need instantiation).

        Comment

        • PlusNet

          #5
          Re: Stopwatch Query

          Thanks you both, for your replies. All OK now.

          -Jerry

          "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
          news:Xns99529FE 68EFA8usenethon eypotrogers@127 .0.0.1...
          "PlusNet" <Jerry.Spence@s omewhere.comwro te in
          news:4674f6f4$0 $8714$ed2619ec@ ptn-nntp-reader02.plus.n et:
          >
          >Why doesn't the following work?
          >>
          >Dim MyStopwatch() as Stopwatch
          >>
          >..Later in the code
          >redim preserve MyStopwatch(10)
          >>
          >..Later in the code
          >>
          >MyStopwatch(1) .start
          >
          You need to instantiate EACH instance of the array.
          >
          So Dim MyStopwatch() as Stopwatch
          >
          For Each Item as StopWatch in MyStopWatch
          If Item is nothing then
          Item = new StopWatch
          end if
          Next
          >
          >
          >The problem is that all the instances of MyStopwatch are set to
          >nothing and when I try doing anything to them it results in an error:
          >Object reference not set to an instance of an object
          >
          Yup - as expected.
          >
          >But you can't do a 'New' with an array. The strange thing is that I
          >don't get the same error thrown when I do the same with all the other
          >objects in my program - just the Stopwatch.
          >
          Because other objects are NOT arrays.
          >
          And if they are arrays, chances are they're arrays of primatives (and
          thus don't need instantiation).

          Comment

          Working...