String Addition / Subtraction Function - vb.net

This was a possibility in VB Script by using a script control with the eval function. For example
ScriptControl1.Eval("(10+1.5)") 'Returns 11.5
Is there a way to do this in Vb.Net? The alternative would be to simply split up the string and verify if it is an addition or a subtracting and work from there. I was just wondering if there's already a built in function that I'm not yet aware of.
Thank you

I figured it out. I had to add a COM reference to Microsoft Script control like so:
Then I just declare my new variable as a Script Control.
Dim ScriptControl1 As New MSScriptControl.ScriptControl()
ScriptControl1.Eval("(10+1.5)") ' Returns 11.5
Note*
Make sure your Target CPU is at x86! I would receive a COMException if i had it set to Any CPU.

Related

Variable declaration syntax

Basically I follow a spec to create functions in Access 2010. These functions are in VBA. When working with record sets the given declaration in the spec is
Dim obj.Recordset As New ADODB.Recordset
Yet every time I try and write it I get a syntax error so I just use:
Dim Recordset As object
I am not sure if this means the same thing but it compiles and seems to work fine. Basically my question is, is the given declaration for a recordset correct and is my alternative acceptable. Also Access 2010 is used as a user front end and the database is stored in MS- SQL server 2008 backend.
It looks like you are trying to define a variable with a '.' in the variable name. That is not a valid character in a variable name. If I didn't know better, this syntax looks like you are trying to somehow assign a data type of ADODB.Recordset to a property named 'Recordset' of a class object named 'obj' (which would be extremely bizarre and I don't know of a valid syntax for in VBA or why anyone would want to). I would expect the following will compile:
Dim rst As New ADODB.Recordset
Also make sure you have added the appropriate reference in Tools --> References (Microsoft ActiveX Data Objects 2.0 Library or other latest version). As to your second question, that should be a viable alternative but I prefer the strongly typed former.

vb.net: Jump out of exceptioned function

I am just reworking my VB6 in .NET.
I have a function that is called NonNullString(byval uAny As Object) As String
In VB6 I worked with a sqlite wrapper, and a recordset's member could be accessed by using
Dim sString$
sString = r("somefield")
(without ".Value")
I have really many of these fields, and I changed most of them to ".Value", but for some I forgot it.
An exception is therefore raised in the function NoNullString, and I am looking for a way to quickly jump out of the function in order to see what the caller was and improve the code.
F5 does not do the job.
Does anybody have any ideas?
Thank you!
Press CTRL+L to see call stack. From there you can navigate through the stack.
You can then use Set Next Statement (CTRL+F9) on the End Function of your errored function. Two times F10 to complete execution of this function. Repeat this step till you are in the scope where you think the error originated. Then, if you are on x86 (so you have Edit&Continue available), fix your code, and drag your currently executed line to the moment when this fix would occur. And then try running your function again.
Unfortunately, you cannot Set Next Statement outside of the current block function/sub, which I was going to suggest originally.

Is it possible that staticallly typing my variables would improve script execution time?

I was updating an old .aspx.vb script that was originally written with dynamically typed variables, like this:
Dim price
I updated them to the following format (along with some IsDBNull checks to compensate):
Dim price As String
After doing this to all of my variables, the script appeared to run noticeably faster. Is this a real effect of static typing, or is it just a coincidence?
Yes, it is part of the reason the feature (Dim var As Type) exists in VB in the first place.

VB.NET: Run a method only once in the Application's life time

I'm making a Library type app which needs to scan the whole computer when it is run for the first time. Not again ever. How can I accomplish it?
I'll be using SQL database to store data. So, I can easily make a table there and store a flag and check it on first run, but is there any other way? Any native support for this in VB.NET?
Use either
Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun
or
My.Application.Deployment.IsFirstRun
Edit:
Check this article for additional info.
I found the best way to be to use appsettings..
Using your SQL database seems like a viable solution. You can also write a flag to a settings file on the file system if you don't want to use a table to hold it. This is a pretty good site with some examples on how to write to a file.
http://www.freevbcode.com/ShowCode.asp?ID=4492
EDIT: If you're looking for a way to determine, using some native feature of VB.NET, whether a method has ever been run before (as in, last year), I think you're out of luck. That said, a quick-and-dirty approach might be to define a method to query the database for the flag you've referred to and store the result of that method in a static flag.
Public Sub MethodToRunOnlyOnce()
' this flag will maintain its value between method calls '
' in the same session '
Static methodAlreadyRun As Boolean = MethodHasBeenRun()
If methodAlreadyRun Then
Exit Sub
End If
Try
' ... code ... '
Finally
MethodToSetDatabaseFlag()
methodAlreadyRun = True
End Try
End Sub
Private Sub MethodToSetDatabaseFlag()
' code here to set Db flag '
End Sub
Private Function MethodHasBeenRun() As Boolean
' code here to check Db flag '
End Function
You could always store this flag in an xml file or in the registry so it's on the PC. If you store it in a database and there are multiple copies of this program running on different PCs, you would have to identify them in the DB somehow, whereas if you keep track of it locally you don't need to worry about it.

Detecting Installed Excel Version (and Service Packs)

I need to be able to detect which version of Excel I have installed in my machine from some .NET code I'm developing. I'm currently using Application.Version for that, but it doesn't give me information about Service Packs.
I would preferably to steer away from something like this:
http://www.mvps.org/access/api/api0065.htm
Managed code welcomed!
Public Shared Function GetExcelVersion() As Integer
Dim excel As Object = Nothing
Dim ver As Integer = 0
Dim build As Integer
Try
excel = CreateObject("Excel.Application")
ver = excel.Version
build = excel.Build
Catch ex As Exception
'Continue to finally sttmt
Finally
Try
Marshal.ReleaseComObject(excel)
Catch
End Try
GC.Collect()
End Try
Return ver
End Function
Returns 0 if excel not found.
Unfortunately, that approach is the only reliable approach. Even Microsoft suggests using a similar technique (this is for checking manually, but the concept is identical).
If you want to do this in managed code, I'd suggest just porting the code from your link, and making a class that's easily extensible when new service packs are released.
You could check the app paths in the registry for the path to the exe and then get its version:
See http://www.codeproject.com/KB/office/getting_office_version.aspx
While not robust, that approach is the only way I know of.
Keep in mind you don't have to check for an exact match. You can use comparisons on the individual values to see if the version you have is for example, SP1 or newer. you know it's newer if the version number is greater than or equal to "11.0.6355.0" (you'll need to implement the comparison)