github VBAndCs/sVB-Small-Visual-Basic v1.8
Small Visual Basic v1.8.6

latest releases: v2.8, v2.7, v2.6...
2 years ago

What's new in sVB v1.8

  1. sVB made some breaking changes to fix some SB issues:
    a) this funny code compiles in SB:
x = y
y = 1

where y is considered declared because it is assigned in the second line, and it's value will be "" in the first line! This will not compile anymore in sVB :)

b) For loop final and step values can be changes in loop body in SB. For example, this loop is infinite in SB:

n = 5
For i = 1 to n
    TextWindow.RightLine(i)
    n = n + 1
EndIf

But now in sVB, final and step values are immune and can't be changed in loop body, so, the above for loop will print numbers from 1 to 5 and end normally, ignoring changes in n in loop body. This makes sVB consistent with VB6 and VB.NET, and it is also a good optimization, to avoid recalculating final and step expressions in every loop iteration.

c) Now, you can write a loop like this:

For i = "a" to "z"
    TextBox1.AppendLine(i)
Next

This will show the ASCII codes of letters from a to z.

  1. More enhancements in inferred variable types. For example, the For loop iterator\counter is now inferred as Double, and variables that are assigned to Form.AddXX methods are inferred as the type of the created control. In fact this was done already in previous version, but there was no auto-completion support for the variable unless it uses control naming convention. This is not necessary anymore, and auto-completion is supported for any name of the variable.

  2. Introducing basic types naming conventions:

  • str for string variables.
  • dbl for double variables.
  • date for date variables.
  • arr for array variables.

These abbreviations can be use as prefixes or suffixes, but they should be distinguished from the rest of the word, by using _ (like str_name) or an uppercase letter for the next word (like StrName) or followed by a number (like str1), or if use them as a suffix, they should start with an upper case (like "myStr"). These rules will prevent confusing cases such as a variable named strange that starts with str but it is just a part of the word not a prefix, so, it will not be considered of type String, unless you named it strStrange, or strAnge :D.
This feature makes it easy to work with complex expressions , function parameters, and ForEach iteration variables, as sVB can't infer their types directly.

  1. sVB now fully supports working with dates:
  • The Date type provides methods to create dates, read and modify date parts, and add values to them.
  • Date variables can use Date methods as extension methods.
  • You can use date literals directly in code. Ex:
d = #1/31/2022#
TextWindow.WriteLine(d.LongDate)

In the above example, we used the English date format, where the month appears before the day. This is a must as long as you use month number in the # # literals. You can move the caret to the date literal, and the help popup window will show the date value in your system culture.
But if you write the month name, you can put it in any order!

d = #1 Jan 2022#
TextWindow.WriteLine(d.LongDate)

The date literal can also contain the time, like:

d = #12/27/2020 9:10:6 AM#

and if you omit the date part, the today date will be used:

d = #15:10:6.123#
TextWindow.WriteLine(d.LongDate)

note that the ".123" is the milliseconds part, and here we used the 24 hour clock, so, we don't need to use the AM/PM part.
In short: these are the exact same date formats used in VB.NET, and you can review them in MS docs.

  • You can also use TimeSpan literals directly in code, which is a specific feature for sVB that doesn't exist in VB.NET. Ex:
ts = #+1.10:14:30#
TextWindow.WriteLine(ts.TotalHours)

This code will show the result 34.24, as the time span (duration) contains 1 day, 10 hours and about one quarter of an hour, so, the TotalHours property gives us approximately 34.24.
Note that time span literal must start with a + or -, to distinguish it from date literal. The rest of the time span format is similar to VB.NET. It can contain only days, hours, minutes, seconds, and milliseconds. Ex:

ts = #-1000.10:14:30.500#
years = ts.TotalDays / 365
TextWindow.WriteLine(years)

the above negative time span contains approximately -2.74 years. There is no built in TotalMonths nor TotalYears in the Date class, as a month can contain 29, 30, or 31 days, and a year can contain 365 or 366 days, so, it is up to you to do the math according to the rules you see fit tour needs.

  • The Date class contains Add and Subtract methods, but you can do these operations directly using + and -. The trick here is that sVB stores dates and time spans as ticks. A tick is 1 over 10 million of a second (1 second = 10 million ticks). You can get the total ticks of a date or a time span by calling the Date.GetTicks() method, or the Ticks extension property. So, when using math operator with date and time span, sVB treats them as normal numbers. You can even multiply 2 dates but of course the result is meaningless :D.
    The following code show you 2 different ways to subtract 1000 days from the today's date:
date1 = Date.Now - #+1000.0:0#
TextWindow.WriteLine(date1.DateAndTime)

d = Date.Now
TextWindow.WriteLine(d.AddDays(-1000))
  • You can also use comparison operators like >, < and = to compare tow dates or time spans. Ex:
D1 = Date.FromCulture("22/9/2022", "ar-EG")
D2 = Date.Now
If D1.ShortDate = D2.ShortDate Then
   TextWindow.WriteLine("In the present")
ElseIf D1 > D2 Then
   TextWindow.WriteLine("In the future")
Else
   TextWindow.WriteLine("In the past")
EndIf
  • sVB toolbox now has a DatePicker control, to allow the user to select dates from a dropdown calendar. Use the SelectedDate to get or set the selected date in the control, and use the OnSelection event to interact with the user after selection a date from the calendar.
  1. You can change controls font properties from code. Previously, this was only available via the form designer, but now every control has FontName, FontSize, FontBold and FontItalic properties. The auto-completion list will show font names available on your system when you set the value of the FontName.

  2. The TextBox control now has SelectionStart, SelectionLength, SelectedText, and CaretIndex properties.

  3. All controls now have the Tag property to allow you to store additional data related to the control.

Don't miss a new sVB-Small-Visual-Basic release

NewReleases is sending notifications on new releases.