tags:

views:

959

answers:

4

I've run across the following line in a VB6 application.

mobjParentWrkBk.ExcelWorkBook.Application.Selection.Insert Shift:=xlToRight

Unfortunately Google and other search engines have not been very useful as they seem to omit the := part.

What would be a C# equivalent?

+12  A: 

This is Visual Basic syntax for optional named parameters. The Insert function has a parameter named Shift, which is being specified.

C#, as far as I know, doesn't have an equivalent for optional named parameters. Instead, you'd need to call the Insert method, specifying Type.Missing for all parameters other than Shift.

See also the following StackOverflow question: http://stackoverflow.com/questions/204877/vbnet-operator

UPDATE (2008-10-29):

C# 4.0 is set to introduce optional and named parameters. See this blog entry on codebetter.com.

John Rudy
Thanks, the Shift part made me think it was some kind of language function, didn't think to look at the method parameters.
Esteban Brenes
Not a problem. :)
John Rudy
"specifying null for all parameters " - actually you should specify Type.Missing for missing optional parameters.
Joe
Good catch, Joe; I'll update accordingly.
John Rudy
Because of SO, you answered my question less than 1 minute after I discovered :=, thanks!
kjack
+2  A: 

VB6 and VB.net has optional parameters in method. c# has the option to do method overloading.

VB6/VB.net way of saying Shift:=xlToRight allows passing value of a specific parameter by name.

e.g. public sub mymethod(optional a as integer = -1, optional b as integer=1) ... end sub

I can call this method with mymethod(b:=10)

For c#, there could be 2 methods for this


void Shift()
{
defaultDirection = directionEnum.Left;
Shift(defaultDirection);
}

void Shift(directionEnum direction)
{
}

1 more thing. Although you can call method just by passing parameters, adding parameter name when calling makes code a little more readable.
e.g. DoSomethingWithData(CustomerData:= customer, SortDirection:=Ascending)

Hope that helps.

shahkalpesh
+1  A: 

It's really all about Excel and how it handles inserts. If you select a range of cells and right-click Insert you will be asked which direction to shift the cells. This is from the Excel Help:

=======

Insert Method on Range Object

Inserts a cell or a range of cells into the worksheet or macro sheet and shifts other cells away to make space.

expression.Insert(Shift, CopyOrigin)

expression Required. An expression that returns a Range object.

Shift Optional Variant. Specifies which way to shift the cells. Can be one of the following XlInsertShiftDirection constants: xlShiftToRight or xlShiftDown. If this argument is omitted, Microsoft Excel decides based on the shape of the range.

CopyOrigin Optional Variant. The copy origin.

===========

If you don't have the constants defined you can subsitute the following numbers

xlShiftDown: -4121 xlShiftToLeft: -4159 xlShiftToRight: -4161 xlShiftUp: -4162

DJ
+6  A: 

Just to clarify John Rudy's response.

The sytax is optional (small 'o'), meaning you don't have to use it. In my experience, most VB6 coders don't use the syntax and instead prefer regular 'unnamed' parameters.

If you do choose to use it, all subsequent paramters in the sub procedure call must be named.

The named paramter syntax can be used on all parameters, whether or not the corresponding argument was declared using the VBA keyword Optional (capital 'O'). Consider, for example, this (slightly daft) VBA function with two parameters, one required and one Optional:

Private Function TimesTen( _
    ByVal Value As Long, _
    Optional ByVal Factor As Long = 10 _
) As Long
  TimesTen = Value * Factor
End Function

In VBA, I can call it using named parameters for the required parameter (the Optional paramter can simply be omitted in VBA, unlike in C#.NET for which Type.Missing must be used for all omitted Optional parameters):

  MsgBox TimesTen(Value:=9)

If I wanted to call it with the parameters in the 'wrong' order (why??) I can achieve this using named parameters:

  MsgBox TimesTen(Factor:=11, Value:=9)

Trying to use a regular 'unnamed' parameter call after a named one causes a compile error:

  MsgBox TimesTen(Value:=9, 11)

So why do VBA coders use named parameters if VB6 coders rarely do, even though they use essentially the same language? I think it is because the VBA generated by the MS Office applications' macro recorder tends to (always?) generate named parameters and many VBA coders learn programming this way.

onedaywhen
It's a shame this isn't getting upvoted more ... I gave you +1 for the extra info.
John Rudy
Thanks for the additional information!
Esteban Brenes
yes this is a very interesting answer. I only just discovered := in some vb6 code and wondered what the delphi syntax was doing there. Now I know!
kjack