Visual Basic lack of parenthesis for functions identification - vb.net

Any way to have any sort of identification for the lack of parenthesis for functions with no parameters at VB?
I have found this answer, but since it is 9 year old, i hope a solution exists now.
I am using Visual studio 2017. I have strict mode on. I have checked format settings available for Visual Studio and found no relative option. I have checked project settings as well.
Edit:
I have noticed that when i type a new function name that it does add parenthesis, could this be triggered somehow?

You are not going to find what you need built into the compiler or project settings, because the optional parenthesis are build into the VB.Net language specification and the compiler will compile according to that specification. Now one thing you might want to consider is running your code through a VB.Net to C# Converter, then running that converted code through a C# to VB.Net converter. For example, consider this VB.Net code:
Sub Main
Foo
End Main
If you run this through Telerik's code converter, it produces this C# code:
public void Main()
{
Foo();
}
Now if you take that C# code and convert it back into VB.Net using Telerik's code Converter, it produces this Vb.Net code:
Public Sub Main()
Foo()
End Sub
Notice how it added the parenthesis to the Foo method. This technique might work for you, if your code isn't too complex.

Related

Why does Imports System not give access to Windows namespace in vbc.exe?

I manage an application that allows the users to automate tasks by writing their own VB code. The user code is compiled using the VBCodeProvider and invoked against the running instance of the application. We've been doing this for a few years now starting with .NET 2.
Traditionally, we have imported the System namespace in the compiler settings so users wouldn't have to write System. all the time. When we went to .NET 4, however, we found that statements like Windows.Forms.Form wouldn't compile anymore. The error was "Type 'Windows.Forms.Form' is not defined." This is odd because other namespaces work. IO.Stream and Reflection.Assembly do not have an error without the System at the beginning.
I've created a simple example. I've put the below code into a file. Then I compiled this file with vbc.exe from both the .NET2 and .NET4 directories. The 2 version works fine. The 4 version will not compile unless you comment out the variable f2.
Imports System
Public Class MyClassName
Public Shared Sub Main
'this works in v2 and v4
Dim f As New System.Windows.Forms.Form
f.ShowDialog
'this does not work in v4
Dim f2 As New Windows.Forms.Form
f2.ShowDialog
End Sub
End Class
Does anyone know how to get this to compile in vbc.exe version 4? And before you say "Just tell the users to type System.Windows.Forms" I will agree that it would be great if they would do that, but users do what users do and I have to work it out.
UPDATE:
I've found that the Windows.Foundation.Diagnostics namespace is causing a collision with the abbreviated use of Window.Forms. Is there any way to hide this namespace from my compilation? Visual Studio 2010 does not have the same conflict, so it must be getting around it somehow.

Using StatePrinter from VB rather than C# to implement ToString

I'm trying to follow the promising suggestion posted here to try StatePrinter as a shortcut to rolling my own ToString methods. I agree with the OP that it is a shame that VS still can't generate this method for me.
I've got a fairly large project, in VS2015 (Community Edition), with both VB and C# code. I added the current stable version of StatePrinter using NuGet.
I can make the example code from the SO answer work fine in my C# code but when I do what I think is the equivalent in my VB code:
Private Shared sp As StatePrinter.Stateprinter = New StatePrinter.Stateprinter
Public Overrides Function ToString() As String
Return sp.PrintObject(Me)
End Function
I just get the compiler error
'Stateprinter' is ambiguous in the namespace 'StatePrinter'
There IS another constructor, StatePrinter (note difference in capitalization only) which is deprecated and, in any case, generates the same error message.
I'm led to the unfortunate conclusions that
VB in VS2015 is acting as if it is case insensitive. Can that be true?
No one else is using StatePrinter from VB.
Can anyone provide any suggestions on how to use StatePrinter from VB? I'm willing to believe I'm making some rather brain-dead mistake in converting the C# example to VB.
It is near impossible to use this directly in VB and get around the ambiguous name issue. You could write a class library wrapper in C# that doesn't expose this mismatch (that is, it has an internal StatePrinter object and exposes constructors that are PascalCased the same.
Another option would be to use reflection in the VB project to get around the case insensitivity.
You could also create a GitHub issue. Or, be a contributor to the project and create a suggested fix for it. :)
As soon as I got done writing #1 in the question above, I was able to figure out how to search for the answer to that bit.
Yes, VB is case insensitive, at least, as far as it needs to be in this case:
See the rather nice writeup here:
https://stackoverflow.com/a/2301980/165164
So, we're left with the rather plaintive: is no one else using StatePrinter from VB?

How can I easily call an IronPython function from VB.net?

I am struggling with this program which uses emgucv(an opencv wrapper for .net) for about 2 weeks. The problem is unfortunately not programming, but setting up emgucv in such a way that it works. I didn't manage to do so for vb.net so I tried doing it for ironpython(because I know python too). Emgucv seems to work perfectly when using ironpython, so I created a function that takes an image as an argument and analyses it in the way I want, returning another image with the results in it. The problem is I want to call this function, giving it the image argument(it could be a string containing the path) from within VB.net and become another string containing the result image as return. I later plan to package that project in a setup so I can redistribute it.
So I am asking you guys: Do you know an easy way to call an IronPython function in VB.net in such a way so I can also package the whole project and redistribute it to people?
Thank you so much for reading this and it would be great if you could also help me with my problem! :)
While IronPython is not my expertise I am well versed in EMGU and its applications. If you insist in using IronPython the following website clearly shows how to pass a string to an IronPython Class.
The following code is taken from the link and is not my own:
Option Explicit On
Option Strict On
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types
Module Module1
Sub Main()
Dim helloWorld As New HelloWorldVB()
Console.WriteLine(helloWorld.HelloWorld("Maurice"))
Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld("Maurice"))
Console.ReadLine()
End Sub
End Module
I would follow the tutorial from the link but the important code is bellow as this imports the require runtime information for IronPython:
**Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types**
&
**Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld("Maurice"))**
Obviously Console.WriteLine(helloWorld.HelloWorld("Maurice")) would be corrected to:
Dim result_location As String = helloWorld.HelloWorld("Maurice")
Where "Maurice" would be the string containing your image location.
Now I have to ask about the problems you were having setting up EMGU in visual studio I know it can be frustrating to do especially to people who are new to it. If you would like I would be happy to help you set it up properly. The reason I ask is since you are providing this to and end user your code could be more efficient without calling IronPython. Especially since each process will require reading and writing from the hard drive.
To start: I will assume that you have included References to Emgu.CV, Emgu.CV.UI, and EMGU.Util in your project. But it is essential that you add "opencv_core220.dll", "opencv_imgproc220.dll" files directly to your project and ensure in the properties window that the 'Copy to Output' option is set to "Copy always". If it isn't you will get errors of not a having the image in the right format etc. You only really need these two .dll to read images in and access the data etc you may need others for .avi movies for example. Note that these two .dll must now be distributed with your project for it to work.
To note this may change depending if your using a 64 bit machine or a 32 bit machine but the 64 bit EMGU version will not run on X86 machines. You must also ensure that your target platform is correct in Visual Studio.
I hope this helps you,
Cheers
Chris

Enforcing using the class name whenever a shared member is accessed

We have a coding standard that says all shared (static) fields and methods must be called with the class name. E.g.
NameOfClass.whatever
Rather then
whatever
Is there a tool that we can use to check this is in fact the case?
(Likewise for modules)
Sorry I should have make it clearer we are using VB.NET.
This is a bigger example of what I mean.
Public Class Class1
Public Shared Sub SharedMethod()
End Sub
Public Shared sharedField As Integer
Public Sub NotSharedMethod()
'this next line shold be written as Class1.SharedMethod
SharedMethod()
'this next line shold be written as Class1.sharedField
sharedField = 5
End Sub
End Class
see also What StyleCop like tools are there for VB.NET
Yes,
Use StyleCop and write your custom rule to do your check.
Here's a reference to check how to write custom-rule for StyleCop.
Sure, just create a custom rule in StyleCop. Then incorporate the use of StyleCop into your automated build process.
Sorry, I did not even realize that StyleCop did not have a VB.NET version. What you need is a static analysis tool for VB.NET. Based on this thread, it looks like Project Analyzer is an option. Unfortunately it's not free.
From the web site:
Maintain. To help maintenance, Project Analyzer lets you enforce coding standards[.]
Whatever tool you use, be sure it incorporate it into your automated build process.
In the Project Properties > Compile, you can set the Warning configuration for "Instance Variable access shared member" to Error and it should generate a compiler error instead of a warning.
I'm not sure how you might do it for all projects. You could change the project template to include that option for all new projects.
Sorry I never did find a good tools for this.
You might be able to use FxCop for this purpose, and write a custom rule. Here is a good site that explains how to write custom FxCop rules.

Is there a library out there to analyze VB.Net code complexity from my C# code?

Given VB.Net code in a string, is there a library (or a command line tool) out there that could calculate Cyclomatic Complextiy and LOC?
This has to be done within my C# code.
Thanks.
There is Refactor!, which does supply some extensibility and also supplys the mesurements (And an extesibility point)
Besides that, there is also NDepend, which allows you to query your code for such infos:
http://www.ndepend.com/Features.aspx