Hi

I am trying to identify whether a variable I have passed to a listarray exists. If it does I can return the position of the variable using the following function, but how can I return something if it is not found in the list (e.g. variable not found in list so return -1)

PublicFunction WhereAmI_InArray(ByVal TelNum AsString) AsInteger
Dim counter As Integer
Dim aPhone As c_newPhone
For counter = 1 To phoneArray.Count - 1
aPhone = phoneArray(counter)
If aPhone.WhoAmI = TelNum Then
Return counter
End If
Next
End Function

Thanks

Member Avatar for iamthwee

but how can I return something if it is not found in the list (e.g. variable not found in list so return -1

Yeah, returning a -1 would be a good idea to indicate that your item wasn't found.


I don't know if this is what you want but you could do something like:

If phonenumber is found Then
  return position
Else
  return -1
EndIf
Else
  return -1
EndIf

It's the else bit I'm having the problem with. If I test it by displaying the number in a message box, it always returns a 0 instead of returning -1?

I think it is something to do with it being inside the For loop, but I'm not sure how it should be changed, I've tried it again and this time it is always returning -1 so sounds like it is breaking out of the loop almost immediately because it is not finding the position the first time it loops and so is executing the return -1.

It's like the Return -1 needs to go outside of the For loop but I'm not sure how!

Member Avatar for iamthwee

Hmm,

Would be possible to post your entire code. Or that relevant sub function?

The sub function is

Public Function WhereAmI_InArray(ByVal TelNum As String) As Integer
Dim counter As Integer
Dim aPhone As c_newPhone
For counter = 1 To phoneArray.Count - 1
aPhone = phoneArray(counter)
If aPhone.WhoAmI = TelNum Then
Return counter
Else Return -1
End If
Next
End Function

that is returning the value 'counter' to the variable yourLocationinExchange in this sub

PublicSub getYourLocationFromExchange()
yourLocationInExchange = c_exchange.WhereAmI_InArray(yourNumber)
MsgBox(yourLocationInExchange)
If yourNumber = "" Then
phone_form.lblMessage.Text = "Please Dial A number before Pressing Ring"
ElseIf yourLocationInExchange = c_exchange.WhereAmI_InArray(myNumber) Then
phone_form.lblMessage.Text = "You cannot call your own phone"
ElseIf yourLocationInExchange = -1 Then
phone_form.lblMessage.Text = "No Phone Exists "
Else
makeRing(yourLocationInExchange)
End If
End Sub

I'm not sure if you have enough here to go on... I would appreciate any help :cheesy:

Member Avatar for iamthwee

What does your c_newPhone class look like?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.