How to encode and decode Base64 in VB.NET

Because sometimes you just need a string with ‘normal characters’ instead of funky ones…

Public Function EncodeBase64(input As String) As String
	Return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(input))
End Function

Public Function DecodeBase64(input As String) As String
	Return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(input))
End Function

Have a nice day!

Unix timestamp in VB

Okay, remember these date functions?
Here is another one when asked to submit a date/time as a Unix timestamp…

Calculated in seconds from January 1st, 1970 in Visual Basic.

Public Function fncConvertToUnixTimestamp(datDate As DateTime) As Double
    Dim datOrigin As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
    Dim diff As TimeSpan = datDate.ToUniversalTime() - datOrigin
    Return (Math.Floor(diff.TotalSeconds))
End Function

Have a nice day!

Convert between Local Time and UTC or Zulu time

Here is some visual basic code for you!
You receive a ZULU time and want to store it in your local time?

Dim MyZulu As New Date(year,month,day,hr,min,sec,DateTimeKind.Utc)
Dim MyLocal As DateTime = MyZulu.ToLocalTime()

Or is someone or some API asking you for the next format in return, and all you have is a DateTime with your local time in it?

*UTC + offset as (YYYY-MM-DDThh:mm:ss+hh:mm)
Continue reading Convert between Local Time and UTC or Zulu time

System.NullReferenceException: ‘Object reference not set to an instance of an object.’

A happy error while coding in visual basic, vb.net
It is not the line where you get this error, but something that happened before, that made one of your variables invalid and unusable.

Read this. (@ NullReference Exception — Visual Basic)

Continue reading System.NullReferenceException: ‘Object reference not set to an instance of an object.’

IDE1006 Naming rule violation

Thank you visual studio with your strange default settings, causing the error/warning all throughout my code: “IDE1006 Naming rule violation: These words must begin with upper case characters”.

Go Google, find this, and solve it actually like so:

  • By going to Tools > Options… > Text Editor > Basic > Code Style > Naming
  • Click ‘Manage Naming Styles’, add new style ‘camelCase’ (or whatever else you require), and save.
  • Set the Required Styles to camelCase and save by clicking OK
  • Thank You Microsoft!

Have a nice day!