Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As per the Subject, that is what I want to do.

I am experimenting here, so I know the code is not perfect & may not even work at all, but I am working on it bit by bit and at the moment I just need to get the Index value into the Function

I have a Loop as follows ...

VB
For Ix = 0 To myArrayLen - 1
    If myXml1.Value.Chars(Ix) = "Y" Then
        myWordData = GetNewsletterHeader(Ix)
        MsgBox(myWordData)
    End If
Next


and a Function as follows ...

VB
Function GetNewsletterHeader(ByRef Ix As Int16) As String
    Dim myXml2 As XmlTextReader = New XmlTextReader(myDataFolder + "NewsLetter Items.xml")
    myItem = "Item" + Ix
    Do While (myXml2.Read())
        If myXml2.IsStartElement() Then
            If myXml2.Name = myItem Then
                GetNewsletterHeader = myXml2.Value.ToString
            End If
        End If
    Loop
End Function


It currently throws an exception "Conversion from string "Item" to type 'Double' is not valid.", presumably because the Ix is a pointer rather than a real value.
Posted
Comments
CHill60 30-Jan-13 10:21am    
Include your definition of Ix and on which line does the exception get thrown

1. Remove the ByRef and replace with ByVal.
Function GetNewsletterHeader(ByVal Ix As Int16) As String


2. Ensure that Ix is declared as Int16 in the scope of the For..Next loop

3. In the Function GetNewsLetterHeader, add the following line
Dim myItem as string


4. Change the assignment to myItem to
myItem = "Item" & Ix.ToString


5. Add the following line as the very first line of the Visual Studio document file that contains this code
Option Strict


6. Let us know which line gets the error and what the Visual Studio Debugger displays for the values of the variables that are involved in the error.
 
Share this answer
 
v8
Ok, the exception is particularly nicely worded and you wouldn't get the problem with C# but the problem is with
myItem = "Item" + Ix

You should be using
VB
myItem = "Item" + Ix.ToString
 
Share this answer
 
Comments
Gary Heath 30-Jan-13 10:45am    
So simple !!! Thank you CHill60 ...
You don't show your declaration of myItem, but I assume it is something like:
VB
Dim myItem as Double
Since the compiler error is talking about converting a string constant "Item" to a double, and why it can't do it. Since Ix is an Int16, myItem must be a double.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900