1

My first question was answered so quickly that I thought i'd ask another question! :)

It's probably really simple for you guys, but I can't seem to get it. I'm trying to convert a simple integer array to a string array because, with the problem i have, it easy to crossover a string rather than a int array (or so I think!)

But here is the code so far:

    Dim Parent1s() As String
     For i = 1 To ChromoLength.Value
      y = y + Val(MyArray(rnd2, i))
      q = Val(MyArray(rnd2, i))
      Parents1(i) = q
  Next i

 For i = 0 To ChromoLength.Value
 Parent1s(i) = Val(Parents1(i))
 Next i
'Cells(8, 1) = Parent1s

I can't sem to get Parent1s to produce a string, keep getting a mismatch error Everything else has been dim'd correctly cause the rest of the code works when i hash out the parent1s statements.

Thanks guys!

1
  • What type is Parents1? Or is that a mistake? Also... why is the first loop using 1 as the lowerbound, and then the second uses 0? Commented Sep 29, 2012 at 23:18

2 Answers 2

3

You can use the CStr() function to convert values to Strings.

Sub toString()

    Dim i As Integer
    Dim iStr As String
    i = 5

    iStr = CStr(i)

    MsgBox (iStr)


End Sub

However, I suspect your problems may be because you have not used Option Explicit and are running into problems with variables being named Parent1s as well as Parents1. If you intended for these to be so similar I would strongly encourage a better naming convention. Otherwise, the following seems like it should work:

For i = 0 To ChromoLength.Value
     Parent1s(i) = CStr(Parents1(i))
 Next i
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the recommendation for Option Explicit. Also, be wary of Integer's as they're only 16-bits. I always use Long instead.
0

If you are attempting to use Parent1s(i) = Val(Parents1(i)) to convert Parents1 to strings, this is not the right way to do it. Val attempts to convert a string, that may contain numbers and letters, to a numeric double. Actually, assuming Parents1 is a standard data type it should implicitly convert to a string with no problems. But if it doesn't, you can always use Cstr().

This may not be your issue, but you never dimension Parent1s().

To dimension Parent1s() add this line:

Redim Parent1s(0 to ChromoLength.Value)

Btw, if you've actually dimmed all of your variables and chosen your names deliberately, you should try to use variable names that will be clear to someone else (which incidentally might mean yourself in a year).

In the future and also to help us help you better, you should either include the lines where you dimension your variables in your sample code or specifically tell us what data types you are using for each variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.