本文介绍了VB Collection.Add - Before参数的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我从下面的行中收到无效的程序调用或参数调试错误





Hi,
I am getting "Invalid Procedure Call or Argument" debug error from below lines


Public Function SomeMethod
   Dim pos As Integer
   If <<Some Condition>> Then
      pos = 1
   End If
   
   myCol.Add myOjb, myKey, Before:=pos
End Function





我想要的对象要添加到最后一个if条件不满足。如果满足条件,则在指定的索引处添加。



在我添加Before参数之前,它工作正常。但不确定最后一个参数有什么问题



I want the object to be added to the last if condition not met. If condition is met then add at the specified index.

It was working fine just before I added Before argument. But not sure what is wrong with the last argument

推荐答案

If <<Some Condition>> Then
   myCol.Add myOjb, myKey, Before:=1
Else
   myCol.Add myOjb, myKey
End If


Public Function SomeMethod(Optional ByVal pos As Integer = 1)
   myCol.Add myOjb, myKey, Before:=pos
End Function





为什么?



Why?

MSDN写道:

之前



可选。一个表达式,指定集合中的相对位置。要添加的元素放在由Before参数标识的元素之前的集合中。 如果Before是数值表达式,则它必须是从1到集合的Count属性(集合对象)的值的数字。如果Before是String表达式,则它必须对应于指定的键字符串被引用的元素被添加到集合中。你不能同时指定Before和After。

Before

Optional. An expression that specifies a relative position in the collection. The element to be added is placed in the collection before the element identified by the Before argument. If Before is a numeric expression, it must be a number from 1 through the value of the collection's Count Property (Collection Object). If Before is a String expression, it must correspond to the key string specified when the element being referred to was added to the collection. You cannot specify both Before and After.





来源: []


这篇关于VB Collection.Add - Before参数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:55