Answered by:
Saving data to a random file using a fixed array element error

Question
-
The file structure is fine for retrieving data from the file but when I go to write data to the elements and hit any of the fixed array elements, I get an exception error. this has got to be a sytax problem I am just not seeing. Maybe someone could point me in the right direction.
It seems that I am not addressing the array member correctly yet the same logic pulls it properly from existing records on file.
in a Module I have the structure as follows:
Structure INVOICE
<VBFixedString(6)> Public NUM As String
<VBFixedString(2)> Public SYMBOL As String
Public FAC_ID As Short
Public CORP_ID As Short
Public DTE As Integer
<VBFixedString(10)> Public INV_NO As String
<VBFixedString(10)> Public PO As String
<VBFixedString(2)> Public FILLER1 As String
Public DELETE As Short
<VBFixedArray(19)> Public ID() As Short
<VBFixedArray(19)> Public QTY() As Integer
<VBFixedArray(19)> Public COST() As Integer
<VBFixedString(1)> Public TRANSFER As String
Public TRAIL As Integer
<VBFixedString(20)> Public FILLER As String
End StructurePublic WRK() As INVOICE
Class Review sub send_rec ReDim Wrk(0 To 1) FileClose(4) FileOpen(4, "D:\myDB\TMP\INVOICE.TMP", OpenMode.Random, , OpenShare.Shared, Len(WRK(1))) PTR = LOF(4) / Len(WRK(1)) PTR = PTR + 1 for j as int = 0 to 19 x = 5 Wrk(1).QTY(j) = X (error occurs here: Object reference not set to an instance of an object.) next j If ITR > 0 And ITR <= PTR Then FilePut(4, WRK(1), ITR)
end if
fileclose(4)
End sub End Class
- Edited by Rx_Michigan Tuesday, February 7, 2012 8:56 PM
Tuesday, February 7, 2012 8:04 PM
Answers
-
Hi,
Here is an example of Random File Access.>>
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/50d7b930-edd3-4ba4-92fe-2c16e5235e51
Would you like me to try an example with your STRUCTURE?
Try the following just after your ReDim
Work(0) = New Invoice
Work(1) = New Invoice
That should get rid of the error "Object reference not set to an instance of an object."
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Edited by John Anthony Oliver Friday, February 10, 2012 1:49 PM
- Marked as answer by Youen Zen Thursday, February 23, 2012 3:23 AM
Friday, February 10, 2012 1:48 PM -
I actually discovered my own answer on this. I have to define an external array for each arrayed item in the structure and pass it along to the data structure as a complete unit. (I wish this stuff was explained better!) I can manipulate each of the pieces of the array pulled out of the data record but going in MUST be a a complete unit.
sub send_rec ReDim Wrk(0 To 1)
ReDim qty1(0 To 19) ReDim cst1(0 To 19) ReDim id1(0 To 19) For J As Int32 = 0 To 19 If Val(Grid1.Rows(J).Cells(1).Value) > 0 Then qty1(J) = CInt(Grid1.Rows(J).Cells(1).Value) * 100 cst1(J) = Int(CDbl(Grid1.Rows(J).Cells(3).Value) * 100)
id1(J) = CInt(Grid1.Rows(J).Cells(4).Value) End If Next J wrk(1).QTY = qty1 ' have to pass the information across as a whole array'
'In VB6 this is how this was stated to pass along an array value to data structure'
'not <> wrk(1).QTY(j) = qty1(j)' wrk(1).ID = id1 ' have to pass the information across as a whole array' wrk(1).COST = cst1 ' have to pass the information across as a whole array'
- Edited by Rx_Michigan Tuesday, February 7, 2012 11:57 PM
- Marked as answer by Youen Zen Thursday, February 23, 2012 3:23 AM
Tuesday, February 7, 2012 11:44 PM -
sub send_rec ReDim Wrk(0 To 1) ReDim qty1(0 To 19) ReDim cst1(0 To 19) ReDim id1(0 To 19) For J As Int32 = 0 To 19 If Val(Grid1.Rows(J).Cells(1).Value) > 0 Then qty1(J) = CInt(Grid1.Rows(J).Cells(1).Value) * 100 cst1(J) = Int(CDbl(Grid1.Rows(J).Cells(3).Value) * 100) id1(J) = CInt(Grid1.Rows(J).Cells(4).Value) End If Next J wrk(1).QTY = qty1 ' have to pass the information across as a whole array' 'In VB6 this is how this was stated to pass along an array value to data structure' 'not <> wrk(1).QTY(j) = qty1(j)' wrk(1).ID = id1 ' have to pass the information across as a whole array' wrk(1).COST = cst1 ' have to pass the information across as a whole array'
There's no requirement to pass the whole array to the variable, although often that is the most convenient way to do it. If you initialise the array correctly you can reference individual elements.
ReDim Wrk(1).COST(19)
ReDim Wrk(1).QTY(19)
ReDim Wrk(1).ID(19)
For J As Int32 = 0 To 19
If Val(Grid1.Rows(J).Cells(1).Value) > 0 Then
Wrk(1).QTY(J) = CInt(Grid1.Rows(J).Cells(1).Value) * 100
Wrk(1).COST(J) = Int(CDbl(Grid1.Rows(J).Cells(3).Value) * 100)
Wrk(1).ID(J) = CInt(Grid1.Rows(J).Cells(4).Value)
End If
Next JWednesday, February 8, 2012 12:58 AM
All replies
-
I actually discovered my own answer on this. I have to define an external array for each arrayed item in the structure and pass it along to the data structure as a complete unit. (I wish this stuff was explained better!) I can manipulate each of the pieces of the array pulled out of the data record but going in MUST be a a complete unit.
sub send_rec ReDim Wrk(0 To 1)
ReDim qty1(0 To 19) ReDim cst1(0 To 19) ReDim id1(0 To 19) For J As Int32 = 0 To 19 If Val(Grid1.Rows(J).Cells(1).Value) > 0 Then qty1(J) = CInt(Grid1.Rows(J).Cells(1).Value) * 100 cst1(J) = Int(CDbl(Grid1.Rows(J).Cells(3).Value) * 100)
id1(J) = CInt(Grid1.Rows(J).Cells(4).Value) End If Next J wrk(1).QTY = qty1 ' have to pass the information across as a whole array'
'In VB6 this is how this was stated to pass along an array value to data structure'
'not <> wrk(1).QTY(j) = qty1(j)' wrk(1).ID = id1 ' have to pass the information across as a whole array' wrk(1).COST = cst1 ' have to pass the information across as a whole array'
- Edited by Rx_Michigan Tuesday, February 7, 2012 11:57 PM
- Marked as answer by Youen Zen Thursday, February 23, 2012 3:23 AM
Tuesday, February 7, 2012 11:44 PM -
sub send_rec ReDim Wrk(0 To 1) ReDim qty1(0 To 19) ReDim cst1(0 To 19) ReDim id1(0 To 19) For J As Int32 = 0 To 19 If Val(Grid1.Rows(J).Cells(1).Value) > 0 Then qty1(J) = CInt(Grid1.Rows(J).Cells(1).Value) * 100 cst1(J) = Int(CDbl(Grid1.Rows(J).Cells(3).Value) * 100) id1(J) = CInt(Grid1.Rows(J).Cells(4).Value) End If Next J wrk(1).QTY = qty1 ' have to pass the information across as a whole array' 'In VB6 this is how this was stated to pass along an array value to data structure' 'not <> wrk(1).QTY(j) = qty1(j)' wrk(1).ID = id1 ' have to pass the information across as a whole array' wrk(1).COST = cst1 ' have to pass the information across as a whole array'
There's no requirement to pass the whole array to the variable, although often that is the most convenient way to do it. If you initialise the array correctly you can reference individual elements.
ReDim Wrk(1).COST(19)
ReDim Wrk(1).QTY(19)
ReDim Wrk(1).ID(19)
For J As Int32 = 0 To 19
If Val(Grid1.Rows(J).Cells(1).Value) > 0 Then
Wrk(1).QTY(J) = CInt(Grid1.Rows(J).Cells(1).Value) * 100
Wrk(1).COST(J) = Int(CDbl(Grid1.Rows(J).Cells(3).Value) * 100)
Wrk(1).ID(J) = CInt(Grid1.Rows(J).Cells(4).Value)
End If
Next JWednesday, February 8, 2012 12:58 AM -
So do you have to redim the fixed array separately or above and beyond rediming the whole array?
I had Redim wrk(0 to 1) at the beginning of the subroutine. That doesn't reset the fixed array portion at the same time? Just trying to understand the difference because redimming the whole structure (or Type ..... End Type) did that in VB6. I don't care which way it is done ...just trying to understand.
Wednesday, February 8, 2012 1:14 PM -
Hi,
Here is an example of Random File Access.>>
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/50d7b930-edd3-4ba4-92fe-2c16e5235e51
Would you like me to try an example with your STRUCTURE?
Try the following just after your ReDim
Work(0) = New Invoice
Work(1) = New Invoice
That should get rid of the error "Object reference not set to an instance of an object."
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Edited by John Anthony Oliver Friday, February 10, 2012 1:49 PM
- Marked as answer by Youen Zen Thursday, February 23, 2012 3:23 AM
Friday, February 10, 2012 1:48 PM -
I had Redim wrk(0 to 1) at the beginning of the subroutine. That doesn't reset the fixed array portion at the same time? Just trying to understand the difference because redimming the whole structure (or Type ..... End Type) did that in VB6. I don't care which way it is done ...just trying to understand.
You have two sets of array - the array of invoices and the arrays within the structure - and both need to be correctly initialised before the array elements can be referenced. You can do this by DIMming the arrays (as per my example) or by setting the array to a new array created for the purpose (as per your solution) but the preferable option in my opinion is to do the initialiation as part of the process of constructing the structure instance, so it happens automatically.
Structure INVOICE
Public NUM As String
<vbfixedstring(2)Public SYMBOL As String
Public FAC_ID As Short
Public CORP_ID As Short
Public DTE As Integer
<vbfixedstring(10)> Public INV_NO As String
<vbfixedstring(10)> Public PO As String
<vbfixedstring(2)> Public FILLER1 As String
Public DELETE As Short
<vbfixedarray(19)> Public ID() As String
<vbfixedarray(19)> Public QTY() As Integer
<vbfixedarray(19)> Public COST() As Integer
<vbfixedstring(1)> Public TRANSFER As String
Public TRAIL As Integer
<vbfixedstring(20)> Public FILLER As String
Public Sub New(ByVal COUNT As Integer)
ReDim ID(COUNT)
ReDim QTY(COUNT)
ReDim COST(COUNT)
End Sub
End Structure
Public WRK() As INVOICE
Class Review
Sub send_rec()
Dim WRK(1)
WRK(1) = New INVOICE(19)
FileClose(4)
FileOpen(4, "D:\myDB\TMP\INVOICE.TMP", OpenMode.Random, , OpenShare.Shared, Len(WRK(1))) PTR = LOF(4) / Len(WRK(1)) PTR = PTR + 1
For j As Integer = 0 To 19
Dim x As Integer = 5 WRK(1).QTY(j) = x
Next j
If ITR > 0 And ITR <= PTR Then
FilePut(4, WRK(1), ITR)
End If
FileClose(4)
End Sub
End Class
- Edited by Acamar Saturday, February 11, 2012 7:08 AM Code formatting
Friday, February 10, 2012 8:57 PM