Results 1 to 8 of 8

Thread: [RESOLVED] Derive from the generic List class

Threaded View

  1. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Derive from the generic List class

    Quote Originally Posted by Sitten Spynne View Post
    Everything above this quote is correct:


    Let's talk about this part:

    The word "instantiate" is the weird one there. Let's talk about it.

    If you declare a variable without assigning it a value, it doesn't have a value. For classes, that means it will hold the value "Nothing". Sort of. So if you wrote this line in the Main module, then 'basket' would be Nothing:
    Code:
    Friend basket As GroceryBasket
    See how that doesn't have the New keyword? If you don't use New, you aren't creating anything.

    To actually create a GroceryBasket, you have to use the New Keyword. The long way to write that out would look like:
    Code:
    Friend basket As GroceryBasket = New GroceryBasket()
    This is tedious, and we very often want to instantiate at the same time we create a variable, so you can take a shortcut:
    Code:
    Friend basket As New GroceryBasket()
    To really get the sense of it, think about a 3D printer. Let's say I want to print a cute little bunny statue. I can load the file that represents the bunny statue into a program. That's "declaring the variable":
    Code:
    Dim statue As BunnyStatue
    But I don't have a statue yet. I have to push the button to send the "blueprints" to the printer so it can print the statue for me. That's what 'New BunnyStatue()' does:
    Code:
    Dim statue As New BunnyStatue()
    "Instantiate" is the fancy word we made up for "create".

    So that quote from above seems kind of silly in context:

    It's already instantiated in the module, so I don't know why they want you to do it again. But you may as well follow the instructions. Since the variable already exists, you don't have to declare it. So under the End If in your Sub, I'd write:
    Code:
    Main.basket = New GroceryBasket()
    ----1------   --------2----------
    The part I've underlined as (1) says, "this is the variable I'd like to change". Since 'basket' belongs to the module 'main', its name is 'Main.basket'. The part I've underlined as (2) says, "Instantiate a new GroceryBasket". The = sign tells VB, "Assign the thing on the right to the variable on the left."

    Now, the next part:

    Since GroceryBasket is a List(Of GroceryItem), you can use its Add() method to add a grocery item to it. That means you need to instantiate at least one GroceryItem and give it to the Add() method. It might look like this:
    Code:
    Dim apple As New GroceryItem(1, "Apple Acres", 0.25)
    Main.basket.Add(apple)
    The first line instantiates a new GroceryItem and stores it in a variable named 'apple'. The next line adds that object to the basket. You could do this all on one line:
    Code:
    Main.basket.Add(New GroceryItem(1, "Apple Acres", 0.25))
    But note how that is a little less clear. Sometimes saving space doesn't help us!

    The assignment's asking you to get used to that, but:

    It seems that part was optional.
    ok so like this:

    Code:
    Public Class LoginForm
        Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
            Try
                Login(txtUsername.Text, txtPassword.Text)
            Catch ex As LoginException
                MessageBox.Show(“Incorrect password.”)
            End Try
            If Main.blnLoggedIn Then
                MessageBox.Show(“Thank you for logging in, “ & txtUsername.Text, “Logged In.”)
                Me.Close()
            End If
            Main.basket = New GroceryBasket()
        End Sub
        Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
            Application.Exit()
        End Sub
    End Class
    But what exactly is supposed to happen when I test it?? Because when I test it it lets me login with username and password on the login form I created but nothing happens with the properties, grocery items, or anything??
    Last edited by EmilyM1105; Feb 2nd, 2018 at 05:23 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width