I am writting a win32 program with VB.NET which need to get some values from Mathematica. I use the sample program in Mathematica and write the code like this:
Imports System
Imports Wolfram.NETLink
Public Class Form1
Public kernel1 As IKernelLink
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
kernel1 = MathLinkFactory.CreateKernelLink()
kernel1.WaitAndDiscardAnswer()
End Sub
Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
kernel1.Evaluate("<< StringConvertTools.x")
kernel1.WaitAndDiscardAnswer()
MsgBox("the data has been loaded successful", MsgBoxStyle.OkOnly, "info")
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim flag1 As Integer
kernel1.Evaluate("1+2")
kernel1.WaitForAnswer()
flag1 = kernel1.GetInteger()
kernel1.Evaluate("XHexceldata[#,True]&/@" + rownumber)
End Sub
End Class
I run the porgram, I will first click the button1, then I will click the button2. But when I click the button2, there will be an erro message "Error code: 3. MLGet out of sequence". I found that the erro is caused by the code flag1 = kernel1.GetInteger()
. I don't know why it can not get the value of "Evaluate("1+2"). And I found that the value of flag1 is always 0. But the sample in the document of .NETLink API here is:
Imports System
Imports Wolfram.NETLink
Public Class SimpleLink
Public Shared Sub Main(ByVal args As String())
' This launches the Mathematica kernel:
Dim ml As IKernelLink = MathLinkFactory.CreateKernelLink()
' Discard the initial InputNamePacket the kernel will send when launched.
ml.WaitAndDiscardAnswer()
' Now compute 2+2 in several different ways.
' The easiest way. Send the computation as a string and get the result in a single call:
Dim result As String = ml.EvaluateToOutputForm("2+2", 0)
Console.WriteLine("2 + 2 = " & result)
' Use Evaluate() instead of EvaluateToXXX() if you want to read the result
' as a native type instead of a string.
ml.Evaluate("2+2")
ml.WaitForAnswer()
Dim intResult As Integer = ml.GetInteger()
Console.WriteLine("2 + 2 = " & intResult)
' You can also get down to the metal by using methods from IMathLink:
ml.PutFunction("EvaluatePacket", 1)
ml.PutFunction("Plus", 2)
ml.Put(2)
ml.Put(2)
ml.EndPacket()
ml.WaitForAnswer()
intResult = ml.GetInteger()
Console.WriteLine("2 + 2 = " & intResult)
'Always Close when done:
ml.Close()
End Sub
End Class
I don't know what's wrong with my code. Can U give me some advice ? Thanks!