How do you convert a string into base64 in .net framework 4 - vb.net

So I've been going around the internet looking for a way to convert regular text(string) into base64 string and found many solutions. I'm trying to use:
Dim byt As Byte() = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text)
TextBox2.Text = convert.ToBase64String(byt)
but it end up with an error saying
'ToBase64String' is not a member of 'System.Windows.Forms.Timer'.
What do I do to fix this? Or if there's a better way to code it please help.

Use System.Convert.ToBase64String(byt). Otherwise the timer is picked up as the innermost matching name.
Not the best name for a timer btw.

Related

Documentation for new String.Format syntax in VB.NET ( $ before leading quote mark )

I'm used to the String.Format method in VB.NET:
Dim mystring = String.Format("Today is {0:d}", Now)
Recently, however, I stumbled upon something like this, which is refreshingly shorter:
Dim mystring = $"Today is {Now}"
However, I wasn't able to find documentation for this new syntax. I would like to know how to use format masks with it, if possible.
That is called "string interpolation" so you can find information by searching for that term. That said, format specifiers work the same way as for String.Format:
Dim mystring = $"Today is {Now:d}"

Remove parts of a string in vb.net

Im using the FolderBrowserDialog to pick a path.
It will return for ex. this= C:\Mypath1\Mypath2\DOCS
I would like to remove everything but DOCS.
If i use VBA i would use a InStrRev combined with a left. But now in in VB.net and im not sure how to achieve this, im pretty sure there is something better than my old VBA way?
Anyone there that could help, google failed me.
Try this:
IO.Path.GetFileName("C:\Mypath1\Mypath2\DOCS")
Which returns DOCS.
I am not sure what you want to do. But maybe this can help you get the last part of a string
Dim fullPath As String = " C:\Mypath1\Mypath2\DOCS"
Dim Parts As String() = fullPath.Split("\")
Dim lastPart As String = Parts(Parts.Length - 1)

Determine Number of Lines in a String Read in from an Access Database

I am writing a program in Visual Basic that writes and reads to and from a Microsoft Access Database. When reading from the database, one of the functions that I am trying to perform is to determine the number of lines in a multi-line string that was written to the database and then subsequently read from the database. Here's what I have tried so far with no luck.
Dim stringLines() As String = databaseReader("multilineString").ToString.Split(CChar("Environment.NewLine"))
Dim stringLinesCount As Integer = stringLines.Length
For some reason, this always results in stringLinesCount being equal to one, regardless of how many lines the string has. In this example, I am using Environment.NewLine, but I have tried \n, \r, vbCr, vbLf, and vbCrLf as well, and they all result in a value of one. Since none of these seem to be working, what can I use instead to determine the number of lines?
Edit:
Dim splitCharacters() As Char = {CChar(vbCrLf), CChar(vbCr), CChar(vbLf), CChar(Environment.NewLine), CChar("\n"), CChar("\r")}
Dim stringLines() As String = databaseReader("multilineString").ToString.Split(splitCharacters)
Dim stringLinesCount As Integer = stringLines.Length
Since Chris Dunaway provided the answer that I view as helpful but posted it as a comment, here's what he said:
VB cannot use C# style escape sequences, so CChar("\n") and CChar("\r") is meaningless in VB. Also, calling CChar("Environment.NewLine") is wrong because you are trying to convert the actual string "Environment.NewLine" to a single character, which obviously won't work. You can just use Environment.Newline directly in the call to String.Split.
If Chris decides to post his comment as an answer, please let me know so that I may remove this.

find() like function for vb.net

in C++ there is a function for strings find which has the syntax Str.find(substr) which returns the start index of the substring if it exists. I was wondering if anyone knows of a function that provides the same functionality in VB.Net. I have not been able to find one in my searches thus far.
Dim x as String ="ijoneodfpmwfg"
Dim i as Integer = x.IndexOf("d")
'i will be 6 after execution
You shoul really look at this page for that answer. It will tell you all the things you can do with a string and you may find other answers to questions, before you have to ask them here:
String Class

Extracting Album art from MP3 files using TagLib - Is there a better way write this code?

I'm using Visual Basic 9 (VS2008) and TagLib.
The following code extracts the album art from an MP3 file and displays it in a PictureBox.
Is there a better way to write this code?
Dim file As TagLib.File = TagLib.File.Create(filepath)
If file.Tag.Pictures.Length >= 1 Then
Dim bin As Byte() = DirectCast(file.Tag.Pictures(0).Data.Data, Byte())
PreviewPictureBox.Image = Image.FromStream(New MemoryStream(bin)).GetThumbnailImage(100, 100, Nothing, System.IntPtr.Zero)
End If
At first glance it looks okay to me.
You could add some error handling, for example if TagLib.File.Create() throws an error or returns "Nothing". Also if the Tag property is empty for some reason, an error will be thrown if you'll try to access ".Pictures".
I'm not intimately familiar with TagLib but it doesn't look like there is much of a better way to write this. The only suggestion I can give is that you could reduce the amount of code by taking advantage of type inference. The two variable declarations don't need an explicit type if "Option Infer" is currently on. This doesn't actually change the quality of the code though, it just reduces the amount of it.
Example
Option Infer On
...
Dim file = TagLib.File.Create(filepath)
If file.Tag.Pictures.Length >= 1 Then
Dim bin = DirectCast(file.Tag.Pictures(0).Data.Data, Byte())
PreviewPictureBox.Image = Image.FromStream(New MemoryStream(bin)).GetThumbnailImage(100, 100, Nothing, System.IntPtr.Zero)
End If