Question of Style for Delegate creation

  • Thread starter Thread starter Just_a_fan
  • Start date Start date
J

Just_a_fan

Both of the Dim statements work in the following. Is there a preference
and why? (Of course, you would not use them both but each, by itself,
works and enables the Invoke to function properly)

Dim newDelegate As New UIDelegate(AddressOf StatusAdd)
Dim newDelegate As UIDelegate = AddressOf StatusAdd
lblStatus.Invoke(newDelegate)

And a follow-on topic:

Actually, this brings up the whole subject of "New". It is something
new to me and I would like to know if there is something written up
explaining about why one uses "New", when it should be used and any
other implications of using it. Coming from VB6, this is foreign to me.

If there is something already written up, then that's great but I don't
know how to find something this esoteric and general. The functional
helps tell "what" but rarely "why".

Thanks for any pointers. I have been wondering about "New" for a while
and the example, above, really pointedly makes me want to know about
this syntax element.

Mike
 
Both of the Dim statements work in the following. Is there a preference
and why? (Of course, you would not use them both but each, by itself,
works and enables the Invoke to function properly)

Dim newDelegate As New UIDelegate(AddressOf StatusAdd)
Dim newDelegate As UIDelegate = AddressOf StatusAdd
lblStatus.Invoke(newDelegate)

And a follow-on topic:

Actually, this brings up the whole subject of "New". It is something
new to me and I would like to know if there is something written up
explaining about why one uses "New", when it should be used and any
other implications of using it. Coming from VB6, this is foreign to me.

If there is something already written up, then that's great but I don't
know how to find something this esoteric and general. The functional
helps tell "what" but rarely "why".

Thanks for any pointers. I have been wondering about "New" for a while
and the example, above, really pointedly makes me want to know about
this syntax element.

Mike

From what I can tell with Reflector, both do exactly the same thing.
The only difference I can see is where the constructor is called. In
the first sample, you call New in the standard way, but in the second
it is called implicitly by the AddressOf keyword.

For your other question, 'New' tells the compiler to create a new
instance of that type and calls its constructor (Public Sub New(...)).
The constructor performs startup tasks such as variable initialization
or property settings that are required for the type to function. For
example, in Window's forms, all the child controls are created and
added to the forms during the form's constructor. Things don't get too
confusing until you see the types such as String that don't require
you to use 'New' to create an instance, all it needs is to have it's
value set as the constructor will be called implicitly. That probably
wasn't the best explanation, so let me know where I can clarify.

Thanks,

Seth Rowe [MVP]
 
Both of the Dim statements work in the following. Is there a preference
and why? (Of course, you would not use them both but each, by itself,
works and enables the Invoke to function properly)

Dim newDelegate As New UIDelegate(AddressOf StatusAdd)
Dim newDelegate As UIDelegate = AddressOf StatusAdd
lblStatus.Invoke(newDelegate)

And a follow-on topic:

Actually, this brings up the whole subject of "New". It is something
new to me and I would like to know if there is something written up
explaining about why one uses "New", when it should be used and any
other implications of using it. Coming from VB6, this is foreign to me.

If there is something already written up, then that's great but I don't
know how to find something this esoteric and general. The functional
helps tell "what" but rarely "why".

Thanks for any pointers. I have been wondering about "New" for a while
and the example, above, really pointedly makes me want to know about
this syntax element.

Mike

Coming from VB6 I understand why the first style would not be used. VB6
would recreate objects that used this style on its own causing reference
problems. It was widely known not to use that style.

LS
 
From what I can tell with Reflector, both do exactly the same thing.
The only difference I can see is where the constructor is called. In
the first sample, you call New in the standard way, but in the second
it is called implicitly by the AddressOf keyword.

For your other question, 'New' tells the compiler to create a new
instance of that type and calls its constructor (Public Sub New(...)).
The constructor performs startup tasks such as variable initialization
or property settings that are required for the type to function. For
example, in Window's forms, all the child controls are created and
added to the forms during the form's constructor. Things don't get too
confusing until you see the types such as String that don't require
you to use 'New' to create an instance, all it needs is to have it's
value set as the constructor will be called implicitly. That probably
wasn't the best explanation, so let me know where I can clarify.

Thanks,

Seth Rowe [MVP]

Good start at my new learning goal. I need to do some reading on that
constructor stuff. That is really the new bit, I guess. It was there,
in different clothes, in VB6 with the old form of the Class. There was
a "constructor" by a different name. It could be that I just pull that
knowledge forward and it will be easy to catch up.

The first reference I ran across is the one with "New" in it. Then upon
further reading I found the one with the "=" sign in it. So that makes
two syntaxes to accomplish the same end -- possibly with a slightly
different execution order of some of the internal functions.

One of my minor complaints about VB in its current life is that there
are SO MANY ways to do the same thing. This seems to lead to code bloat
and slowness of compiling and startup, etc. It seems to me, after some
30 years in the industry, that one way to do each task is better. It is
simpler to learn, write and maintain. I guess it might show that I
started on IBM assembler in two major flavors, 1620 and 360/370. In
each, there was only one way to add a number to a register, one way to
move data from one place to another, etc. That was all that was needed.
But these days, that maxim has gone away. It seems like the more ways
there are to do the same thing the better it is (for whom, I don't
know!).

Thanks for the good answer.

Of course, in assembler, if you coded "B R14" instead of "BR R14", you
have a logical error which was not reported by the assembler. Syntax
correct, function incorrect. You found out about it later in a very bad
way. Extra points if anyone remembers what the problem is with the
first variation.

Major liquid libation award for remembering why BB2 or B7 were special
in the 1620.

But I digress... OF's do that sometimes...

Mike
 
Back
Top