|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
3D Array from Stringa string that I wrote to a text file with a 2D array. I use the following to load the 2D array from the string: StartAt = 4 For SMAY1 = 1 To 24 Line Input #1, strString For SMAX1 = 1 To 3 gaGridSMA(SMAX1, SMAY1) = CByte(Mid$(strString, StartAt, 1)) StartAt = StartAt + 1 Next SMAX1 StartAt = 4 Next SMAY1 It works fine. When I try to do something similar for the 3D array I get a "Type Mismatch" error. Can you not use the MID$ function with a 3D array? Here is my code: StartAt = 4 For Measure = 1 To nTotalMeasures For Y1 = 1 To 24 Line Input #1, strString For X1 = 1 To 64 gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3)) StartAt = StartAt + 3 Next X1 StartAt = 4 Next Y1 StartAt = StartAt + 192 Next Measure Thanks, Frank >I have a string that I wrote to a textfile from a 3D array, I also have <code snipped>> a string that I wrote to a text file with a 2D array. I use the > following to load the 2D array from the string: > It works fine. When I try to do something similar for the 3D array I <code snipped>> get a "Type Mismatch" error. Can you not use the MID$ function with a > 3D array? Here is my code: Rather than writing the array as a string or having to differentiate between 2D or 3D arrays, you can simply write the entire thing to the file and read it back as-is: '*** Dim MyArr() As Byte Dim FNum As Integer Dim NewVar As Variant ' Set temporary file name and path Const TempName As String = "C:\ATestFile.xyz" ' Declare array ReDim MyArr(1, 1, 1) As Byte ' Populate demo array MyArr(0, 0, 0) = 1 MyArr(0, 0, 1) = 2 MyArr(0, 1, 0) = 3 MyArr(0, 1, 1) = 4 MyArr(1, 0, 0) = 5 MyArr(1, 0, 1) = 6 MyArr(1, 1, 0) = 7 MyArr(1, 1, 1) = 8 ' Open file and write array to disk FNum = FreeFile() Open TempName For Binary Access Write As #FNum ' Important; write array as variant so bounds are written Put #FNum, , CVar(MyArr) Close #FNum ' Erase the local array Erase MyArr ' Re-open the test file and read in the same array FNum = FreeFile() Open TempName For Binary Access Read Lock Write As #FNum Get #FNum, , NewVar Close #FNum ' Type-cast the variant back to a byte array MyArr = NewVar Debug.Print MyArr(1, 0, 1) ' Prints "6" ' Destroy the temp file Call Kill(TempName) '*** Hope this helps, Mike - Microsoft Visual Basic MVP - E-Mail: ED***@mvps.org WWW: Http://EDais.mvps.org/ I already wrote out the TextFile, it contains a variable, as well as a
3D array written to a string, followed by 2 2D arrays also written out as a string. I need to have there thing written in this format because I want to be able to look at the textfile to troubleshoot any problems with the file. This is why I chose not to write it to a binary file - it would be mishmash then. I am able to reload my 2D arrays using the MID$ function via a loop, but I cannot seem to do the same for my 3D array. Perhaps my loop is wrong or something Here is the cose in its entirety: (The arrays are defined in a different module)The program fails on the line "gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3))" Option Explicit Dim Measure As Integer Private Sub Form_Load() frmOpen.LstChangeDrive.Drive = "C:\" frmOpen.LstChangeDir.Path = "C:\Program Files\MyProgram\Songs\" frmOpen.LstOpen.Path = "C:\Program Files\MyProgram\Songs\" frmOpen.LstOpen.Pattern = "*.xyz" End Sub Private Sub LstChangeDrive_Change() LstChangeDir.Path = LstChangeDrive.Drive End Sub Private Sub LstChangeDir_Change() LstOpen.Path = LstChangeDir.Path End Sub Private Sub cmdCancel_Click() Unload frmOpen End Sub Private Sub cmdOpen_Click() Dim FileName As Integer Dim Index As Integer Dim strString As String Dim StartAt As Long StartAt = 4 FileName = FreeFile Open "C:\Program Files\MyProgram\Songs\" & LstOpen.FileName For Input As #1 Do Until EOF(1) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read TotalMeasures Line Input #1, strString nTotalMeasures = CInt(Mid(strString, 18, 3)) Call RedrawGridM Line Input #1, strString 'Ignore "" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read Note Data Grid Line Input #1, strString 'Ignore "Note Data" StartAt = 4 For Measure = 1 To nTotalMeasures For Y1 = 1 To 24 Line Input #1, strString For X1 = 1 To 64 gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3)) StartAt = StartAt + 3 Next X1 StartAt = 4 Next Y1 StartAt = StartAt + 192 Next Measure Line Input #1, strString 'Ignore "" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read SMA Line Input #1, strString 'Ignore "Solo, Mute, Arm" StartAt = 4 For SMAY1 = 1 To 24 Line Input #1, strString For SMAX1 = 1 To 3 gaGridSMA(SMAX1, SMAY1) = CByte(Mid$(strString, StartAt, 1)) StartAt = StartAt + 1 Next SMAX1 StartAt = 4 Next SMAY1 Line Input #1, strString 'Ignore VPF ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read VPF Line Input #1, strString 'Ignore "Volume, Pan, Effects" StartAt = 4 For VPFY1 = 1 To 24 Line Input #1, strString For VPFX1 = 1 To 3 gaGridVPF(VPFX1, VPFY1) = CByte(Mid$(strString, StartAt, 1)) StartAt = StartAt + 1 Next VPFX1 StartAt = 4 Next VPFY1 Loop 'Loop until EOF Close #1 bSongChanged = False sCurrentTitle = LstOpen.FileName frmMain.Caption = sCurrentTitle nCurrentMeasure = 1 Call RedrawGrid Unload frmOpen End Sub Private Sub RedrawGrid() 'frmMain.picGrid.Picture = LoadPicture() frmMain.picGrid.Tag = "C:\Program Files\MyProgram\Images\GRID.bmp" frmMain.picGrid.Picture = LoadPicture(frmMain.picGrid.Tag) For X1 = 1 To 64 For Y1 = 1 To 24 If gaGrid(nCurrentMeasure, X1, Y1) > 0 Then Select Case Y1 Case 1, 2, 3, 4 frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(64, 64, 64), BF Case 5, 6, 7, 8, 9, 10, 11 frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(157, 157, 161), BF Case 12, 13, 14, 15, 16 frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(64, 64, 64), BF Case 17, 18, 19, 20 frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(88, 87, 104), BF Case 21, 22 frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(0, 0, 0), BF Case 23, 24 frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(128, 128, 128), BF End Select End If Next Y1 Next X1 End Sub Private Sub RedrawGridM() frmMain.picGridM.Tag = "C:\Program Files\MyProgram\Images\MEASURE.bmp" frmMain.picGridM.Picture = LoadPicture(frmMain.picGridM.Tag) Measure = 1 For Measure = 1 To nTotalMeasures If gaGridM(MX1, MY1) > 0 Then frmMain.picGridM.Line (MX1, MY1)-Step(0.8, 0.7), RGB(214, 214, 214), BF End If Next Measure End Sub --------------------------------------------------------------------- My TextFile looks something like this (the strings in "NoteData" are acutually a minimum of 195 characters to maximum of about 15,000 characters, I shortenend them for readability here)... Total Measures = 001 Note Data 01-127127127000000000000000000000000 02-127000000000000000000000000000000 03-127000000000000000000000000000000 04-000000000000000000000000000000000 05-000000000000000000000000000000000 06-000000000000000000000000000000000 07-000000000000000000000000000000000 08-000000000000000000000000000000000 09-000000000000000000000000000000000 10-000000000000000000000000000000000 11-000000000000000000000000000000000 12-000000000000000000000000000000000 13-000000000000000000000000000000000 14-000000000000000000000000000000000 15-000000000000000000000000000000000 16-000000000000000000000000000000000 17-000000000000000000000000000000000 18-000000000000000000000000000000000 19-000000000000000000000000000000000 20-000000000000000000000000000000000 21-000000000000000000000000000000000 22-000000000000000000000000000000000 23-000000000000000000000000000000000 24-000000000000000000000000000000000 Solo, Mute, Arm 01-000 02-000 03-000 04-000 05-000 06-000 07-000 08-000 09-000 10-000 11-000 12-000 13-000 14-000 15-000 16-000 17-000 18-000 19-000 20-000 21-000 22-000 23-000 24-000 Volume, Pan, Effects 01-001002007 02-001002007 03-001002007 04-001002007 05-001002007 06-001002007 07-001002007 08-001002007 09-001002007 10-001002007 11-001002007 12-001002007 13-001002007 14-001002007 15-001002007 16-001002007 17-001002007 18-001002007 19-001002007 20-001002007 21-001002007 22-001002007 23-001002007 24-001002007 frankamend***@sbcglobal.net wrote:
> See my other response--cut the sample code down to the bare minimum to> I already wrote out the TextFile, it contains a variable, as well as a > 3D array written to a string, followed by 2 2D arrays also written out > as a string. I need to have there thing written in this format because > I want to be able to look at the textfile to troubleshoot any problems > with the file. This is why I chose not to write it to a binary file - > it would be mishmash then. > > I am able to reload my 2D arrays using the MID$ function via a loop, > but I cannot seem to do the same for my 3D array. Perhaps my loop is > wrong or something Here is the cose in its entirety: ... demonstrate the problem as a single sample---you <may> then find the problem goes away as you find the error (amazing how often that happens! :) . If it doesn't you'll at least have gotten a short enough piece of codethat someone is likely to be willing to take the time to look at in more detail. As I also noted, it certainly doesn't at first blush seem like there should be anything to do w/ Mid$ going on here... > The arrays are Sometimes how you define the array is the most important part of the code. > defined in a different module Please include how you are declaring them. > "gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3))" CByte will fail with a type mismatch error if the input to it starts with non numeric character, or if it was given a null string. Try this sample: Private Sub Form_Load() Dim s As String Dim b As Byte s = "1BC" b = CByte(Mid(s, 1, 1)) ' No problem Debug.Print b b = CByte(Mid(s, 3, 1)) ' Type mismatch, Mid(s, 3, 1) = "C" Debug.Print b b = CByte(Mid(s, 4, 1)) ' Type mismatch, Mid(s, 3, 1) = "" Debug.Print b End Sub <frankamend***@sbcglobal.net> wrote in message Show quoteHide quote news:1129569038.512869.147080@g14g2000cwa.googlegroups.com... >I already wrote out the TextFile, it contains a variable, as well as a > 3D array written to a string, followed by 2 2D arrays also written out > as a string. I need to have there thing written in this format because > I want to be able to look at the textfile to troubleshoot any problems > with the file. This is why I chose not to write it to a binary file - > it would be mishmash then. > > I am able to reload my 2D arrays using the MID$ function via a loop, > but I cannot seem to do the same for my 3D array. Perhaps my loop is > wrong or something Here is the cose in its entirety: (The arrays are > defined in a different module)The program fails on the line > "gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3))" > > Option Explicit > Dim Measure As Integer > > Private Sub Form_Load() > frmOpen.LstChangeDrive.Drive = "C:\" > frmOpen.LstChangeDir.Path = "C:\Program Files\MyProgram\Songs\" > frmOpen.LstOpen.Path = "C:\Program Files\MyProgram\Songs\" > frmOpen.LstOpen.Pattern = "*.xyz" > End Sub > > Private Sub LstChangeDrive_Change() > LstChangeDir.Path = LstChangeDrive.Drive > End Sub > > Private Sub LstChangeDir_Change() > LstOpen.Path = LstChangeDir.Path > End Sub > > Private Sub cmdCancel_Click() > Unload frmOpen > End Sub > > Private Sub cmdOpen_Click() > Dim FileName As Integer > Dim Index As Integer > Dim strString As String > Dim StartAt As Long > > StartAt = 4 > FileName = FreeFile > > Open "C:\Program Files\MyProgram\Songs\" & LstOpen.FileName For > Input As #1 > > Do Until EOF(1) > '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read > TotalMeasures > Line Input #1, strString > nTotalMeasures = CInt(Mid(strString, 18, 3)) > Call RedrawGridM > Line Input #1, strString 'Ignore "" > '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read > Note Data Grid > Line Input #1, strString 'Ignore "Note Data" > StartAt = 4 > For Measure = 1 To nTotalMeasures > For Y1 = 1 To 24 > Line Input #1, strString > For X1 = 1 To 64 > gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, > 3)) > StartAt = StartAt + 3 > Next X1 > StartAt = 4 > Next Y1 > StartAt = StartAt + 192 > Next Measure > Line Input #1, strString 'Ignore "" > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read > SMA > Line Input #1, strString 'Ignore "Solo, Mute, Arm" > StartAt = 4 > For SMAY1 = 1 To 24 > Line Input #1, strString > For SMAX1 = 1 To 3 > gaGridSMA(SMAX1, SMAY1) = CByte(Mid$(strString, StartAt, > 1)) > StartAt = StartAt + 1 > Next SMAX1 > StartAt = 4 > Next SMAY1 > Line Input #1, strString 'Ignore VPF > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Read > VPF > Line Input #1, strString 'Ignore "Volume, Pan, Effects" > StartAt = 4 > For VPFY1 = 1 To 24 > Line Input #1, strString > For VPFX1 = 1 To 3 > gaGridVPF(VPFX1, VPFY1) = CByte(Mid$(strString, StartAt, > 1)) > StartAt = StartAt + 1 > Next VPFX1 > StartAt = 4 > Next VPFY1 > > Loop 'Loop until EOF > Close #1 > > bSongChanged = False > sCurrentTitle = LstOpen.FileName > frmMain.Caption = sCurrentTitle > nCurrentMeasure = 1 > Call RedrawGrid > Unload frmOpen > End Sub > > Private Sub RedrawGrid() > 'frmMain.picGrid.Picture = LoadPicture() > frmMain.picGrid.Tag = "C:\Program Files\MyProgram\Images\GRID.bmp" > frmMain.picGrid.Picture = LoadPicture(frmMain.picGrid.Tag) > For X1 = 1 To 64 > For Y1 = 1 To 24 > If gaGrid(nCurrentMeasure, X1, Y1) > 0 Then > Select Case Y1 > Case 1, 2, 3, 4 > frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(64, > 64, 64), BF > Case 5, 6, 7, 8, 9, 10, 11 > frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), > RGB(157, 157, 161), BF > Case 12, 13, 14, 15, 16 > frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(64, > 64, 64), BF > Case 17, 18, 19, 20 > frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(88, > 87, 104), BF > Case 21, 22 > frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), RGB(0, > 0, 0), BF > Case 23, 24 > frmMain.picGrid.Line (X1, Y1)-Step(0.8, 0.8), > RGB(128, 128, 128), BF > End Select > End If > Next Y1 > Next X1 > End Sub > > Private Sub RedrawGridM() > frmMain.picGridM.Tag = "C:\Program > Files\MyProgram\Images\MEASURE.bmp" > frmMain.picGridM.Picture = LoadPicture(frmMain.picGridM.Tag) > Measure = 1 > For Measure = 1 To nTotalMeasures > If gaGridM(MX1, MY1) > 0 Then > frmMain.picGridM.Line (MX1, MY1)-Step(0.8, 0.7), RGB(214, 214, > 214), BF > End If > Next Measure > End Sub > > --------------------------------------------------------------------- > My TextFile looks something like this (the strings in "NoteData" are > acutually a minimum of 195 characters to maximum of about 15,000 > characters, I shortenend them for readability here)... > > Total Measures = 001 > > Note Data > 01-127127127000000000000000000000000 > 02-127000000000000000000000000000000 > 03-127000000000000000000000000000000 > 04-000000000000000000000000000000000 > 05-000000000000000000000000000000000 > 06-000000000000000000000000000000000 > 07-000000000000000000000000000000000 > 08-000000000000000000000000000000000 > 09-000000000000000000000000000000000 > 10-000000000000000000000000000000000 > 11-000000000000000000000000000000000 > 12-000000000000000000000000000000000 > 13-000000000000000000000000000000000 > 14-000000000000000000000000000000000 > 15-000000000000000000000000000000000 > 16-000000000000000000000000000000000 > 17-000000000000000000000000000000000 > 18-000000000000000000000000000000000 > 19-000000000000000000000000000000000 > 20-000000000000000000000000000000000 > 21-000000000000000000000000000000000 > 22-000000000000000000000000000000000 > 23-000000000000000000000000000000000 > 24-000000000000000000000000000000000 > > Solo, Mute, Arm > 01-000 > 02-000 > 03-000 > 04-000 > 05-000 > 06-000 > 07-000 > 08-000 > 09-000 > 10-000 > 11-000 > 12-000 > 13-000 > 14-000 > 15-000 > 16-000 > 17-000 > 18-000 > 19-000 > 20-000 > 21-000 > 22-000 > 23-000 > 24-000 > > Volume, Pan, Effects > 01-001002007 > 02-001002007 > 03-001002007 > 04-001002007 > 05-001002007 > 06-001002007 > 07-001002007 > 08-001002007 > 09-001002007 > 10-001002007 > 11-001002007 > 12-001002007 > 13-001002007 > 14-001002007 > 15-001002007 > 16-001002007 > 17-001002007 > 18-001002007 > 19-001002007 > 20-001002007 > 21-001002007 > 22-001002007 > 23-001002007 > 24-001002007 > I am declaring the array in a module like so...
Public gaGrid(400, 64, 24) As Byte All of the characters in the string are numeric from 000, to 127. With the exception of the 3rd character, but I ignore the first 3 characters using the StartAt variable. Try breaking your statement into 2 parts like the following, so you can see
what CByte() is converting and eliminate the problem. Change the following: gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3)) To: Dim s As String ' Put this at the beginning of the routine s = Mid$(strString, StartAt, 3) gaGrid(Measure, X1, Y1) = CByte(s) When you get the error, highlight "s" with the mouse and then move the mouse over it, VB will show you its value. Also, you could right click it and select "Add Watch" so you can easily view it each time you run your program. You could also add a Debug.Print to show "s" value like the following: Debug.Print "'"; s; "'" If "s" was "000A ", then this would show: '000A ' So you can see if the string has strange characters at the end. Hope this helps... <frankamend***@sbcglobal.net> wrote in message Show quoteHide quote news:1129595524.886809.199910@g49g2000cwa.googlegroups.com... >I am declaring the array in a module like so... > > Public gaGrid(400, 64, 24) As Byte > > All of the characters in the string are numeric from 000, to 127. With > the exception of the 3rd character, but I ignore the first 3 characters > using the StartAt variable. > Thanks for all your help...
I am relatively new to VB so I am not real good at debugging. I did not do what you said here, but funny you mention the mouse over thing. I did notice that when the code fails, I put my mouse over strString, and the result is "". Here is the code: StartAt = 4 For Measure = 1 To nTotalMeasures For Y1 = 1 To 24 StartAt = 4 Line Input #1, strString For X1 = 1 To 64 gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, 3)) '''''''I put my mouse over strString from the line above StartAt = StartAt + 3 Next X1 Next Y1 'StartAt = StartAt + 192 Next Measure strString gets set using "Line Input#" does it not? Or is it a problem that I am putting the "Line Input inside of my loop? If there is a blank line, Line Input #1, strString would return "". Open the
input file and see if it has blank lines. You may want to adjust the routine that writes to the file so it doesn't write blank lines. You could also check for blank lines and skip them when found, like the following: If strString <> "" Then ' Skip blank lines ' Process the line End If > So it seems like Line Input, strString is not loading the string, Maybe? It's loading the string, it's letting you know that there is a blank line in the file. If you move the mouse over "Measure" and "Y1" and they both show 1, then the first line in the file is blank. Open the file with Notepad and see if you have blank lines. Typically it's the first or last line. <frankamend***@sbcglobal.net> wrote in message Show quoteHide quote news:1129604507.475143.318400@o13g2000cwo.googlegroups.com... > Thanks for all your help... > > I am relatively new to VB so I am not real good at debugging. > > I did not do what you said here, but funny you mention the mouse over > thing. I did notice that when the code fails, I put my mouse over > strString, and the result is "". Here is the code: > > StartAt = 4 > For Measure = 1 To nTotalMeasures > For Y1 = 1 To 24 > StartAt = 4 > Line Input #1, strString > For X1 = 1 To 64 > gaGrid(Measure, X1, Y1) = CByte(Mid$(strString, StartAt, > 3)) > '''''''I put my mouse over strString from the line above > StartAt = StartAt + 3 > Next X1 > Next Y1 > 'StartAt = StartAt + 192 > Next Measure > > strString gets set using "Line Input#" does it not? Or is it a problem > that I am putting the "Line Input inside of my loop? > Yep... I do have blank lines, I did it for readability, I thought by
using "Line Input #1", and then not doing anything with it, I would be in effect skipping it. How can I keep the blank lines and effectively do nothing with the blank lines using the LineInput#1 command? I figured it out, it had nothing to do with the blank lines, these can
be ignored, as I thought, by using the "Line Input #1" command and then doing nothing with it. My problem was that my loop was not correct. I had the Y1 loop inside of the Measure loop, and my file had Y1 in each line as written out to the file. Just had it backwards... DUH! All a learning process... Thanks for all your help Someone and Duane! Much appreciated. I did what you said with the "s" and when I put the mouse over "s" I
get "". So it seems like Line Input, strString is not loading the string, Maybe? frankamend***@sbcglobal.net wrote:
> I don't see a problem. Mike had a simpler answer, probably, but where> I have a string that I wrote to a textfile from a 3D array, I also have .... > It works fine. When I try to do something similar for the 3D array I > get a "Type Mismatch" error. Can you not use the MID$ function with a > 3D array? Here is my code: .... do you get a mismatch error and you don't show the declarations for the arrays and variables which makes discerning precisely what's going on near impossible. Need more specifics, including the error and where precisely. A short sample code that demonstrates the problem would help if you still want this diagnosed. (You may find that in making that self-contained demo the problem goes away). |
|||||||||||||||||||||||