3 Replies - 1792 Views - Last Post: 05 December 2009 - 06:45 AM Rate Topic: -----

#1 Gary2507  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 09

getting Conversion from string to type double error

Post icon  Posted 04 December 2009 - 09:03 PM

I have code that loops through a string array to add words one at a time to a line of limited length. I am trying to fit a phrase on up to four lines, centering the words on each line.
My code appears to add the first word but on the 2nd word I get a "conversion from string "IS" to type 'Double' is not valid" error.

Here is my code so far:

varResult = Split(strPhrase)
		intWordCnt = UBound(varResult) 'count words in phrase

		'make words fit on one line
		intBoardLine = 1

		For R = 0 To intWordCnt
			If strBoardLine(intBoardLine) = "" Then	'if line is empty
				strBoardLine(intBoardLine) = varResult®   'Add the word
			Else
				'see if the line will hold the next word too.
				'if room left over, then:
				If strBoardLine(intBoardLine).Length + 1 + varResult® < Choose(intBoardLine, 10, 12, 12, 10) Then
					'add the next word and keep going
					strBoardLine(intBoardLine) = strBoardLine(intBoardLine) & " " & varResult® : GoTo NextR
					'if it fits perfectly, then:
				ElseIf strBoardLine(intBoardLine).Length + 1 + varResult®.Length = Choose(intBoardLine, 10, 12, 12, 10) Then
					'add on the new word, go to the next line, and continue
					strBoardLine(intBoardLine) = strBoardLine(intBoardLine) & " " & varResult®
					intBoardLine = intBoardLine + 1 : GoTo NextR
					'if it does not fit
				ElseIf strBoardLine(intBoardLine).Length + 1 + varResult®.Length > Choose(intBoardLine, 10, 12, 12, 10) Then
					'go to next line but stay on same word
					intBoardLine = intBoardLine + 1
					R = R - 1
				End If
			End If


The phrase in strPhrase is "GOD IS LOVE"

I don't understand why I am getting this error when I didn't on the first loop through the code and it Put the word "GOD" into the line.

This post has been edited by Gary2507: 04 December 2009 - 09:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: getting Conversion from string to type double error

#2 theunborncoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 123
  • Joined: 15-October 09

Re: getting Conversion from string to type double error

Posted 04 December 2009 - 10:08 PM

You have to check you code again because

as per my knowledge

if "varResult" is array then you can use following statement
intWordCnt = UBound(varResult) 'count words in phrase

if "varResult" is array then you cannot use following statement
strBoardLine(intBoardLine) = varResult® 'Add the word
Was This Post Helpful? 0
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: getting Conversion from string to type double error

Posted 05 December 2009 - 04:24 AM

Arrays start at 0. So:

Dim things(5) As Integer

Is an array with 6 elements in total, with indices: 0, 1, 2, 3, 4, 5.

So, intBoardLine = 1 should be 0

And, on this line strBoardLine(intBoardLine).Length + 1 + varResult( R )

Should be strBoardLine(intBoardLine).Length + 1 + varResult( R ).Length like on the later lines. Using Option Strict would catch this mistake.

GoTo isn't used much nowadays, it leads to confusing code. See Dijkstra's '68 paper "Goto statement considered harmful" which started the backlash against it.
Was This Post Helpful? 0
  • +
  • -

#4 Gary2507  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 09

Re: getting Conversion from string to type double error

Posted 05 December 2009 - 06:45 AM

Thanks for the help.

sometimes I look at the code expecting it to do what I was thinking and not seeing what I actually typed.

I appreciate the tip on not using GoTo - carry over from my VB6 days.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1