-
Jun 24th, 2013, 02:44 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Unable to update Array/variables
Hey,
I have this lovely class here
vb.net Code:
Public Class data Public Property Name As String Public Property Subname As String Public Property Type As String Public Property parameter() As String Public Property Summary As String End Class
And I use it to get data from an xml with this function here, which was put together with the help from one of the community members on here and worked wonderfully in previous use.
vb.net Code:
Public Function getdata(location As String) As IEnumerable(Of data) Dim doc = XDocument.Load(location) Dim newdata = From n In doc.<UserEx>.<CompletionList>.<member> Select New data With { .Name = n.@name, .Summary = n.<summary>.Value, .Type = .Name.Substring(0, 1) } Return newdata End Function
So the Problem:
Once I get the data I try to update/change some of it using this for loop
vb.net Code:
Dim newdata As IEnumerable(Of data) = getdata(location) Dim temp As Integer = 0 For Each line In newdata temp = line.Summary.IndexOf("(") If temp > 0 Then line.Subname = line.Summary.Substring(0, temp) 'If I output it here "Console.Writeline(line.subname)" then it is right but if I output after the for loop it does not work :\ End If Next
But for some reason when I output it nothing is updated. I just get blanks, any reason why? :S
I have stepped through it and it does go through the if statement and I see it assign a value but nothing is changed when I output everything
Last edited by Crzyrio; Jun 24th, 2013 at 02:48 PM.
-
Jun 24th, 2013, 03:09 PM
#2
Re: Unable to update Array/variables
I assume that you are talking about setting the Subname. If you have stepped through the code and seen that it was updating, and it later doesn't have anything in that field, then you are looking at the wrong object. There really isn't a different explanation that makes sense, so, as odd as it may sound, that has to be it. The question is: Why?
I don't have a specific answer for that, but I notice that you call getData to fill the NewData collection. I'd guess that you may be calling GetData again at some later point such that you end up with a different collection, one that you haven't modified, and are mistaking one for the other, but that's just a guess.
My usual boring signature: Nothing
-
Jun 24th, 2013, 03:18 PM
#3
Thread Starter
Addicted Member
Re: Unable to update Array/variables
Originally Posted by Shaggy Hiker
I assume that you are talking about setting the Subname. If you have stepped through the code and seen that it was updating, and it later doesn't have anything in that field, then you are looking at the wrong object. There really isn't a different explanation that makes sense, so, as odd as it may sound, that has to be it. The question is: Why?
I don't have a specific answer for that, but I notice that you call getData to fill the NewData collection. I'd guess that you may be calling GetData again at some later point such that you end up with a different collection, one that you haven't modified, and are mistaking one for the other, but that's just a guess.
Yup, talking about subname. I am still in a test project and this is the only thing thing in the project so it makes no senses lol.
vb.net Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim newdata As IEnumerable(Of data) = getdata("C:\D&V Electronics Ltd\CurrentVersion\Applications\ScriptPad\EPT125.xml")
Dim temp As Integer = 0
For Each line In newdata
temp = line.Summary.IndexOf("(")
If temp > 0 Then
line.Subname = line.Summary.Substring(0, temp)
End If
temp = temp + 1
Next
For Each line In newdata
Console.WriteLine(line.Subname)
Next
End Sub
When I am stepping through it, it shows that the 'line' is updated but not the newdata collection
-
Jun 24th, 2013, 03:44 PM
#4
Re: Unable to update Array/variables
Oh, that's more interesting. So, in your snippet, you can step through and watch Subname be changed to something new, but then in the next loop you see nothing?
One point I would ask about is what does temp = temp + 1 do? After all, each time through the loop you set temp explicitly to the IndexOf something, so adding 1 to it afterwards doesn't have any impact, so why is that there?
Another question is about the screen shot you show: If you hit the breakpoint on this line:
line.Subname = line.Summary.Substring(0, temp)
Another question would be this: What happens if you turn line into an array of data with .ToArray (or even a list (of Data) with .ToList). Do you get the same behavior? I see that "In-Memory queue", which makes me think that you might be getting delayed execution, so the subname may not be filled, yet. I'm not sure if that IEnumerable would work that way, but it looks like a LINQ remnant, and LINQ definitely works that way. It doesn't have to be actually executed at the time the line is run. That would be removed if you were using an array or a list, so converting to one or the other of those would be a reasonable test.
My usual boring signature: Nothing
-
Jun 24th, 2013, 03:49 PM
#5
Re: Unable to update Array/variables
change this:
Code:
line.Subname = line.Summary.Substring(0, temp)
to this:
Code:
dim tempString as String
tempString = line.Summary.Substring(0, temp)
line.Subname = tempString
put a break point on the
line.Subname = tempString
line... see what tempString value is...
-tg
-
Jun 24th, 2013, 04:01 PM
#6
Thread Starter
Addicted Member
Re: Unable to update Array/variables
Originally Posted by Shaggy Hiker
Oh, that's more interesting. So, in your snippet, you can step through and watch Subname be changed to something new, but then in the next loop you see nothing?
One point I would ask about is what does temp = temp + 1 do? After all, each time through the loop you set temp explicitly to the IndexOf something, so adding 1 to it afterwards doesn't have any impact, so why is that there?
Another question is about the screen shot you show: If you hit the breakpoint on this line:
line.Subname = line.Summary.Substring(0, temp)
Another question would be this: What happens if you turn line into an array of data with .ToArray (or even a list (of Data) with .ToList). Do you get the same behavior? I see that "In-Memory queue", which makes me think that you might be getting delayed execution, so the subname may not be filled, yet. I'm not sure if that IEnumerable would work that way, but it looks like a LINQ remnant, and LINQ definitely works that way. It doesn't have to be actually executed at the time the line is run. That would be removed if you were using an array or a list, so converting to one or the other of those would be a reasonable test.
Thanks for the quick reply ! and yes that is exactly what is happening and I find it quiet weird
The temp = temp + 1 was only there because I forgot to remove it. I was using it before in a for loop, but it shouldn't have effected the way it was working. Removed it now
You were right . putting the data to an array and then processing/updating it works perfectly.
Never knew Linq and IEnumerable did this, something interesting to read up on!
Just one more quick question with regard to LINQ and XML if ya dont mind answering here, else Ill make a new post.
I have am xml file like so, this is with all collapsed
xml Code:
<?xml version="1.0" encoding="utf-8"?>
<UserEx>
<CompletionList>
<CompletionList id="ers" Key="1"Mod="Control">
<CompletionList id="its" Key="2" Mod="Control">
</UserEx>
How would I Call up on just the first list ? "<CompletionList>"
RIght now when I do
vb.net Code:
Dim xdata = From n In doc.<UserEx>.<CompletionList>.<member>
Select New data With {
It brings up data from the other 2 list, and since the first list has no ID, I am unsure how to filter it.
-
Jun 24th, 2013, 04:34 PM
#7
Re: Unable to update Array/variables
I'm no fan of LINQ, so I'm not the one to answer, but since the first list appears to have nothing at all, what would you expect to get back in any case? Would you be looking for nodes that have no associated data? You might be able to stick a .First or .FirstOrDefault onto the end to get back just the first one, but that will only work if the one without data is the first one.
My usual boring signature: Nothing
-
Jun 25th, 2013, 08:05 AM
#8
Thread Starter
Addicted Member
Re: Unable to update Array/variables
Originally Posted by Shaggy Hiker
I'm no fan of LINQ, so I'm not the one to answer, but since the first list appears to have nothing at all, what would you expect to get back in any case? Would you be looking for nodes that have no associated data? You might be able to stick a .First or .FirstOrDefault onto the end to get back just the first one, but that will only work if the one without data is the first one.
Fair enough lol, I am starting to like it.
Sorry should have mentioned it before but all three of those nodes have child nodes under it, with a lot of data. I need to get all of the child nodes under the first one, but with my current statement I get nodes from all three.
-
Jun 25th, 2013, 09:25 AM
#9
Re: Unable to update Array/variables
Hi,
This is a great example of why you should have created a new thread to answer your last question since I had skipped this thread knowing that ShaggyHiker was helping you.
To help out then, here are two ways you can do this:-
Code:
Dim xdata1 As XElement = myDoc.Document.<UserEx>.<CompletionList>.First
Dim xdata2 As XElement = (From n In myDoc.<UserEx>.<CompletionList> Select n).First
This may not be exactly what you are looking for but you should be able to adapt this example to accommodate your needs.
If you ever need to get more than the first Element than you can use the LINQ Method Take. i.e:-
Code:
Dim xdata3 As IEnumerable(Of XElement) = myDoc.Document.<UserEx>.<CompletionList>.Take(TheNumberOfElementsYouNeed)
Hope that helps.
Cheers,
Ian
-
Jun 25th, 2013, 09:56 AM
#10
Thread Starter
Addicted Member
Re: Unable to update Array/variables
Originally Posted by IanRyder
Hi,
This is a great example of why you should have created a new thread to answer your last question since I had skipped this thread knowing that ShaggyHiker was helping you.
To help out then, here are two ways you can do this:-
Code:
Dim xdata1 As XElement = myDoc.Document.<UserEx>.<CompletionList>.First
Dim xdata2 As XElement = (From n In myDoc.<UserEx>.<CompletionList> Select n).First
This may not be exactly what you are looking for but you should be able to adapt this example to accommodate your needs.
If you ever need to get more than the first Element than you can use the LINQ Method Take. i.e:-
Code:
Dim xdata3 As IEnumerable(Of XElement) = myDoc.Document.<UserEx>.<CompletionList>.Take(TheNumberOfElementsYouNeed)
Hope that helps.
Cheers,
Ian
I agree, I should have started a new thread. This is far from original topic as well.
Thanks for the reply, just one last question. This is the first time I am comming across an xml file with multiple names. How would I pull up the child members from the 2nd element? I would usually use
vb.net Code:
Dim xdata = From n In doc.<UserEx>.<CompletionList>.<member> Where n.@name = "ers"
Just to clarify
xml Code:
<?xml version="1.0" encoding="utf-8"?>
<UserEx>
<CompletionList>
<Child nodes here>
<CompletionList id="ers" Key="1"Mod="Control">
<Child nodes here>
<CompletionList id="its" Key="2" Mod="Control">
<Child nodes here>
</UserEx>
I need all the child nodes which fall under
xml Code:
<CompletionList id="ers" Key="1"Mod="Control">
Last edited by Crzyrio; Jun 25th, 2013 at 11:19 AM.
-
Jun 25th, 2013, 12:01 PM
#11
Re: Unable to update Array/variables
Hi,
Well I could suggest:-
Code:
Dim xdata3 As IEnumerable(Of XElement) = myDoc.Document.<UserEx>.<CompletionList>.Skip(1).Take(1)
But this all depends on the whether the structure of the XML file stays the same or not and whether this is the only occurrence of what you are looking to extract. As mentioned, rather that continuing this thread with additional questions I suggest that you follow your own advice, i.e:-
else Ill make a new post.
and then we can get the full picture of what you need to achieve and hopefully help you better.
Cheers,
Ian
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|