Using MSscriptControl in VB.net adding an object gives Specified cast is not valid error - vb.net

Here a sample code that produces the error. I have used MSscript in VB projects in the past and those projects are functioning.
The error reported is: "When casting from a number, the value must be a number less than infinity"
Or if anyone has another suggested way to easily add scripting to a project.
Private Sub Run_Script()
Dim scriptEngine As New MSScriptControl.ScriptControl()
Dim TestClass As New Sample
Dim ScriptCode As String
scriptEngine.Language = "VbScript"
scriptEngine.AddObject("Test", TestClass, True)
ScriptCode = "MsgBox ""tests"" "
scriptEngine.AddCode(ScriptCode)
End Sub
End Class
Public Class Sample
Public Sub Test()
MessageBox.Show("This is a test")
End Sub
End Class

I found the answer. I needed to set com visible to true. This is found under "Assembly Information"

Related

Calling a function of a classmodul

probably just a stupid syntax error but when I try to call a function i created in a class module I get the error message that my "objectvarable or withblock is not declarde".
Here the minimal code example from both modules:
'calling
Dim AllZyklen1 As New ArrayList
For Each Wartungsplan In ArrayWartungsplan
Set AllZyklen1 = Wartungsplan.GetAllZyklen 'added set
next Wartungsplan
'function itself
Public Function GetAllZyklen() As ArrayList
Dim AllZyklen2 As New ArrayList
'allZyklen2 gets calculated, no other functions are called just local varaibles of the class are used
If Not AllZyklen2.Contains(Zyklus) Then
AllZyklen2.Add Zyklus
end if
Set GetAllZyklen = AllZyklen2 'added set
End Function
(numbers are added to "allzyklen" just for easier reading, they are actually both called "allzyklen" without number)
Shouldnt that work? I just cant see the error.
EDIT: As for the Solution, what the answer states is absolutley correct and was necessary for my code to work. Unfortunatley I also had an spelling error for a attribute in the classmodule. In which case vba just highlights the call of this function, but no errors within the function... I ended up moving the function from the classmodule to the main module where the correct line with the spelling error got highlighted and the mistake was easier to spot.
You need to use Set for Objects (ArrayList is an object).
So it should be:
'calling
Dim AllZyklen1 As New ArrayList
For Each Wartungsplan In ArrayWartungsplan
Set AllZyklen1 = Wartungsplan.GetAllZyklen
Next Wartungsplan
and
'function itself
Public Function GetAllZyklen() As ArrayList
Dim AllZyklen2 As New ArrayList
'allZyklen2 gets calculated, no other unctions are called just local varaibles of the class are used
Set GetAllZyklen = AllZyklen2
End Function
Full example that works:
Class Module ClassWartungsplan:
Option Explicit
Public Function GetAllZyklen() As ArrayList
Dim AllZyklen2 As New ArrayList
'allZyklen2 gets calculated, no other unctions are called just local varaibles of the class are used
AllZyklen2.Add "abc"
Set GetAllZyklen = AllZyklen2
End Function
Standard Module:
Option Explicit
Sub Example()
Dim AllZyklen1 As New ArrayList
Dim Wartungsplan As New ClassWartungsplan
Set AllZyklen1 = Wartungsplan.GetAllZyklen
Debug.Print AllZyklen1(0) ' prints ABC in the immediate window
End Sub

Create class method with already existing name from a DLL

I come from this post : Create class method with already existing name, where I explain that I need to make code compatible with MS Access and PostgreSQL. My first thought was to create a new class for PostgreSQL support and rewrite the functions previously used. The solution brought could work, but the problem is that the MS Access database class is loaded from a DLL (Microsoft Shared\DAO\dao360.dll, DAO.DBEngine). How can I interface or override those DLL functions and get rid of this "ambiguous name detected" error ?
As said in the answer to your first post you can use the Implements feature to do this. Without further details and yout code it is just more than difficulut to tell where your issue is.
IDatabase could look like that
Option Explicit
Sub OpenDB(fileName As String)
End Sub
Function ReadData(lngNr As Long) As String
End Function
clsAccess like that
Option Explicit
Implements IDatabase
Dim m_Dbs As DAO.Database
Dim m_Rcd As DAO.Recordset
Dim m_Filename As String
Sub IDatabase_OpenDB(fileName As String)
m_Filename = fileName
Set m_Filename = OpenDatabase(m_Filename, , True)
End Sub
Function IDatabase_ReadData(lngNr As Long) As String
Set m_Rcd = m_dbs.OpenRecordset("qryTable", dbOpenSnapshot)
m_Rcd.Move lngNr - 1
IDatabase_ReadData = m_Rcd.Fields("fldName").Value
m_Rcd.Close
End Function
You need to add the references in Tools/References.

Why does Intellisense not work for Default Public Property (sometimes)?

I've had problems with default properties in my code that I can - at least to some extent - reproduce with the following code:
Public Class Dawg
Public Enum DawgEnum
Demo1
Demo2
End Enum
Default Public Property DawgProp(x As DawgEnum) As String
Get
Return "whatever"
End Get
Set(value As String)
'...
End Set
End Property
Public Sub DawgSub()
Me(DawgEnum.Demo1) = "Intellisense works here..."
End Sub
End Class
Public Class DawgTest
Public Sub SomeSub()
Dim d As New Dawg
d(Dawg.DawgEnum.Demo1) = "And here as well..."
End Sub
Public Shared Sub AnotherSub()
Dim d As New Dawg
d(Dawg.DawgEnum.Demo1) = "No help for finding 'Dawg.DawgEnum.Demo1' here..."
d.DawgProp(Dawg.DawgEnum.Demo1) = "...Intellisense helped here..."
End Sub
End Class
The problem being that within the "Shared Sub AnotherSub" when I start typing "d(" Intellisense does not offer me the list of available Enum values for the property.
I also have the problem in other classes that structurally are identical to the class "DawgTest" (a simple public class) where it doesn't work either (within a shared sub or not) - although there are no problems in the test above.
Of course, this isn't a major problem since the code compiles fine and I can always use the explicit property, but it's very annoying and confusing and just like to know, if this is by-design? And, if so: why? If not: how can I fix it?
I can't for sure say, if this problem has been there all along or, if it just came up out of nowhere...
Any thoughts - even 5 cents or less - on the issue would be greatly appreciated :o)

How Do I fill in this Structure String Array in vbNET?

Visual studio tells me the variable must be declared even though it already is.
I filled in a structured array in a similar way using a loop though the type was an Int.
I do not want to use a loop this time just hard code it.
Structure Sentence
Dim strWord As String
End Structure
Dim strArticles(1) As Sentence
strArticles(0).strWord = "The"
Thanks
Are you defining the Structure in your method body? It must be defined outside of a method, either in a module ore a class. See this example.
This works just fine:
Module Module1
Sub Main()
Dim s = New Sample()
s.DoIt()
End Sub
End Module
Class Sample
Structure Sentence
Dim strWord As String
End Structure
Public Sub DoIt()
Dim strArticles(1) As Sentence
strArticles(0).strWord = "The"
Console.WriteLine(strArticles(0).strWord)
End Sub
End Class

Why does my function's name appear twice in the "locals" window?

I have created a Class Module in which I have defined a function. Whenever that function is called, it is listed twice in the locals window. Only the second one's value changes, the first one stays either empty or "zero", depending on its type, until the end of the code's execution. I don't have this problem with functions defined in standard modules. Did I do something wrong, is this a bug, or is there a logical reason behind this?
Contents of the TestClass class module:
Public Value As Double
Function AddFive() As Double
AddFive = Me.Value + 5
End Function
Contents of the standard module:
Sub TestSub()
Dim TestObject As New TestClass
TestObject.Value = 2
MsgBox TestObject.AddFive
End Sub
Here is a screenshot showing that, when the code is executed line-by-line, the function's value is listed twice in the locals window, and only the second value has changed after the function's code was executed.
(link to screenshot)
I'm using VBA for Excel 2010.
Thanks in advance.
The issue is more in how you are doing it. If you have a function that just adds 5 to an internal variable of a class object, then it's technically a void (Sub in VBA) since you don't need a return value.
Your code should be:
CLASS
Public Value As Double
Sub AddFive()
Me.Value = Me.Value + 5
End Sub
MODULE
Sub test()
Dim testObject As New TestClass
testObject.Value = 2
testObject.AddFive
MsgBox testObject.Value
End Sub
I can imagine there could be a number of reasons why there are 2 variables created, but I find it a bit pointless to go into why there is unexpected behavior since you are doing improper code.
If you want, you can even write class function that will show it's value + 5 in a msgbox and this would not create an extra variable either. But that is strange and I think you want the code above. But here it is regardless:
CLASS
Public Value As Double
Sub ShowPlusFive()
MsgBox Me.Value + 5
End Sub
MODULE
Sub test()
Dim testObject As New TestClass
testObject.Value = 2
testObject.ShowPlusFive
End Sub