-
Feb 2nd, 2018, 02:43 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Derive from the generic List class
Can someone tell me if Im on the right track and what Im missing??
My instructions are:
Add a new public class named GroceryBasket to the project.
1. Derive from the generic List class.
2. Ensure that only GroceryItem objects are stored as items.
Here is my GroceryBasket Code:
Code:
Public Class GroceryBasket
Inherits List(Of GroceryItem)
End Clas
Did I write that right?? If not how do I??
Heres my GroceryItem Code:
Code:
Public Class GroceryItem
Private mScanNumber As Integer
Public ReadOnly Property ScanNumber As Integer
Get
Return mScanNumber
End Get
End Property
Private mBrandName As String
Public Property BrandName As String
Get
Return mBrandName
End Get
Set(value As String)
mBrandName = value
End Set
End Property
Private mDescription As String
Public Property Description As String
Get
Return mDescription
End Get
Set(value As String)
mDescription = value
End Set
End Property
Private mPrice As Double
Public Property Price As Double
Get
Return mPrice
End Get
Set(value As Double)
If value > 0 Then
mPrice = value
End If
End Set
End Property
Private mAisle As String
Public Property Aisle As String
Get
Return mAisle
End Get
Set(value As String)
mAisle = value
End Set
End Property
Public Sub New(MscanNumber As Integer, brandName As String, price As Double)
Me.mScanNumber = ScanNumber
Me.BrandName = brandName
Me.Price = price
End Sub
End Class
I already did this part: Add the following variable declaration to the Main module: Friend basket As New GroceryBasket
Heres my Main Code:
Code:
Module Main
Friend blnLoggedIn As Boolean
Dim arrUsernames() As String = {"Admin", "Clerk", "Manager"}
Dim arrPasswords() As String = {"P@ssword", "pa$$word", "passw0rd"}
Dim userIndex As Integer
Friend basket As New GroceryBasket
I dont understand how to do this part either??
Note: You can add code to the btnLogin_Click event handler procedure to instantiate the GroceryItem class and add multiple objects to the GroceryBasket variable. Make sure you either remove or comment out this code after testing.
Here is my LoginForm with the btnlogin_Click handler:
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
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Application.Exit()
End Sub
End Class
Last edited by EmilyM1105; Feb 2nd, 2018 at 02:56 PM.
-
Feb 2nd, 2018, 04:26 PM
#2
Re: Derive from the generic List class
Everything above this quote is correct:
I dont understand how to do this part either??
Let's talk about this part:
Note: You can add code to the btnLogin_Click event handler procedure to instantiate the GroceryItem class
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:
Note: You can add code to the btnLogin_Click event handler procedure to instantiate the GroceryItem class
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:
add multiple objects to the GroceryBasket variable.
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:
Make sure you either remove or comment out this code after testing.
It seems that part was optional.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Feb 2nd, 2018, 05:08 PM
#3
Thread Starter
Addicted Member
Re: Derive from the generic List class
Originally Posted by Sitten Spynne
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.
-
Feb 2nd, 2018, 05:13 PM
#4
Re: Derive from the generic List class
Nope and nope. Read it again. I didn't say "End Sub", and that's not the line I said to add. It's the line you already added.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Feb 2nd, 2018, 05:51 PM
#5
Re: Derive from the generic List class
Originally Posted by Sitten Spynne
...
"Instantiate" is the fancy word we made up for "create".
So that quote from above seems kind of silly in context:
Note: You can add code to the btnLogin_Click event handler procedure to instantiate the GroceryItem class
It's already instantiated in the module, so I don't know why they want you to do it again.
...
I think you might have misread that, or else I don't know what's going on.
What they instantiated in the module was a GroceryBasket. The quote says you might want to instantiate the GroceryItem class, I presume so you can add items to the basket for testing purposes when you execute the login code. Later, you would remove the preloaded GroceryBasket test code, so the basket would be normally empty when you login.
-
Feb 2nd, 2018, 05:52 PM
#6
Thread Starter
Addicted Member
Re: Derive from the generic List class
Originally Posted by Sitten Spynne
Nope and nope. Read it again. I didn't say "End Sub", and that's not the line I said to add. It's the line you already added.
Ok so everything I had up top is right then? Do i need to add anything to the event handler or no? That just confused me because you said after End if add that.
-
Feb 2nd, 2018, 05:54 PM
#7
Thread Starter
Addicted Member
Re: Derive from the generic List class
Originally Posted by passel
I think you might have misread that, or else I don't know what's going on.
What they instantiated in the module was a GroceryBasket. The quote says you might want to instantiate the GroceryItem class, I presume so you can add items to the basket for testing purposes when you execute the login code. Later, you would remove the preloaded GroceryBasket test code, so the basket would be normally empty when you login.
Right..so am I missing anything in my codes or not?? Im so confused??
-
Feb 3rd, 2018, 05:27 AM
#8
Re: Derive from the generic List class
I don't know how you're suppose to display the items in the basket to verify they are there.
Also, I don't understand the hierarchy of your program.
You have a login button and textboxes, but are those on a separate login form compared to your main form?
The reason I ask is because if the user successfully logins in you do a "Me.Close" which will close the current form that the code is running in, so I assume you must have another main form that had a button that presented the login form.
Going with that assumption, I created a quick test where the main form (form1) had a button to show the login form, and another button to simply print out items in the basket using Debug.Print (prints to the Immediate Window or Output Window depending on your IDE option).
Code:
Public Class Form1
Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
LoginForm.ShowDialog()
End Sub
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
For Each item As GroceryItem In Main.basket
Debug.Print("{0}, {1}, {2}", item.ScanNumber.ToString, item.BrandName, item.Price.ToString)
Next
End Sub
And in the login code, added items to the basket along the lines that Sitten Spynne mentioned.
Code:
Public Class LoginForm
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Try
Login(txtUsername.Text, txtPassword.Text)
Catch ex As Exception '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
Dim anItem As GroceryItem
anItem = New GroceryItem(1, "Apple Acres", 0.25)
Main.basket.Add(anItem)
anItem = New GroceryItem(2, "Banana Barn", 0.49)
Main.basket.Add(anItem)
End Sub
End Class
I didn't create the "LoginException" value, so just commented it out rather than deal with it.
I also probably would have put the test code that adds items to the basket inside the If block where the user successfully logged in, because this will add items even if Main.blnLoggedIn is false since it is outside the block.
Once the login form is closed, and assuming the lines that add items to the basket were executed, on the main form if you press the "show" button, you should see the items printed out, verifying that the code did successfully add items to the basket.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|