Help with a little error.. - Geeks to Go Forums

Jump to content

Log in Register Register Malware removal guide How it works

Help with a little error.. VB and Strings

#1 TehJoJo

  • Group: Member
  • Posts: 2
  • Joined: 03-June 05

Posted 03 June 2005 - 08:00 AM

I spent a while tryng to figure this out. eh. I figure i'll ask some fellow programmers.

                        [1]   buf = Left(sArray(counter), 12)
                        [2]   I = InStr(buf, " ")
                        [3]   a = I - 2
                        [4]   name = Left(buf, a)

I get a "Run Time Error '5' - Invalid procedure call or argument" on line 4 (the numbers in the code box are the lines.

Anyone know what the problem is? From what I know, it should work.

#2 stu_design

  • Group: Member
  • Posts: 217
  • Joined: 04-April 05

Posted 03 June 2005 - 11:22 AM

what r u trying to do

give us what ur doing, what language etc

littyle more info be appreciated

#3 scicatur

  • Group: Member
  • Posts: 16
  • Joined: 13-May 05

Posted 03 June 2005 - 11:25 AM

String processing at runtime has its own special stumbling stones. If the programmer correctly anticipates them the result is a code that takes in account all possibilities considering the string.

The problem is this: You create a string buffer that has 12 characters and it is created from some sArray(counter) ... now it is not certain (and as programmer you should never assume it to be 100% certain) what exactly the string contents are.

[edited ...v.2]
Say that the 'buf' is " some funny bunny " .. then your [2] line makes I = 1 . (Because of the leading white space)
after this 'a' becomes - 1 . In your code line [4] makes a runtime error because your code attempts to take substring from 'buf' using negative length. Computers will never try to understand paradoxes and so you get the errors.
[/edited]

Now you should probably anticipate what you want to do if your buffer 'buf' contents are something else from what you expected.

Some General Ideas:
- firstly trim all your strings. Trimming means that you cut away all leading as well as all tailing white space characters from your string. I think VB has a function for this purpose

-secondly check the values of critical variables using if-statements. Like before the line [4] you should check in if-statement what is the value of 'a'. Then you make your code handle the situation when the value is out of accepted range.

PS. would be helpful if you mentioned the language in your post like stu_design mentioned. I guessed that you use VB and checked your functions with google but still helpful if you mention.

#4 TehJoJo

  • Group: Member
  • Posts: 2
  • Joined: 03-June 05

Posted 03 June 2005 - 03:01 PM

Yes, i got it to work. I was because a few of the strings in the array didnt have a space, so it made integer a negative.

Share this topic: