Content Archived

This content is no longer current. Our recommendation for up to date content: http://channel9.msdn.com/Series/Visual-Basic-Fundamentals-for-Absolute-Beginners

Understanding and Creating Classes - 14

Sign in to queue

Description

Now that you have a good sampling of basic Visual Basic syntax under your belt, it's time to tackle some of the more challenging topics. Classes are integral to the .NET Framework, particularly the .NET Framework Class Library. As a means of better understanding what classes are -- particularly as utilized in the .NET Framework -- we demonstrate how to create your own custom classes. In this lesson we demonstrate how classes are defined and new instances are created. We demonstrate how to define Properties (using the prop "code snippet" to create auto implemented properties) and how to both set their values and get their values for a given instance of the class as well as creating Methods in our classes. We talk about how the classes you create are really custom dat types and can be used as such in helper method declarations and such.

Download the source code for Understanding and Creating Classes

Embed

Download

The Discussion

  • User profile image
    avv

    hey thx for this

    i have question about the method inside the class

    does it take more memory if i use it in the class camper if its was in the module ?

    well in the module it appear once
    and the class it appear in all the instances i made

  • User profile image
    David N

    This tutorial is excellent!

  • User profile image
    BobTabor

    @avv ... Honestly?  The memory footprint is negligible unless you have a massive massive application.  I would worry about optimizing your application last -- and only make changes once you have determined a baseline of performance and use a profiling tool to determine where the bottle necks are.  Writing your application in a maintainable way should be your first goal.

    But these issues are WAY down the line.  You'll get there ... best wishes!

  • User profile image
    Jay

    I second what David N said, this has really helped me to understand classes properly, as before i knew some of the things involved, but i was a bit confused with all the different key terms used, such as instance, object, etc and what they actually mean.

    Thanks!

  • User profile image
    BobTabor

    @David N: @Jay: Thank you both for your encouraging words.  Glad this worked for you!

  • User profile image
    connor horton

    from all the tutorials i have been watching and i do understand, your tutorials are excellent. i just wanted to ask you, if i wanted to make an application that the user would input there date of birth and then it would tell them how many days, how many years and how many months old they are i have been trying but i don't think i am getting anywhere if you could just give me a general idea of what i have to do, then it would be much appreciated thank you.

  • User profile image
    connor horton

    sorry for post as i have now worked out my application for working out how many days user has been alive for thanks to your tutorials i will copy and paste my code to show what i have done and for you to see if there is any way i can improve my code thank you
    Module Module1

    Sub Main()
    Console.WriteLine("find out how many days you have been alive for: format = 00, 00, 0000 day month year")
    Dim mybirthdate = Console.ReadLine()
    Dim myage = Date.Now.Subtract(mybirthdate)
    Console.WriteLine(myage.TotalDays.ToString())
    Console.ReadLine()
    End Sub

    End Module

  • User profile image
    Antonio Moore

    I'm looking through this tutorial and I'm finding them all very helpful. However, I'm using Visual Studio 2008 and when I go to create my class;
    Public Property Make() As String

    Visual Studio is automatically adding;

    Public Property Make() As String
    Get

    End Get
    Set(ByVal value As String)

    End Set
    End Property

    The code won't work unless I define it all through this. Do I need to make anychanges to the code Visual Studio gives me, or o I leave it as it is?

  • User profile image
    M Fogg

    I have the same oportunity as Antonio.

    What do we need to do to fix this issue?


    Warning 1 Property 'Make' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Documents and Settings\mark_fogg\My Documents\Visual Studio 2008\Projects\SimpleClasses\SimpleClasses\Car.vb 6 9 SimpleClasses

  • User profile image
    mdfogg

    OK, What we needed to do Antonio, is let it fill in the pseudo code.
     It should only give you a warning.
    Everything should then work as advertised.
    At least it does for me. I am also running using Visual Studio 2008.

  • User profile image
    mdfogg

    I installed the free version of Microsoft Visual Basic 2010 Express and it works fine without adding the code snip-its. 

    Otherwise you need to  add them...

    Private _make As String
    Public Property Make() As String
    Get
    Return _make
    End Get
    Set(ByVal value As String)
    _make = value
    End Set
    End Property

  • User profile image
    BobTabor

    @connor horton: Your code looks just fine.  Nice work!

    @Antonio Moore: @M Fogg: Both styles work, and @mdfogg: is absolutely correct in what he suggests.  The code snippets are merely text macros ... they produce a tiny bit of text in a window that happens to be the code editor.  There are similar programs that could be used across all Windows applications ... text expanders ... in this case, it's merely baked into Visual Studio.  Hope that helps!

  • User profile image
    Marc Comia

    Sir Bob i have same problem with Antonio, i use Microsoft Visual studio 2008 but when i type the code in the class.vb
    example

    Public Property Make As String '<--- it shows error then when i hit the return key it should like this.

    Public Property Make() As String
    Get

    End Get
    Set(ByVal value As String)

    End Set
    End Property

    Please help me and explain to me why it is like this. i am a newbie but i am fast learner ^_^ i hope your positive response thank you..

    P.S Sir 10 stars for all of your videos from 1 to 25 keep it up sir God bless you...

  • User profile image
    Marc Comia

    Sir Bob i have same problem with Antonio, i use Microsoft Visual studio 2008 but when i type the code in the class.vb
    example

    Public Property Make As String '<--- it shows error then when i hit the return key it should like this.

    Public Property Make() As String
    Get

    End Get
    Set(ByVal value As String)

    End Set
    End Property

    Please help me and explain to me why it is like this. i am a newbie but i am fast learner ^_^ i hope your positive response thank you..

    P.S Sir 10 stars for all of your videos from 1 to 25 keep it up sir God bless you...

  • User profile image
    moaneibrahim

    Great Video

Add Your 2 Cents