Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am getting "Invalid Procedure Call or Argument" debug error from below lines


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


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
Posted
Comments
Wombaticus 7-Sep-15 12:43pm    
If thr condition is not met in your code above, then pos is left undefined

If the condition is not met then pos is not initialized. You might write, for instance
VB
If <<Some Condition>> Then
   myCol.Add myOjb, myKey, Before:=1
Else
   myCol.Add myOjb, myKey
End If
 
Share this answer
 
Comments
Maciej Los 7-Sep-15 14:55pm    
Good advice, +5!
yash35 8-Sep-15 2:59am    
Can I modify this as below, since I have more conditions to check for and pos varies

If <<Some Condition>> Then
myCol.Add myOjb, myKey, Before:=pos
Else
myCol.Add myOjb, myKey
End If
I'd recommend to read these:
Sub Procedures (Visual Basic)[^]
Collections in Visual Basic[^]

I would do that this way:
VB
Public Function SomeMethod(Optional ByVal pos As Integer = 1)
   myCol.Add myOjb, myKey, Before:=pos
End Function


Why?
MSDN wrote:
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.


Source: Add Method (Collection Object)[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900