bsofttech
asked on
VB.Net Global Arrays Problem
Hello everyone. I am a rusty old VB6 programmer (now working in VS2010 VB) and I am trying to read in a flat file. Parse it, sort it and then spit it back out into multple pieces after the sort.
I have decided that since the files are quite small it would be easiest to read the contents into arrays. After which I I can sort the items and write my individual files back out to disk.
I have attached the code below. At this point my hangup is with creating globally shared arrays and then populating them. Visual studio says I need to create a new instance.
Please help me out.
Imports System
Imports System.IO
Public Class MyCommonStuff
Public Shared partnum() As String
Public Shared parts() As String
Public Shared lineitems As New Integer
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles Button1.Click
Dim lin As String
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("C:\EDI\JDEDI IN")
' Read and display the lines from the file until the end
' of the file is reached.
lin = sr.ReadLine()
txt_Row1.Text = lin
sr.Close()
End Sub
Private Sub btn_Parse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Parse.Click
Dim EDI As String
Dim ident As String
Dim EDIDate As String
Dim Rtran As String
Dim ltextlength As Long
Dim Y As Integer
Dim Z As Integer
If txt_Row1.Text = Nothing Then GoTo lv
EDI = txt_Row1.Text
ltextlength = Len(EDI)
MyCommonStuff.lineitems = (ltextlength - 80) / 80
lbl_length.Text = "Parts in order: " + Str$(MyCommonStuff.lineite ms)
'Parse the front
ident = Mid$(EDI, 13, 12)
txt_CustIdent.Text = ident
EDIDate = Mid$(EDI, 25, 8)
txt_Date.Text = EDIDate
Rtran = Mid$(EDI, 1, 80)
txt_Retran.Text = Rtran
'Parse the rest
Do Until Y = MyCommonStuff.lineitems
Y = Y + 1
Z = Z + 80
MyCommonStuff.partnum(Y) = Mid$(EDI, Z + 1, 12) **** Problem kicks in on this line*****
MyCommonStuff.parts(Y) = Mid$(EDI, Z + 1, 80)
Loop
lbl_LastPart.Text = "Last Part Number Read: " + MyCommonStuff.partnum(Y)
lbl_Complete.Text = "Parse Complete"
lv:
End Sub
Private Sub btn_Upload1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Upload1.Click
Dim linout As String
Dim Y As Integer
Dim Z As Integer
Using sw As StreamWriter = New StreamWriter("C:\EDI\JDEDI IN2")
'Prepare String & set variable
Y = 0
linout = txt_Retran.Text
'Find part numbers to write
Do Until Y = MyCommonStuff.lineitems
Y = Y + 1
linout = linout + MyCommonStuff.parts(Y)
Loop
sw.WriteLine(linout)
End Using
End Sub
End Class
I have decided that since the files are quite small it would be easiest to read the contents into arrays. After which I I can sort the items and write my individual files back out to disk.
I have attached the code below. At this point my hangup is with creating globally shared arrays and then populating them. Visual studio says I need to create a new instance.
Please help me out.
Imports System
Imports System.IO
Public Class MyCommonStuff
Public Shared partnum() As String
Public Shared parts() As String
Public Shared lineitems As New Integer
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles Button1.Click
Dim lin As String
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("C:\EDI\JDEDI
' Read and display the lines from the file until the end
' of the file is reached.
lin = sr.ReadLine()
txt_Row1.Text = lin
sr.Close()
End Sub
Private Sub btn_Parse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Parse.Click
Dim EDI As String
Dim ident As String
Dim EDIDate As String
Dim Rtran As String
Dim ltextlength As Long
Dim Y As Integer
Dim Z As Integer
If txt_Row1.Text = Nothing Then GoTo lv
EDI = txt_Row1.Text
ltextlength = Len(EDI)
MyCommonStuff.lineitems = (ltextlength - 80) / 80
lbl_length.Text = "Parts in order: " + Str$(MyCommonStuff.lineite
'Parse the front
ident = Mid$(EDI, 13, 12)
txt_CustIdent.Text = ident
EDIDate = Mid$(EDI, 25, 8)
txt_Date.Text = EDIDate
Rtran = Mid$(EDI, 1, 80)
txt_Retran.Text = Rtran
'Parse the rest
Do Until Y = MyCommonStuff.lineitems
Y = Y + 1
Z = Z + 80
MyCommonStuff.partnum(Y) = Mid$(EDI, Z + 1, 12) **** Problem kicks in on this line*****
MyCommonStuff.parts(Y) = Mid$(EDI, Z + 1, 80)
Loop
lbl_LastPart.Text = "Last Part Number Read: " + MyCommonStuff.partnum(Y)
lbl_Complete.Text = "Parse Complete"
lv:
End Sub
Private Sub btn_Upload1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Upload1.Click
Dim linout As String
Dim Y As Integer
Dim Z As Integer
Using sw As StreamWriter = New StreamWriter("C:\EDI\JDEDI
'Prepare String & set variable
Y = 0
linout = txt_Retran.Text
'Find part numbers to write
Do Until Y = MyCommonStuff.lineitems
Y = Y + 1
linout = linout + MyCommonStuff.parts(Y)
Loop
sw.WriteLine(linout)
End Using
End Sub
End Class
ASKER
I tried this change and Visual Studio comes back and says classes can not be shared and shows it as an error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
to create a new instance of a class, you have to use the word NEW
e.g
you can not assign the partnum without instantiating the class first
e.g
Dim xString as New MyCommonStuff
xString.partnum = file.readalltext("MyFilePath")
you can not assign the partnum without instantiating the class first
@nepaluz, there is no need to instantiate the class, because partnum is shared.
oh, in my first post (ID: 35210738), change Loop to Next. :-)
ASKER
Zhaolai.
Your solution works. Please tell me one more thing though.
How do I display the last item or a specific item from the array? Here is my current command in that layout. It throws an error.
lbl_LastPart.Text = "Last Part Number Read: " + MyCommonStuff.partnum(Y)
I am trying to show the last item read on the screen. It would be handy to know how to show any item though.
Thanks Much.
Your solution works. Please tell me one more thing though.
How do I display the last item or a specific item from the array? Here is my current command in that layout. It throws an error.
lbl_LastPart.Text = "Last Part Number Read: " + MyCommonStuff.partnum(Y)
I am trying to show the last item read on the screen. It would be handy to know how to show any item though.
Thanks Much.
lbl_LastPart.Text = "Last Part Number Read: " + MyCommonStuff.partnum(MyCo mmonStuff. partnum.Co unt -1)
ASKER
Wish this person was on my staff!
You might find this snippet useful:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileName As String = "C:\Users\Mike\Documents\SomeFile.txt"
Dim lines As New List(Of String)
lines.AddRange(System.IO.File.ReadAllLines(FileName))
Debug.Print("First Line: " & lines(0))
Debug.Print("Last Line: " & lines(lines.Count - 1))
Debug.Print("")
For i As Integer = 0 To lines.Count - 1
Debug.Print(i & ": " & lines(i))
Next
End Sub
Public Shared Class MyCommonStuff
Public Shared partnum() As String
Public Shared parts() As String
Public Shared lineitems As New Integer
End Class