0

我正在编译我的字符串代码(我从文本文件中读取我的代码)在 vb 中,它工作正常,但我有一个函数返回可为空的双精度(双精度?)

当我这样使用它时

Dim x As Double? = Myfunc(1000) 'it returns Nothing

我的 x 变量没有填充,没关系

但是当我这样使用它时

Dim x = Myfunc(1000) 'it returns Nothing

我的 x 值为 0 !!!!

我该如何解决这个问题我希望我的用户编写像第一个代码块这样的代码

我测试了所有 Option Explicit 和 Option Strict ,但它没有给我带来任何好处。

请告诉我如何使用 Just dim x 而不是 Dim x as (type) 谢谢你的帮助

更新:这是 Myfunc 代码:

   Function Myfunc(parameterId As Long) As Double?

        If parameterId = 1000 Then
               Return Nothing
        Else
            Return tot(parameterId) 'it is a dictionary of values
        End If

    End Function

这是我的编译类:

     Private Shared Function Compile(ByVal vbCode As String) As CompilerResults
        Dim providerOptions = New Dictionary(Of String, String)
        providerOptions.Add("CompilerVersion", "v4.0")

        ' Create the VB.NET compiler.
        Dim vbProv = New VBCodeProvider(providerOptions)
        ' Create parameters to pass to the compiler.
        Dim vbParams = New CompilerParameters()
        ' Add referenced assemblies.



        vbParams.ReferencedAssemblies.Add("mscorlib.dll")
        vbParams.ReferencedAssemblies.Add("System.Core.dll")
        vbParams.ReferencedAssemblies.Add("System.dll")
        vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        vbParams.ReferencedAssemblies.Add("System.Data.dll")
        vbParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
        vbParams.ReferencedAssemblies.Add("System.Xml.dll")
        vbParams.ReferencedAssemblies.Add("System.Xml.Linq.dll")


        vbParams.GenerateExecutable = False
        ' Ensure we generate an assembly in memory and not as a physical file.
        vbParams.GenerateInMemory = True

        ' Compile the code and get the compiler results (contains errors, etc.)
        Return vbProv.CompileAssemblyFromSource(vbParams, vbCode)

    End Function
4

1 回答 1

2

如上所述,Option Infer On需要包含以强制编译器将变量创建为所需类型 - 在这种情况下,Double?MyFunc.

于 2014-07-09T12:59:56.967 回答