Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy contents of one structure to another

Status
Not open for further replies.

KeyserSoze

Programmer
May 18, 2000
79
US
I have two public record structures in my application as follows:

Public Structure Input_Record
<VBFixedString(1770)> Dim Confirm_Buffer As String
End Structure

Public Structure Input_Record_Detail
<VBFixedString(1)> Dim IN_REC_TYPE As String
<VBFixedString(1)> Dim IN_CONFS_TO_PRINT As String
<VBFixedString(8)> Dim IN_ACCT_NUMBER As String
<VBFixedString(1760)> Dim FILLER As String
End Structure


In a routine, I have declared two local structures:

Dim New_Record As Input_Record
Dim New_Record_Detail As Input_Record_Detail


After opening my input file, I perform a read:

FileGet(InputChannel, New_Record)

Is there a way to copy the contents of New_Record over to New_Record_Detail? Is there a way to automatically associate the value of New_Record with New_Record_Detail so that when New_Record changes in value, the value of New_Record_Detail will change as well?

Thanks!


 
Not with structures no. Since structures have no intelligence.

But you could write a function to do it.

Christiaan Baes
Belgium

"My new site" - Me
 
Yes there is a way, but it is kind of roundabout. It turns out that you can declare Events in structures, and you can declare one and have it fire every time the structure receives new data. Change your structure definitions to this:

Public Structure Input_Record
<VBFixedString(1770)> Dim sConfirm_Buffer As String

Public Event NewDataReceived(ByVal Confirm_Buffer As String)

Public Property Confirm_Buffer() As String
Get
Return sConfirm_Buffer
End Get
Set(ByVal Value As String)
sConfirm_Buffer = Value
RaiseEvent NewDataReceived(sConfirm_Buffer)
End Set
End Property
End Structure

Public Structure Input_Record_Detail
<VBFixedString(1)> Dim IN_REC_TYPE As String
<VBFixedString(1)> Dim IN_CONFS_TO_PRINT As String
<VBFixedString(8)> Dim IN_ACCT_NUMBER As String
<VBFixedString(1760)> Dim FILLER As String
End Structure


Then, in your app's code, put an event handler sub:

Private Sub NewDataReceived(ByVal Confirm_Buffer As String)

New_Record_Detail = Nothing

New_Record_Detail = New Input_Record_Detail

New_Record_Detail.IN_REC_TYPE = Confirm_Buffer.Substring(0, 1)
New_Record_Detail.IN_CONFS_TO_PRINT = Confirm_Buffer.Substring(1, 1)
New_Record_Detail.IN_ACCT_NUMBER = Confirm_Buffer.Substring(2, 8)
New_Record_Detail.FILLER = Confirm_Buffer.Substring(10)

'som message boxes for testing
MsgBox(New_Record_Detail.IN_REC_TYPE)
MsgBox(New_Record_Detail.IN_CONFS_TO_PRINT)
MsgBox(New_Record_Detail.IN_ACCT_NUMBER)
MsgBox(New_Record_Detail.FILLER)
End Sub

In Form_Load (or somewhere else appropriate), wire up the structure you declare to the event handler:

'declare these global to the form
Dim New_Record As Input_Record
Dim New_Record_Detail As Input_Record_Detail

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

AddHandler New_Record.NewDataReceived, AddressOf NewDataReceived

End Sub

Finally, here's a little Button_Click event I set up to test this:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim s As String
Dim i As Integer
Dim i2 As Integer

'fill a string with 1770 characters of 0 to 9
For i = 1 To 177
For i2 = 0 To 9
s &= i2.ToString
Next
Next

New_Record.Confirm_Buffer = s

End Sub

Let me know if this does what you want, or if it doesn't.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Jebenson:

Your solution works great. Now, if I were to do it from the point of reading a record from a file, what changes would I have to make? I want to use a FileGet statement to initially get the information and then copy it over to a another area.
 
Is the file binary or text?




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
You can read the text file like this:

Dim f As System.IO.StreamReader

f = New System.IO.StreamReader("C:\Path\to\your\input\file.txt")

'loop through input file, reading one line each pass
While f.Peek <> -1 'read to end of file

New_Record.Confirm_Buffer = f.ReadLine

'do some stuff with your Input_Record_Detail here

End While

f.Close()

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top