What in the function is going on?

Results 1 to 3 of 3

Thread: What in the function is going on?

  1. #1
    Join Date
    Dec 1969

    Default What in the function is going on?

    Help me with this code:<BR><BR>&#039;******************************* *****<BR>Function BuildSpace(theString, intNumber)<BR> strSpace = ""<BR> lenString = len(theString)<BR> StringDiff = intNumber - len(theString)<BR> <BR> For i = 0 to StringDiff - 1<BR> strSpace = strSpace & chr(32)<BR> Next<BR> <BR> BuildSpace = strSpace & theString<BR>End Function<BR><BR>var = BuildSpace("test", 10)<BR>&#039;**********************************<BR ><BR>I get this error<BR>&#039;**************************<BR>Micro soft VBScript runtime error &#039;800a000d&#039; <BR><BR>Type mismatch: &#039;BuildSpace&#039; <BR>&#039;***************************<BR><BR>Wha t I am trying to do is give the function a string and the length of what I need the resulting string to be. When the string is too short then chr(32) are added till it is the right length.<BR><BR>Why is it giving me a Type mismatch? thanks

  2. #2
    Join Date
    Dec 1969

    Default Try This...(Smoke)...(Drink)...

    Try this instead...<BR><BR>Function BuildSpace(theString, intNumber)<BR>strSpace = ""<BR><BR>strSpace = Left(theString & Space(intNumber), intNumber)<BR><BR>BuildSpace = strSpace<BR><BR>End Function<BR><BR>Little easier and you don&#039;t have to go through loop...<BR><BR>KLC

  3. #3
    Join Date
    Dec 1969

    Default Why add two wasted lines?

    Function BuildSpace(theString, paddedSize)<BR>&nbsp; &nbsp; BuildSpace = Left(theString & Space(paddedSize), paddedSize)<BR>End Function<BR><BR>Does exactly the same thing. (And uses a meaningful variable name, but that&#039;s my own personal bug-a-boo that you can choose to ignore.)<BR><BR>I suppose, for completeness, we should do it essentially the same way he was trying to do:<BR><BR>Function BuildSpace(theString, paddedSize)<BR>&nbsp; &nbsp; BuildSpace = theString & Space( paddedSize - Len(theString) )<BR>End Function<BR><BR>The thing wrong (or maybe right! depending on what you want!) with that is that if theString is longer than the desired paddedSize already, then the result of BuildSpace will simply be that oversized string. The first version will chop the original string at the size given.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts