Why won't it work? (array resize)

gazeranco

Well-known member
Joined
Feb 11, 2006
Messages
45
Location
Englandshire!
Programming Experience
Beginner
I have made code to assign the title of an xml page to an array like so:-

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsNothing(rssDetail) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]title = (rssDetail.InnerText)
[/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2] myarray(myitemint)
myarray(myitemint) = (title)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE]

The code is in a for next loop so the integer myitemint is going up each time and so should be assigning a different title to my array as it runs? I then use another for next loop to get the values inside the array but it returns blank messageboxes...

VB.NET:
[SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][SIZE=2] a [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] a = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] (myitemint)
MessageBox.Show(myarray(a))
a = a + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]

Help guys! :)
 
1 more thing.. I know the top loop actually works because if i throw a message box in the top loop like so..

VB.NET:
messagebox.show(title)

Then it displays all the titles of the <ITEM> 's from the feed i select..

Hope you can help! :)
 
ReDim myarray(myitemint)
this will also clear any previous content in array, to preserve do this:

ReDim Preserve myarray(myitemint)
 
Thanks john, i sort of have it working, but for some reason it is taking one news item then skipping one then taking one then skipping one so on so forth :confused:

I can only think that myitemint is incrementing more than once but i only have it typed "+ 1" once so its shud just take all 30 but it only does 15. my brain hurts! :(
 
heres some code :(

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] RefreshToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] RefreshToolStripMenuItem.Click
rssDoc.Load(rssStream)
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] currentRssItem [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] rssItems
[/SIZE][SIZE=2][COLOR=#008000]' TITLE
[/COLOR][/SIZE][SIZE=2]rssDetail = currentRssItem.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"title"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'ReDim titlearray(myitemint - 1)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] myitemint > 30 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]title = (rssDetail.InnerText)
myarray(myitemint) = (title)
myitemint = myitemint + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]title = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' LINK
[/COLOR][/SIZE][SIZE=2]rssDetail = currentRssItem.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"link"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsNothing(rssDetail) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]link = (rssDetail.InnerText)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]link = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'' DESCRIPTION
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'rssDetail = currentRssItem.SelectSingleNode("description")
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'If Not IsNothing(rssDetail) Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' description = rssDetail.InnerText
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' description = ""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'End If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] a [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] a = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 30
MessageBox.Show(myarray(a))
a = a + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
I don't how you other stuff works, but in this sub you iterate for each rssItems every refreshClick but you don't reset myitemint. It also looks like you are only catching the last item every time.
 
The way its supposed to work is a click my refresh button, it takes the title from all the rss items on the xml site (in this case bbcnews) and then puts each title into my array, it sort of works, but its only doing 15 items instead of 30 and its the every other item (title) that its getting, heres some more code ;)

VB.NET:
[SIZE=2][COLOR=#0000ff]
Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Form
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] link [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myrssurl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] title [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rssURL [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = (myrssurl)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myRequest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.WebRequest = System.Net.WebRequest.Create(rssURL)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myitemint [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rssDetail [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#008000]'Dim test As Boolean = IsNothing(rssDetail)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rssDoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rssItems [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNodeList = rssDoc.SelectNodes([/SIZE][SIZE=2][COLOR=#800000]"rss/channel/item"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myResponse [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.WebResponse = myRequest.GetResponse()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rssStream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.IO.Stream = myResponse.GetResponseStream()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] currentRssItem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] description [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myarray(30) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE]
 
:D I found it!!

VB.NET:
For a = 0 To 30 [COLOR=green]'this automatically counts from 0 to 30 by step 1
[/COLOR]MessageBox.Show(myarray(a))
' a = a + 1 '[COLOR=green]comment out this line, you increment a another step here !!!!![/COLOR]
Next
Also try to see what happens to myitemint if you click refresh more than one time...
 
IT WORKS WOOHOO!!

Thanks John ;)

I did not realise that for next would auto increment :)

You asked about clicking refresh, it throws an error which i don't understand :(

see attached...
 

Attachments

  • refresh.JPG
    refresh.JPG
    42.9 KB · Views: 42
Hello World!

I have caught the above error, but not solved it yet, i need to work on something else but im not sure how...

Basically when you click the text going accross i want it to take you to the url, here is what i have in place at the moment...

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] currentRssItem [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] rssItems
[/SIZE][SIZE=2][COLOR=#008000]' TITLE
[/COLOR][/SIZE][SIZE=2]rssDetail = currentRssItem.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"title"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'ReDim titlearray(myitemint - 1)
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] myitemint > 30 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]title = (rssDetail.InnerText)
myarray(myitemint, linkint) = (title)
myitemint = myitemint + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]title = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' LINK
[/COLOR][/SIZE][SIZE=2]rssDetail = currentRssItem.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"link"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsNothing(rssDetail) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]link = (rssDetail.InnerText)
myarray(myitemint, linkint) = (title)
linkint = linkint + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]link = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

I thought that was right but its not working, does anyone know a better way of doing this? Am i working with array in the wrong way? :confused:
 
Double click the linklabel to get to it's LinkClicked event handler, here you would just start the Url as a new process, which will open the webpage in default browser.
It will typically look like this (where theUrl is a string):
VB.NET:
Private Sub LinkLabel1_LinkClicked(sender, e) Handles LinkLabel1.LinkClicked
  Process.Start(theUrl)
End Sub
You don't have to use an array, after loading the xml you got all the items in the global XmlNodeList variable rssItems.
This list can be accessed by index to get a new item to display (and it already also contains the link!).
The list also got a .count property so you will know when you have displayed the last item and can start over.
 
Back
Top