I don't need this declaration because it only makes my code big and unreadable.
Is there a way to make Visual Studio (VS) not add it automatically. Every time I remove it, it is added back by VS.
Function DoStuff(Tom As String)
NOT
Function DoStuff(ByVal Tom As String)
It actually makes your code more clear to the non-believers. I don't think they hurt readability of the code, rather the opposite.
Install Visual Studio 2010 Service pack 1. It doesn't insert the ByVal declaration.
Turn off pretty formatting at Tools/Options/Text Editor/Basic/VB Specific/Pretty Listing (reformatting) of code.
Related
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.
I'm trying to reverse engineer a calculator made with visual basic. The original author has this function called on a lot of variables that looks like:
V(Offset)
V(CPX)
V(CAng)
I can't find this anywhere in any of the functions that he wrote. But I also don't think this is a utility function in visual basic. Does anyone know what this means?
My best guess is that this is converting the units of the numbers it is called on to a format that is compatible with other numbers.
As mentioned by GSerg above, V is not a utility function.
Enabling "Pretty listing" in Visual Studio 2010 for VB.NET, causes autoinserting of ByVal in front of all the parameters which have it implicitly.
This is an issue since, working on a big project, when we need to review the code, all this non-changes are highlited in the comparison with the version control base.
Is there any way to have "Pretty listing" to continue formatting the code, but not to touch the rest of the code or not to force explicit ByVal?
(We also use ReSharper, in case this might have some functionality to resolve this issue)
Cannot update to SP1 as it's not been approved on the internal software centre.
I believe the answer is you can't avoid the insertion of ByVal without either turning off Pretty Listing, or upgrading to a more recent version of Visual Studio.
As a workaround, perhaps you can create a version of the unmodified code that has had Pretty Listing applied to it (e.g. cut and paste the entire code) and compare the modified code to that.
This should be a community wiki question.
I've had to work with a lot of vb.net lately, and recently created the following expression.
If If(report.IsPublicReport, False) Then Return True
Now, this is a simple coalesce inside of an if statement, nothing to be scared of. However, when I was scanning the method looking for an error I had made, this line would constantly stop me dead in my tracks. I just couldn't scan it. Every time I had to stop and break it down manually in my head. Largely because I had to stop and figure out what each If was actually doing in the expression.
I have since rewritten the line as
If report.IsPublicReport.GetValueOrDefault() Then Return True
While more verbose, I am finding that this disrupts my train of thought less as I am scanning the code.
This got me thinking,
Is this something other more experienced
VB.net developers are running into?
Are there any other types of
expressions that are largely
avoided, or at least not favored?
Am I just whining about nothing?
As long as you are uncomfortable with VB.NET syntax, I would strongly recommend you use Option Strict On so you catch mistakes like these quicker. The best way is by changing it globally so it is always on by default. Tools + Options, Projects and Solutions, VB Defaults, change Option Strict to "On".
What you're describing is something Scott Hanselman (and probably others) call "Code smell".
It's basically the idea that when you look at a piece of code and something doesn't seem "right" about it. This is not a capability that developers just "have". It's something you develop over time as you read and write more and more code.
It's not just VB either, you'll see plenty of idiomatic constructs in every language that will (or should) make you pause and question what you're looking at.
That Double IF would definitely do it for me
In Vb.NET you can write:
row!FirstName = "Test"
instead of
row("FirstName") = "Test"
A while ago I used to write row!FirstName (you can do that with every item in a collection that can be accessed by a string parameter) because I thought that was a good idea because it looks more statically typed (like person.FirstName as a property) and is shorter.
However I realized that this is not a good idea because after changing "FirstName" to "Name" i often looked for String in the current file which are highlited in Visual Studio (the object!param syntax is not).
Which makes finding them harder.
You can write
Private Sub Form1_Load() Handles MyBase.Load
End Sub
instead of
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
which is fine if you don't need sender or e (I suppose it is just some compiler magic that adds the signature itself) but I refuse to use the shorter way, cause you don't recognize it as an eventhandler at first sight.
Does anyone know how to stop Visual Studio VB.NET editor from changing my beautiful scientific notation numbers into hideous decimal notation?
It seems that this is part of "Pretty Listing" (single checkbox in the options). I'd like to keep the other features of pretty listing, I just don't want to have to stare at 0.0000000000000001 when I could be looking at 1e-16
I don't think there's a way to do it. You could rely on the implicit CDbl() conversion in this situation:
Dim myPrettyNumber As Double = "1E-16"
Or if you just want to be able to read it more easily, add a comment:
Dim myUglyNumber As Double = 0.0000000000000001 ' 1E-16
You may turn pretty listing back on after defining your constants.
Visual Basic won't obfuscate numbers that have already been defined so long as you don't modify the lines that they're on. If you accidentally do modify a line being forced to scientific notation Visual basic will only convert that line to use fixed notation.
Obviously this works best for the declaration of constants or formula's that won't change very often. It is less viable otherwise.
There is an option in VB to turn off "pretty listing":
http://msdn.microsoft.com/en-us/library/vstudio/y0y5th94.aspx