Question assigning an array in a for loop

brtm5

Member
Joined
May 10, 2012
Messages
5
Programming Experience
Beginner
Im trying to assign an array to global array in a for loop. I've spent many hours on this and have come up with nothing that works right.
I would greatly appreciate some help. Here is my code:

Public Class XMLCleanUp
Public splitoff() As String '// array holding the split off data

Public Function colors_Function(ByVal Inputfile() As String, ByVal count As Integer) As String

Dim x As Integer = 0 '//counter for the array
Dim w As Integer = 0 '//counter for the array

Dim aryVariable(x) As String '// array of the <color>
Dim aryText() As String '// set the counter in the string
aryText = Inputfile '// assigning the file to the array that will be written out.

Dim FILE_NAME2 As String = ("C:\colorsplitoff.txt")
Using writer As StreamWriter = New StreamWriter(FILE_NAME2)


For z = 0 To newstr_count - 1

If aryText(z).Trim = "<Color>" Then
Do Until aryText(z).Trim = "</Color>"
aryVariable(x) = (aryText(z), z) ' <----This is where Im trying to assign the array to a public array that is used in other functions
writer.WriteLine(aryText(z), z)
z = z + 1
splitoffcount = splitoffcount + 1
Loop
If aryText(z).Trim = "</Color>" Then
writer.WriteLine(aryText(z), z)
splitoffcount = splitoffcount + 1
End If
End If
Next z


End Using
Return newstr_count
End Function
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

I have had a look at this for about 5 minutes now and to be honest I have got a headache from trying to figure out what you are trying to do with the code that you have posted. Either that, or I have had one beer to many today??

From what I can see you are trying to do something with an array called anyText and then based on the contents of that array, which is potentially an XML structure, you want to write things to a new file?? However, the majority of this code does not make any sense at all and is full of errors of one sort or another.

Therefore, please do take the time to explain in plain English exactly what it is that you are trying to do. Do NOT get into trying to explain your code, just focus on what your Starting Situation is and what your Ultimate End Goal is and I am sure we should be able to help give you some better guidance on how to solve this.

Hope that helps.

Cheers,

Ian
 

brtm5

Member
Joined
May 10, 2012
Messages
5
Programming Experience
Beginner
LOL Thanks Ian.
I guess an explanation would help. OK here is what Im trying to do.

1) I want to read in an XML file
2) copy off the lines from <color> to </color> into and array
example:
<Color>
<Id Name="Black">Def.Color</Id>
<RGB>0,0,0</RGB>
<CMYK>0,0,0,1</CMYK>
</Color>

3) Then I want to take that array and look at each group of <color> to </color> and see if any of them are exactly the same.
4) If there are any duplicates I want to remove them from the array.
5) Then output whats left over to a file.
Hopefully that makes sense
Thanks
Chris
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Yeah, that makes a bit more sense. You could do this using a DataSet to read the XML file, group the Table in that DataSet by a Colour value, then take the first DataRow in each group and import that row to a new DataTable and finally use the resulting DataTable to save a new XML file.

Have a look here:-

Read the XML file into a DataSet. A DataSet is used here to Infer the schema of the DataTable from the XML file:-

VB.NET:
Dim importDS As New DataSet
Dim exportDT As New DataTable("GroupedColors")
 
importDS.ReadXml(Application.StartupPath & "\XMLFile1.xml")
exportDT = importDS.Tables(0).Clone

Now group the colours and save the first row in each group to a new DataTable for Export:-

VB.NET:
Dim myColorGroups = importDS.Tables(0).Rows.Cast(Of DataRow).GroupBy(Function(x) x(1))
 
For Each colorGrp In myColorGroups
  For Each DR In colorGrp.Take(1)
    exportDT.ImportRow(DR)
  Next
Next
 
exportDT.WriteXml(Application.StartupPath & "\XMLFileGrouped.xml")

Hope that helps.

Cheers,

Ian
 

brtm5

Member
Joined
May 10, 2012
Messages
5
Programming Experience
Beginner
Thank you Ian for your help. I kinda see where you're going with this. Unfortunately I'm still a beginner and will have to read up on what you suggested.
In the mean time can you tell me how I can pass an element from one array to a public array so it can be used in other functions.
For some reason I just can get this right.

Here is an example of what Im trying to do

Public GlobalArray() As String

Dim aryText() As String
aryText = Inputfile '// assigning the file to the array that will be written out.

Dim newstr_count As Integer = UBound(aryText)

For z = 0 To newstr_count - 1

If aryText(z).Trim = "copy I'm looking to extract from the array"
GlobalArray = (aryText(z), z) '<------- Here is where I can't figure out how to pass the element of the array to another array
z = z + 1
End if

next z

Thanks Chris
 
Last edited:

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

In the mean time can you tell me how I can pass an element from one array to a public array so it can be used in other functions.

If you want to use an Array, or any other type of object, in another Function or Sub then you do not need to assign the contents of that object to a GLOBAL variable first before it can be used in that other Function or Sub. You would pass the object as an Argument to the Function or Sub so that it becomes a separate Private variable of the same object type within that Function or Sub. So:-

If you have an Array declared in the Load event of a form, i.e:-

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  Dim myFirstArray() As String = {"1", "2", "3", "4", "5", "6", "7"}
End Sub

And you then wanted to use the above Array in another Function or Sub, you would say:-

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  Dim myFirstArray() As String = {"1", "2", "3", "4", "5", "6", "7"}
  'Call a Function passing the myFirstArray variable as an argument
  'so that it can be used in the Function
  Dim bolResult = IterateThroughArray(myFirstArray)
End Sub
 
Private Function IterateThroughArray(ByVal myArray() As String) As Boolean
  For Each strValue In myArray
    MsgBox(strValue)
  Next
  Return True
End Function

Now you know that, you can rewrite the rest of your code to use this concept, however I would like to explain one more thing. Have a look at the array assignment concept you are trying to use i.e:-

VB.NET:
GlobalArray = (aryText(z), z)

There is NO concept that would allow you to do this. If you wanted to assign the full contents of one array to another array, you would say:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myFirstArray() As String = {"1", "2", "3", "4", "5", "6", "7"}
  Dim mySecondArray() As String
 
  mySecondArray = myFirstArray
End Sub

And if you wanted to assign just one ELEMENT of one array to one element of another array, you would say:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myFirstArray() As String = {"1", "2", "3", "4", "5", "6", "7"}
  Dim mySecondArray(myFirstArray.Length - 1) As String
  Dim SingleArrayElement As Integer = 2
 
  mySecondArray(SingleArrayElement) = myFirstArray(SingleArrayElement)
End Sub

Hope that helps.

Cheers,

Ian
 
Top Bottom