将索引值传递给函数 [英] Passing an Index value to a Function
问题描述
根据主题,这就是我想要做的事情。
我在这里进行实验,所以我知道代码不完美&可能根本不工作,但我正在一点一点地工作,目前我只需要将Index值输入函数
我有一个循环如下......
对于 Ix = 0 myArrayLen - 1
如果 myXml1.Value.Chars(Ix)= Y 然后
myWordData = GetNewsletterHeader(Ix)
MsgBox(myWordData)
结束 如果
下一步
和一个函数如下......
函数 GetNewsletterHeader( ByRef Ix As Int16 ) As String
Dim myXml2 As XmlTextReader = 新 XmlTextReader(myDataFolder + NewsLetter Items.xml)
myItem = 项目 + Ix
while (myXml2.Read())
If myXml2.IsStartElement()< span class =code-keyword>然后
如果 myXml2.Name = myItem 那么
GetNewsletterHeader = myXml2.Value.ToString
结束 如果
结束 如果
循环
结束 功能
它当前抛出异常从字符串转换项目到类型''Double''无效。,大概是因为Ix是指针而不是实际值。 div class =h2_lin>解决方案
1。删除ByRef
并替换为ByVal
。
函数GetNewsletterHeader(ByVal Ix As Int16)As String
2.确保Ix
在For..Next
循环范围内声明为Int16
3.在函数GetNewsLetterHeader
中,添加以下行
将dim myItem作为字符串
4.将作业更改为myItem
改为
myItem =Item& Ix.ToString
5.将以下行添加为包含此代码的Visual Studio文档文件的第一行
Option Strict
6.告诉我们哪一行会出错,哪些是Visual Studio Debugger显示错误中涉及的变量的值。
好的,异常措辞特别好,你不会遇到C#的问题,但问题是与myItem =Item+ Ix
你应该使用myItem = 项目 + Ix .ToString
你没有显示myItem的声明,但我认为它是这样的:
Dim myItem as Double由于编译错误正在谈论关于将字符串常量Item转换为double,以及为什么它不能这样做。由于Ix是Int16,myItem必须是双精度。
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 ...
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 ...
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.
1. Remove theByRef
and replace withByVal
.
Function GetNewsletterHeader(ByVal Ix As Int16) As String
2. Ensure thatIx
is declared as Int16 in the scope of theFor..Next
loop
3. In theFunction GetNewsLetterHeader
, add the following line
Dim myItem as string
4. Change the assignment tomyItem
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.
Ok, the exception is particularly nicely worded and you wouldn''t get the problem with C# but the problem is withmyItem = "Item" + Ix
You should be usingmyItem = "Item" + Ix.ToString
You don''t show your declaration of myItem, but I assume it is something like:
Dim myItem as DoubleSince 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.
这篇关于将索引值传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!