Random integer in VB.NET

As has been pointed out many times, the suggestion to write code like this is problematic: Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer Dim Generator As System.Random = New System.Random() Return Generator.Next(Min, Max) End Function The reason is that the constructor for the Random class provides a default seed based … Read more

Differences Between vbLf, vbCrLf & vbCr Constants

Constant Value Description —————————————————————- vbCr Chr(13) Carriage return vbCrLf Chr(13) & Chr(10) Carriage return–linefeed combination vbLf Chr(10) Line feed vbCr : – return to line beginning Represents a carriage-return character for print and display functions. vbCrLf : – similar to pressing Enter Represents a carriage-return character combined with a linefeed character for print and display … Read more

(OrElse and Or) and (AndAlso and And) – When to use?

Or/And will always evaluate both1 the expressions and then return a result. They are not short-circuiting. OrElse/AndAlso are short-circuiting. The right expression is only evaluated if the outcome cannot be determined from the evaluation of the left expression alone. (That means: OrElse will only evaluate the right expression if the left expression is false, and … Read more

.NET Core doesn’t know about Windows 1252, how to fix?

To do this, you need to register the CodePagesEncodingProvider instance from the System.Text.Encoding.CodePages package. To do that, install the System.Text.Encoding.CodePages package: dotnet add package System.Text.Encoding.CodePages Then (after implicitly or explicitly running dotnet restore) you can call: Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var enc1252 = Encoding.GetEncoding(1252); Alternatively, if you only need that one code page, you can get it directly, … Read more

Hidden Features of VB.NET?

The Exception When clause is largely unknown. Consider this: Public Sub Login(host as string, user as String, password as string, _ Optional bRetry as Boolean = False) Try ssh.Connect(host, user, password) Catch ex as TimeoutException When Not bRetry ”//Try again, but only once. Login(host, user, password, True) Catch ex as TimeoutException ”//Log exception End Try … Read more

Classes vs. Modules in VB.NET

Modules are VB counterparts to C# static classes. When your class is designed solely for helper functions and extension methods and you don’t want to allow inheritance and instantiation, you use a Module. By the way, using Module is not really subjective and it’s not deprecated. Indeed you must use a Module when it’s appropriate. … Read more