6

I'm a C# developer working on a VB.NET project, and VS keeps trying to get me to use the := thingie when I call a function with a ByRef parameter like so:

While reader.Read()
HydrateBookFromReader(reader:=???)

the HydrateBookFromReader function has the following signature:

Public Function HydrateBookFromReader(ByRef reader As SqlDataReader) As Book

Why does intellisense keep insisting that I use that := construction, and what is it for?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Josh E
  • 7,390
  • 2
  • 32
  • 44

3 Answers3

10

In VB, the := is used in specifying named parameters.

Contact(Address:="2020 Palm Ave", Name:="Peter Evans")

This is especially useful for specifying optional parameters.

DOK
  • 32,337
  • 7
  • 60
  • 92
  • Exactly the answer I was looking for! Thanks. The other answers were also very helpful and informative, but focus on other aspects of the question – Josh E May 23 '09 at 20:29
  • yw. Kinda hard to Google punctuation marks. When they add this to C# in the next release, it'll just be the colon without the equal sign. Just to keep us on our toes when we switch languages. – DOK May 23 '09 at 20:41
  • 1
    I'd just add that this is especially useful when dealing with a method with lots of optional parameters. You see this most when calling Office APIs--there are some APIs that have 15 optional paramters and it's much more convenient to specify the parameters by name rather than omitting all the positional arguments. (This is why C# is adding the feature.) – panopticoncentral May 26 '09 at 16:59
4

Why does intellisense keep insisting that I use that := construction, and what is it for?

It's important to note that IntelliSense doesn't insist, it proposes. Using it in your case wouldn't make sense … this feature is primarily used for very long parameter lists with many optional parameters, of which you only want to pass, say, the last one. It's useful when working with Microsoft Office Interop.

Also (since you mention it in your tags): this has got nothing to do with ByRef. ByRef is equivalent to ref and out in C#, i.e. it allows the method to manipulate the parameter itself.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Very insightful, the key thing here was that intellisense is there to suggest things but you still retain control. – Neil Trodden May 23 '09 at 20:25
  • good points -- Intellisense was insisting on proposing it all the time, causing no end of annoyance for me, especially now knowing what it's for, and knowing that it is nonsensical when applied to a single-parameter method – Josh E May 23 '09 at 20:28
  • The reason i mentioned ByRef in the tags is that I don't see the intellisense suggestion when I do the same typing against a ByVal parameter, so I thought it had something to do with ByRef – Josh E May 23 '09 at 20:31
1

Intellisense may be suggesting the := syntax, but I suspect that it will compile without it.

HydrateBookFromReader(myReader);

In future versions of C# where optional parameters are allowed, named parameters will allow you to specify some parameters but not others, and to specify parameters in a different order than they were declared. Named parameters will also allow you to optionally clarify the purpose of the parameter being passed in, making the code more readable in some cases.

Named parameters will be especially important in c# 4.0 for COM Interop, where many superfluous parameters can be eliminated.

Anders Hejlsberg has an excellent discussion about the future of C# on Channel 9 at http://channel9.msdn.com/pdc2008/TL16/. His discussion about named parameters is at 40 minutes and 45 seconds into the talk.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • +1 for the good tie-in with C# 4.0, however it doesn't entirely address the original question. – Josh E May 23 '09 at 20:30