|
-
Oct 4th, 2003, 07:25 AM
#1
Thread Starter
Fanatic Member
Enter a delimited string into an array [SOLVED]
I'm trying to break a delimited string into an array using the Split function, then transfer the string array into another array.
Using the split function to break this down generates a Type Mismatch.
VB Code:
Choices = Mid$(QuestionArray(0).Question, InStr(QuestionArray(0).Question, ">") + 1, Len(QuestionArray(0).Question))
I don't understand where the type is mismatching. The result is a delimited String. I am using a : as the delimiter, those have always worked for me in the past. The only thing that comes to mind is that the entire formula I'm using must be of type String.
Last edited by hothead; Oct 4th, 2003 at 03:56 PM.
-
Oct 4th, 2003, 10:53 AM
#2
Hyperactive Member
The Instr function is incorrect. The first arg has to be the starting char position. Change
InStr(QuestionArray(0).Question, ">")
to
InStr(1, QuestionArray(0).Question, ">")
-
Oct 4th, 2003, 12:00 PM
#3
So Unbanned
Originally posted by vbmom
The Instr function is incorrect. The first arg has to be the starting char position.
The start position is Optional. Also a Type mismatch deals with one memory type trying to be passed to another, incompatible type.
Your problem seems to be linked to QuestionArray(0).Question, so what's this declared as?
Or it could be that Choices is not willing to accept the data.
Why ">" if your delimiter is ":"?
-
Oct 4th, 2003, 12:19 PM
#4
Thread Starter
Fanatic Member
">" is used as a delimiter for parsing the question from the answers. That part works fine.
I can parse single strings no problem, it's string arrays that I'm stuck on.
vbmom, I just changed my InStr to fit yours, and I'm still getting the error.
digitalerror, I declared the QuestionArray variable as a type tQuestion, as in the example you gave me. The code works fine without the Split function thrown in, so maybe if I could get a better grasp of the Split function, I might be able to fix this problem. I did a search on MSDN, and found a part of the function I'm not familiar with, here's the same code with the Split function thrown in.
VB Code:
Choices = Split(Mid$(QuestionArray(0).Question, InStr(1, QuestionArray(0).Question, ">") + 1, Len(QuestionArray(0).Question)), ":", , vbTextCompare)
And finally, here's the way the questions are defined in the text file in case you need that also.
Question>Answer1:Answer2:Answer3:Answer4:
The 3rd parameter, Limit, is giving me some doubts as to the accuracy of the code. I left it blank, as it defaults to -1, which places no limits as to how big the array can be, from my understanding.
Last edited by hothead; Oct 4th, 2003 at 12:38 PM.
-
Oct 4th, 2003, 01:01 PM
#5
Hyperactive Member
In your first post you mentioned Split but didn't use it in the code. I was confused about that and now I see that you replaced the Mid$ with Split and added some args that made sense... so let's see...
BTW, even if a parameter is optional, you still have to supply something, even if it's nothing. Can't go rearranging arguments on Bill.
Is Choices dimed as follows?
Dim Choices() as String
Gotta be that way or over-confidently Dim'd to the number of expected elements.
Split(Mid$(X, InStr(1, X, ">") + 1, Len(X)), ":", , vbTextCompare)
The Split part looks okay, which means the X, which is the Question(0).Question you're supplying may not be a string. Maybe you have to be explicit about some other property of the Question object you're trying to use. Maybe VB doesn't like the fact that you have an array and an object named Question. I don't see how that would work anyway.
Why don't you put Question(0).Question in a string var and debug.print it before the split?
Also, I don't think this would cause type mismatch but why do you want to take Mid of a string for a specified Length > the length of the string? If you want to get everything after the ">", then you just take Mid(X,Instr(1,X,">")+1).
Gotta go do laundry now. Hope this helped.
-
Oct 4th, 2003, 01:27 PM
#6
Thread Starter
Fanatic Member
I'm still getting the error.
I now know for certain that it's the way Split is being used. I tried the same method in a new program, and it errored out just as it does now.
Last edited by hothead; Oct 4th, 2003 at 01:52 PM.
-
Oct 4th, 2003, 02:53 PM
#7
Maybe this'll help somehow (no errors):
VB Code:
Private Type Questions
Question As String
End Type
Dim Choices() As String
Dim QuestionArray(0) As Questions
Private Sub Form_Load()
QuestionArray(0).Question = "Question>Answer1:Answer2:Answer3:Answer4:"
Choices = Split(Mid$(QuestionArray(0).Question, InStr(1, QuestionArray(0).Question, ">") + 1, Len(QuestionArray(0).Question)), ":", , vbTextCompare)
End Sub
Oh, btw, the ":" at the very end makes an array that's 1 element longer than I think you want (that element is "")
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Oct 4th, 2003, 03:09 PM
#8
Thread Starter
Fanatic Member
I'm seeing a new error message.
Compiler Error: Can't assign to array.
-
Oct 4th, 2003, 03:13 PM
#9
Post your entire code along with the file you want to delimit (also, my code works fine on my comp)
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Oct 4th, 2003, 03:18 PM
#10
Thread Starter
Fanatic Member
Too late. I fixed it myself. The problem was in the declaration of the Choices variable.
I used Choices(3) when I should've used Choices().
-
Oct 4th, 2003, 03:19 PM
#11
I'd give this one to vbmom
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Oct 4th, 2003, 03:27 PM
#12
Thread Starter
Fanatic Member
Well, everybody had a hand in it. Turns out there were several things wrong. In the file, I had an extra ":" at the end of each line, which I guess was making the program try to add an extra element to an array where it was not necessary. I believe another cause was the complexity of my formula. It was using an unnecessary mixture of ints, strs, and tquestions.
-
Oct 4th, 2003, 04:46 PM
#13
So Unbanned
Originally posted by vbmom
BTW, even if a parameter is optional, you still have to supply something, even if it's nothing. Can't go rearranging arguments on Bill.
Only if compare is specified.
http://msdn.microsoft.com/library/de...vafctinstr.asp
Start
Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. The start index is 1 based.
and further down...
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
'...
' Comparison is binary by default (last argument is omitted).
MyPos = InStr(SearchString, SearchChar) ' Returns 9.
-
Oct 4th, 2003, 05:13 PM
#14
Hyperactive Member
Dinnertime. Well Digi-err, I didn't know that. I guess I always use vbTextCompare because I'm always looking for text in any case when I'm searching.
Congrats hothead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|