PC Review


Reply
Thread Tools Rate Thread

AddressOf doesn't work with if operator

 
 
Marco Segurini
Guest
Posts: n/a
 
      3rd Dec 2010
Hi all,

is there someone that may explain why the VB compiler report an error
for this code?

thanks a lot
Marco.

Module Module1

Sub fn1()
End Sub

Sub fn2()
End Sub

Sub Main()
Dim if_fn1 As Object = If(True, AddressOf fn1, AddressOf fn2)
' error BC30491: Expression does not produce a value
End Sub

End Module
 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      3rd Dec 2010
Am 03.12.2010 10:30, schrieb Marco Segurini:
> Hi all,
>
> is there someone that may explain why the VB compiler report an error
> for this code?
>
> thanks a lot
> Marco.
>
> Module Module1
>
> Sub fn1()
> End Sub
>
> Sub fn2()
> End Sub
>
> Sub Main()
> Dim if_fn1 As Object = If(True, AddressOf fn1, AddressOf fn2)
> ' error BC30491: Expression does not produce a value
> End Sub
>
> End Module


This is not an issue with the If operator. Doesn't work also:

Dim o As Object = AddressOf fn1

AddressOf itself does not produce a value. You may want to create a Delegate:

Dim o As Object = New System.Windows.Forms.MethodInvoker(AddressOf fn1)

--
Armin
 
Reply With Quote
 
Marco Segurini
Guest
Posts: n/a
 
      3rd Dec 2010
On 3 Dic, 12:36, Armin Zingler <az.nos...@freenet.de> wrote:
> Am 03.12.2010 10:30, schrieb Marco Segurini:
>
>
>
> > Hi all,

>
> > is there someone that may explain why the VB compiler report an error
> > for this code?

>
> > thanks a lot
> > Marco.

>
> > Module Module1

>
> > * * Sub fn1()
> > * * End Sub

>
> > * * Sub fn2()
> > * * End Sub

>
> > * * Sub Main()
> > * * * * Dim if_fn1 As Object = If(True, AddressOf fn1, AddressOf fn2)
> > ' error BC30491: Expression does not produce a value
> > * * End Sub

>
> > End Module

>
> This is not an issue with the If operator. Doesn't work also:
>
> * *Dim o As Object = AddressOf fn1
>
> AddressOf itself does not produce a value. You may want to create a Delegate:
>
> Dim o As Object = New System.Windows.Forms.MethodInvoker(AddressOf fn1)
>
> --
> Armin


Sorry, you are true.

the real code is:

Sub Main()
' this works ...
'Dim if_fn2 As System.Action = AddressOf fn1
'if_fn2()
' but this doesn't
Dim if_fn1 As System.Action = If(True, AddressOf fn1,
AddressOf fn2) ' error BC30491: Expression does not produce a value.
End Sub

Marco.
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      4th Dec 2010
Am 03.12.2010 17:53, schrieb Marco Segurini:
> On 3 Dic, 12:36, Armin Zingler <az.nos...@freenet.de> wrote:
>> Am 03.12.2010 10:30, schrieb Marco Segurini:
>>
>>
>>
>>> Hi all,

>>
>>> is there someone that may explain why the VB compiler report an error
>>> for this code?

>>
>>> thanks a lot
>>> Marco.

>>
>>> Module Module1

>>
>>> Sub fn1()
>>> End Sub

>>
>>> Sub fn2()
>>> End Sub

>>
>>> Sub Main()
>>> Dim if_fn1 As Object = If(True, AddressOf fn1, AddressOf fn2)
>>> ' error BC30491: Expression does not produce a value
>>> End Sub

>>
>>> End Module

>>
>> This is not an issue with the If operator. Doesn't work also:
>>
>> Dim o As Object = AddressOf fn1
>>
>> AddressOf itself does not produce a value. You may want to create a Delegate:
>>
>> Dim o As Object = New System.Windows.Forms.MethodInvoker(AddressOf fn1)
>>
>> --
>> Armin

>
> Sorry, you are true.
>
> the real code is:
>
> Sub Main()
> ' this works ...
> 'Dim if_fn2 As System.Action = AddressOf fn1
> 'if_fn2()
> ' but this doesn't
> Dim if_fn1 As System.Action = If(True, AddressOf fn1,
> AddressOf fn2) ' error BC30491: Expression does not produce a value.
> End Sub


You're right. There seems to be a special assigment behavior. I've looked
for it described in the VB language specificaion but didn't find anything that
describes this behavior. So, the AddressOf operator itself does not produce
a value, it depends on how it is used - which seems to be an exception to how
expressions work in general. Or why else does

Dim o1 As System.Action = AddressOf fn1

work, while

Dim o1 As Object = AddressOf fn1

does not? An Object variable should be able to accept any kind of object,
but this more flexible assignment is not allowed whereas the limitting version works.
Not a consistent behavior, IMO.

So, only this seems to be left:

If(True, New Action(AddressOf fn1), New Action(AddressOf fn2))


--
Armin
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using Addressof operator owais Microsoft VB .NET 3 24th May 2005 07:22 AM
AddressOf Operator and IsPostback RA Microsoft ASP .NET 6 12th Nov 2004 07:53 PM
Getting error in AddressOf Operator, Plz help ItsMe Microsoft VB .NET 4 19th Apr 2004 08:59 AM
addition operator doesn't work in expression buidler Dan Sweetwood Microsoft Access 0 25th Feb 2004 11:44 PM
Re: arithmetic operator doesn't work Wayne Morgan Microsoft Access Forms 0 25th Feb 2004 07:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:27 AM.