Tip #2 - Visual Basic .NET programming tutorial one - Variables - vbNETtutorialshow

Joined
May 4, 2012
Messages
10
Programming Experience
Beginner
#2 - Visual Basic .NET programming tutorial one - Variables - vbNETtutorialshow

[video=youtube;b4-MXu7hqs8]http://www.youtube.com/watch?v=b4-MXu7hqs8[/video]

Video 2 of my new series
 
I highly do not recommend this video, this is a terrible way to introduce people to declaring and using variables.

Within the first 3 minutes you've done 2 things that would fail if Option Strict was turned ON, I'm not saying that you should explain what Option Strict even is in your video but you should be aware that if option strict is turned on then you should be showing in your video that when you declare a class-level variable you should use the 'Private' keyword and not 'Dim', Dim should only be used to declare a variable in a Sub/Function (which included events, like a button's click event).

Also this is absolutely terrible:
Dim intMyNumber As Integer = "5"

Why is that terrible, you're declaring an Integer variable and you're assinging it a string. "5" and 5 are not the same "5" is a string, 5 is not, a much better example would be:
Private intMyNumber As Integer = 5

I've also noticed twice now that you're using the vb6 compatibility MsgBox() in these first two videos, while there's nothing wrong with using it, it's much more preferred to use MessageBox.Show() and the reason for that is because the MessageBox class is in the System.Windows.Forms namespace with all of the other winforms controls, also if a vb project where to ever be ported over to another .Net language (like C#) all of the MsgBox() calls would have to be changed to MessageBox.Show() anyways because C# does not have access to the Microsoft.VisualBasic.Compatibility namespace and thus can't use the vb6 legacy support objects either. A 3rd reason is if someone who knows C# already and is using your videos as a starting point to learning vb.net wouldn't have to care about MsgBox because MessageBox.Show() is available in both languages. In all of the Microsoft code examples I've seen (both C# and VB.Net examples) they use MessageBox.Show() as the .Net "standard" messagebox.

MsgBox() was only added for the app's the people (unfortunately people do this) use the vb6 conversion wizard to port their vb6 apps to .Net apps instead of re-writing them like they should.

Not trying to discourage the creating of instructional tutorial videos, but if you're going to make one at least try to set a good example.
 
I understand mate! I am 16 years old and taught myself Visual Basic, I am also currently in college learning C++.

The reason I posted on this forum was to learn more and to get feedback from people such as you, maybe this forum is more advanced than I expected however, new people have found my tutorials awesome and easy to follow, thank you for the feedback and I will take that into account.

I have always used MsgBox() because I have watched people on YouTube on VB.NET lessons, also using it, I just assumed it was the best to use.

I will take your feedback into account and would like to say thanks alot, if you could provide me with any other tips, feel free to private message me, I would love to become more advanced and learn more about Visual Basic!

-Joe.
 
I understand mate! I am 16 years old and taught myself Visual Basic, I am also currently in college learning C++.

The reason I posted on this forum was to learn more and to get feedback from people such as you, maybe this forum is more advanced than I expected however, new people have found my tutorials awesome and easy to follow, thank you for the feedback and I will take that into account.

I have always used MsgBox() because I have watched people on YouTube on VB.NET lessons, also using it, I just assumed it was the best to use.

I will take your feedback into account and would like to say thanks alot, if you could provide me with any other tips, feel free to private message me, I would love to become more advanced and learn more about Visual Basic!

-Joe.
Well the purpose of this site is so everyone (beginners and experts alike) can learn from each other. Everyone has to start learning somewhere & I don't mean to discourage instructional videos from being made, I think it's fantastic to see some of those being posted on this forum, but with the enormous amounts of "beginners" and "intro to vb.net" videos blatantly starting off beginners with what's considered "bad practices" is getting to be a bit ridiculous, in my opinion and it would be nice to start seeing a few intro to vb.net videos where some good examples are used, like using a scope declaration variable at the class level (form level) instead of using dim, things like:
Public Class Form1
  Private MyNumberInteger As Integer = 1

  Private Sub MessageButton_Click (...) Handles MessageButton.Click
    If MyNumberInteger > 0 Then
        MessageBox.Show("Number is acceptable", "Good Number")
    Else
        MessageBox.Show("Number is 0 or less", "Not Good Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End if
  End Sub
End Class


And if you have any code that you would like reviewed, by all means, create a thread for it. Just don't post an entire project for review, it's doubleful you'll get too many people with enough time to go through a whole project, but if you have a single routine, or a not-a-lot of code form you'd like reviewed we're all here to help each other out.
 
Thank you so much for being so helpful!

Yes I see now to use Private instead and I wasn't sure whether I was meant to put "1" or 1 but my gut feeling thought it was "1", but now for future practices I understand now, maybe tell me why it still understands "1" even if it's a string when you are declaring it as an integer?

EDIT, I receive an error when I try to use MessageBox.show, have a look.

The code I used:
VB.NET:
    Private intMyNumber As integer = 1

    Private Sub MsgBoxBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MsgBoxBtn.Click
        MessageBox.Show("Your number is: " & intMyNumber & ".", MessageBoxButtons.OK, MessageBoxIcon.Information)


    End Sub

and the error I recieved:

"Error 1 Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion:
'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String) As System.Windows.Forms.DialogResult': Argument matching parameter 'owner' narrows from 'String' to 'System.Windows.Forms.IWin32Window'.
'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String) As System.Windows.Forms.DialogResult': Argument matching parameter 'text' narrows from 'System.Windows.Forms.MessageBoxButtons' to 'String'.
'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String) As System.Windows.Forms.DialogResult': Argument matching parameter 'caption' narrows from 'System.Windows.Forms.MessageBoxIcon' to 'String'.
'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'caption' narrows from 'System.Windows.Forms.MessageBoxButtons' to 'String'.
'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'System.Windows.Forms.MessageBoxIcon' to 'System.Windows.Forms.MessageBoxButtons'. c:\users\joegrech\documents\visual studio 2010\Projects\Variables Tutorial\Variables Tutorial\Main.vb 6 9 Variables Tutorial
"

Please help!


-Joe
 
Last edited:
Thank you so much for being so helpful!

Yes I see now to use Private instead and I wasn't sure whether I was meant to put "1" or 1 but my gut feeling thought it was "1", but now for future practices I understand now, maybe tell me why it still understands "1" even if it's a string when you are declaring it as an integer?

EDIT, I receive an error when I try to use MessageBox.show, have a look.

The code I used:
VB.NET:
    Private intMyNumber As integer = 1

    Private Sub MsgBoxBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MsgBoxBtn.Click
        MessageBox.Show("Your number is: " & intMyNumber & ".", MessageBoxButtons.OK, MessageBoxIcon.Information)


    End Sub

and the error I recieved:

"Error 1 Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion:
'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String) As System.Windows.Forms.DialogResult': Argument matching parameter 'owner' narrows from 'String' to 'System.Windows.Forms.IWin32Window'.
'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String) As System.Windows.Forms.DialogResult': Argument matching parameter 'text' narrows from 'System.Windows.Forms.MessageBoxButtons' to 'String'.
'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String) As System.Windows.Forms.DialogResult': Argument matching parameter 'caption' narrows from 'System.Windows.Forms.MessageBoxIcon' to 'String'.
'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'caption' narrows from 'System.Windows.Forms.MessageBoxButtons' to 'String'.
'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'System.Windows.Forms.MessageBoxIcon' to 'System.Windows.Forms.MessageBoxButtons'. c:\users\joegrech\documents\visual studio 2010\Projects\Variables Tutorial\Variables Tutorial\Main.vb 6 9 Variables Tutorial
"

Please help!


-Joe
There's a couple of things going on here.

First, when you do this:
VB.NET:
Private MyInt As Integer = "1"
and you have Option Strict OFF (not recommended, imo), what the compiler does is it implicitly (if it can) the string "1" into an integer 1, which is successful so that's why it works. When you turn Option String ON, the compiler sees you trying to assign a string to an integer directly and Strict ON disallows (disables) the implicit conversion and throws a compile error stating that you need to either assign an integer directly (the "1" without the quotes is an integer, all numbers without a decimal is an integer by default, all numbers with a decimal are a double by default and anything inside double quotes is always a string) or convert or cast the string to an integer.
So, with option strict ON you can do this:
VB.NET:
Private MyInt As Integer = 1
or
VB.NET:
Pricate MyInt As Integer = Integer.Parse("1") 'This is obviously dumb to do in this example, but it will work with Strict ON

Ok, the second thing happening is MsgBox() and MessageBox.Show() have different parameter signatures:

MsgBox() has ("Main message", Buttons & Image enum, "Title")
MessageBox.Show() has ("Main Message", "Title", Buttons Enum, Image Enum)

So
VB.NET:
MsgBox("Number is acceptable", vbOkOnly, "Good Number")
Would be:
VB.NET:
MessageBox.Show("Number is acceptable", "Good Number", MessageBoxButtons.OK)


Third
When you use a non-string variable in a string, you need to convert it to a string as part of the concatenation, luckily for you every object in .Net has a ToString() method, which converts whatever it is into a String, so this:
VB.NET:
 MessageBox.Show("Your number is: " & intMyNumber & ".", MessageBoxButtons.OK, MessageBoxIcon.Information)
Can become this:
VB.NET:
 MessageBox.Show("Your number is: " & intMyNumber.ToString & ".", MessageBoxButtons.OK, MessageBoxIcon.Information)

Alternatively you can use the String.Format() function if you're wanting to get into something a little more flexible (but it is slightly more complicated to understand at first)
As an example you can do this:
VB.NET:
 MessageBox.Show(String.Format("Your number is: {0}.", intMyNumber), MessageBoxButtons.OK, MessageBoxIcon.Information)
When you pass the "intMyNumber" variable into the String.Format() call, it replaces the "{0}" automatically with this intMyNumber.ToString() for you. You can have multiple of those too, but I'll let you read up on String.Format() on your own first, if you have any questions trying it out, be sure to post your attempt code too.
 
Back
Top