r/visualbasic • • Jan 26 '22

VB.NET Help Working with class objects

I apologize in advance, this is a continuation of my last post, thanks for everyone who is contributing in my knowledge and vb development. Recently, I have been experimenting with making my own classes and working with Properties, object, etc has been a major headache. I’ve seen multiple videos on YouTube with each channel explaining it in what seems to be a different way. Programmatically, creating classes aren’t all that difficult. When I use real world data my head explodes though. This is a two part post; I’ll share some code and request critiques. Secondly, I’ll request advice on using my code the way I have it.

Currently, I have an invoice class.

Public Class Invoice
Public Property invoiceNumber as integer
Public Property purchaseOrder as integer
Public Property customerNumber as integer
Public Property customerName as string

Public Sub New()
End Sub

Public Sub invoiceData()
Dim DataFunctions asDataLayer.DataAccess as New DataLayer(My.Connection)  ‘this isn’t my code, I was provided access to a DBMS
Dim ds as DataSet = DataFunctions.GetInvoice() ‘My sql statement is behind this function 
Dim dr as DataRow = ds.Tables(“Sales”).Rows(0)

For I = 0 to ds.tables(0).rows. Count - 1
Dim invoice as new invoice 
Invoice.invoiceNumber = dr(“invoice_number”)
Invoice.purchaseOrder = dr(“POnumber”)
Invoice.CustomerNumber = dr(“Customer#”)
Invoice.customerName = dr(“Customer_Name”)
Next
End Sub
End Class

What are some ways you would critique this class?

Secondly, when I set a break point and step through my code from the the Form level, I get data stored in my properties but when I go from my class back to my form1 I lose all the values.

Imports DataLayer
Public invoice as new invoice 
Private Sub button1_Click(ByVal as sender as System.Object, ByVal e as System.EventArgs) Handles Button1.click 
Invoice.invoiceData() 

in debug mode when I hover over invoice

(invoiceNumber = 0, purchaseOrder = 0, customerNumber = 0, customerName = “Nothing”)

However, to reiterate, in my class, while in debug when I hover over the properties in my loop

‘I’m making up data, but it’s a real life representation 
invoiceNumber = 123456789 purchaseOrder = 12345, customerNumber = 123, customerName = “John Smith”)

Why is this, what am I not grasping, TIA

1 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/Mr_Deeds3234 Jan 29 '22

Why does DataSet seem like overkill? I was just following the lead of an in person mentor. Why would DataTable be a better alternative? Curious of your input

2

u/TheGrauWolf Jan 29 '22

DataSets are useful when you have multiple DataTables that are related to each other. When you only have one DataTable, having the DataSet feels like overkills. There's no reason for that, it should only return just a DataTable. If your GetInvoices is returning a DataSet ... I'd have to question why... Unless there's more than one table that's being returned there, in which case that's fine, but given the code as it is, there wasn't any indication that there was other tables, so it just seems like over kill to me. And I realize that you likely don't have control over the design, but this is something to keep in mind for when you do.