Attribute Conversion Confusion

Published 14 June 06 10:54 PM | adrian

There's a difference when defining attribute between C# and Visual Basic.

I was converting the following code snippet, a typical WCF contract definition. (In fact, it was taken from a ChatRoom sample from netfx3.com.)

[ServiceContract(Session = true, CallbackContract = typeof(IChatCallback))]
interface IChat

Logically, the attribute part should be translated into this in VB:

<ServiceContract(Session = true, CallbackContract = GetType(IChatCallback))> Public Interface IChat

Well, it's not. First of all, there is no such thing as assignment operation in parameter area (the above code is constructor call of ServiceContract attribute class. The Intellisense gave away unhelpful cue to do the above conversion. It displays tooltip like:

New([CallbackContract As System.Type], [ConfigurationName As String], ... and so on. Based on the info, I tried giving all of the information it requests by coding it like this:

<ServiceContract(GetType(IchatCallback), , ... and so on. In the end, VS complains about “Too many arguments to 'Public Sub New()'.” Using the Object Explorer, clearly that the definition of the constructor is without parameter. After digging through samples, luckily there's a hint on one of the samples from MSDN. You should use “:=” in place of “=” to do the assignment in VB. So the above C# code can be translated into:

<ServiceContract(Session:=true, CallbackContract:=GetType(IChatCallback))> Public Interface IChat

That line, does the job.

Share this post: | | | |
Filed under:

Comments

# adrian said on June 15, 2006 03:21 AM:

Of course,

because it (VB.NET) sucks!

:)

# adrian said on June 15, 2006 07:04 AM:

Iya. Ini sih syntax VB-nya yg dodol.

Attribute is just another .NET Type. Dia punya dua jenis parameter: Positional Parameter & Named Parameter.

Positional Parameter bisa kamu assign spt constructor, Named Parameter yg mesti kamu sebut namanya, dan manually assign the value (spt halnya Session & CallbackContract pd contoh diatas).

It's VB assignment for Attribute Named Parameters yg pakai ":=".

Makanya, C# aja...

# adrian said on June 16, 2006 04:14 PM:

Lebih baik daripada tidak ada sama sekali.