Prevent Visual Basic from converting scientific notation constants to fixed notation [duplicate] - vb.net

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

Related

Dsplaying unicode in a Form

After much trial and error ,and with assistance from the community, the ability to use Unicode in a form has not been very successful. So the question is. Is there a method to have the ability to use Unicode within a form ? primarily in textbox control were a currency symbol like the Peso symbol(₱) will appear in a textbox. there are various ways to have it on a worksheet but in a form is an allusive task.
After much trial and error ,and with assistance from the community, the ability to use Unicode in a form has not been very successful.
Says who? :)
Private Sub UserForm_Initialize()
TextBox1.Text = ChrW(8369)
End Sub
And welcome to stackoverflow :)
Among other methods, you could:
copy & paste Unicode into a textbox, or,
or use ChrW to use a specific character like this:
UserForm1.TextBox1 = ChrW(8369) 'puts a ₱ in the text box.
Note that in most cases the VBA Editor (VBE) won't display Unicode (so copy/pasting the above code into your VBE will replace the ₱ with a ?).
By the way CodePoints is a handy site for finding/identifying unicode symbols. Type whatever you're looking for in the search bar, or copy & paste from a website to find out more.
Also, note that all symbols that appear on your system may or or may not render properly on others.

How to change VBA array decimal separator?

I would like to change VBA array decimal separator to dot. I see it as comma. I tried: Application.DecimalSeparator="."
But when I get the value as MyString = matrix(2, 1), the decimal separator in VBA arrays maliciously persists as comma. I am not able to get rid of the pest.
Is there a way to detect which system separator for VBA arrays is used?
VBA uses quite a few bits drawn from various parts of the platform to work out which decimal and thousands separator to use. Application.DecimalSeparator changes a few instances (mostly on the workbook); you can tweak others at the OS level, but even then though you get to a couple of cases where you can't change the settings.
Your best bet is to write a simple function to check which separator your platform uses based on a trial conversion of say 1.2 to a string and see what the second character ends up being. Crude but strangely beautiful.
Armed with that you can force an interchange of . and , as appropriate. Naturally then though you will have to manage all string to number parsing yourself, with some care.
Personally though I think this is epitomises an unnecessary fight with your system settings. Therefore I would leave everything as it is; grin and bear it in other words.
You have to change it in system settings, Excel takes this kind of settings from system.
I have end up with this function which does exactly what I want. Thank you all for answers, comments and hints.
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function

VBA object properties appear in lower case

Sometimes when developing you find that typical property names show up in lower case.
TempVars.Item("abc")
Might appear like this.
TempVars.item("abc")
Or
tbxMyTextbox.Value
Shows up as
tbxMyTextbox.value
The question is why does this happen and how do you fix it?
I've asked myself this question several times and seen others ask it in SO and elsewhere. In most cases the question comes up when searching for answers on other coding errors. Sometimes the developer (me too) wonders if there's something wrong that they're missing that causes this lower case issue.
A while back I ran across an answer (I'll add the original reference when I run across it again) that made the most sense and actually allowed me to correct this nagging issue.
Apparently it happens when you use a variable in lower case that has a name that's the same as a property.
So
Dim value as string
will result in
myObject.Value
appearing as
myObject.value
Solution?
Because VBE apparently considers this variable across the entire IDE -- apparently without regard to scope; the way to revert to the proper case is to temporarily create the variable in upper case and avoid naming variables with the same name as existing properties in your application.
So the solution for the .value / .Value issue above would be to temporarily include the line
Dim Value as string
in a module within your app. Run the code that includes this line.
Afterwards, remove this temporary line of code.
Then as hoped, .value properties will again appear as .Value and not .value.
Of course, we should avoid those reserved words in the first place, but if at some point that wasn't done the above should fix the issue.
Here are some reserved word links:
Microsoft - Access 2002 & later
Allen Browne's bad word list
Reserved Word search tool (various languages)
Thanks to #ScottCraner for the nudge about avoiding reserved words.
Try something like this:
CallByName(myObject, "value", VbGet)

Excel VBA - How to stop auto formatting of scientific numbers to floating in VBA editor

I need to enter a series of equations into a VBA function. The equations contain coefficients in scientific notation and I need to keep this notation to allow easy checking of the VBA script.
However, as soon as I enter a scientific number in the VBA editor and hit Return, the number is automatically converted to a floating number. Although accuracy is not effected, readability suffers tremendously and makes checking very hard.
How can I suppress auto formatting in the VBA 7.0 editor?
For example, if I enter the following line in the VBA editor:
e = 3.472e-4 * d + 2.156e-7
and hit Return I end up seeing the following line
e = 0.0003472 * d + 0.0000002156
I need to somehow stop this auto-formatting nightmare.
You could make a 1-character wrapper for val like:
Function v(s As String) As Double
v = Val(s)
End Function
and then use expressions like:
e = v("3.472e-4") * d + v("2.156e-7")
that might minimize the impact on readability
alternatively -- why not just accept what the editor does but have comments that explain the background formula?
Another approach -- use another editor like Textpad for which VBA syntax highlighting is available (in Textpad's case you need to download a VBA syntax definition file from their website to enable highlighting). You'll lose intellisence but it still has some nice features. I haven't used Textpad for VBA since the built-in editor in Excel is adequate for me, but I have used Textpad extensively for some other languages and find it easy to work with. You could edit code in Textpad (or maybe Notepad++) and copy it over to the VBA editor when you want to test.

VB.NET IDE: Refresh code lines

I am using the Find & Replace function frequently, and I don't care about the case.
So I am replacing e. g.
"Dim s As String = SomeFunc(SomeArg)"
with
"dim s as string = somenewfunc(somearg)"
The IDE replaces it fine, but it uses my lower case typing.
Only when I change something in that line (for example by adding a space at the end of the line)
"dim s as string = somenewfunc(somearg)"
, it becomes
"Dim s As String = SomeNewFunc(SomeArg)"
as it should be.
Does anybody know some magic way to refresh all my lines of code?
Clean and Rebuild did not help me.
Thank you!
A few options:
When you do your replace use the proper case.
You could also do Format Document. Edit --> Advanced --> Format Document or Ctrl E, D But this will only do the current document. Not sure if this will fix your case in the document. Maybe there is a way to do this to all documents, I am not sure. I imagine resharper can do this with code cleanup.
You might want to look at the advice from this link:
How to format all files in Visual Studio 2012?
See Jay Bazuzi's answer.
You will want to tweak his code to use .vb instead of .cs