Create new Customer class dynamically in vb 2010

Associate
Joined
31 Dec 2002
Posts
458
Hi, I am stuck for the last week trying to understand what is probably staight forward for most of you. In vb 2010 I am trying to do the following:

Have a customer class with the following properties so:

public class Customer
public property name as string
public property bill as string
public property usage as string
end class

I want a sub routine that creates a new instance of the Customer class after a customer has entered their name in a textbox,taking the name from a textbox as the parameter, something like:

private sub addCustomer(name as string)
dim name as new Customer
newCustomer.name = txtName.text
newCustomer.usage = txtUsage.text
newCustomer.bill = txtBill.text

end sub

I feel I may be close but would appreciate it if someone could give me a steer. I have omitted the form start and end class code and other code, but it is just the standard prepopulated code. I have also created the customer class by adding new class. thanks
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
What you've got looks like a reasonable start, but what are you doing with the Customer object once you've created it?

Your method is called add customer, but you don't add it to anything. You're creating it and that's it. Nothing else can do anything with your object the way it stands.
 
Associate
OP
Joined
31 Dec 2002
Posts
458
the trouble is, what I need to do is have a customer object that is named after that particular customer. I do not know before the app runs what the customers name will be. I have text boxes for them to enter their name, account number, usage, etc.

I want to be able to retrieve this information later so that next time they login they will be able to view their previous bill and usage information. I originally was using an arraylist() which worked fine with .add to bring the textbox text into the array. The problem is that for each customer that is added to the array their information is added sequentially and there is no index. The same customer could also have multiple sets of data. If I had an index I could then keep track of it and retrieve their information that way. I could use a multi-dimension array, but this is hard to navigate and conceptualise (for me anyway). I cannot use a database for this app.

the app is for me to learn from, not a production app as I am fairly new to programming.
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
What are you actually trying to do with this application?
Do you have a list of all your requirements and what you currently have coded?

Putting the objects into an ArrayList (or any in memory collection) won't persist data after the application is closed. You'll have to store them in some sort of external database.
 
Associate
OP
Joined
31 Dec 2002
Posts
458
I know it won't persist apart from dumping to a text file at the moment. I am using it to learn about using simple classes and retrieving and storing information in them. It is a simple customer database if you like where users can enter their consumption of electricity for example and for the program to work out their unit costs, total up a bill and present it to them. They can then login later and view previous bills. I know a database will be the ideal solution but want to know how to create a new instance of my customer class, based off customer name.

Thanks. Hope that makes sense:)
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Ah, I see the problem now - was browsing on my phone earlier and the small screen must have made me miss it :p

The parameter to your method is a String.
You're then trying to assign a variable of type Customer to that variable. As a String is not a Customer it won't work.

Where is the 'name' parameter coming from? i.e. how are you calling this method?
You're already setting the customer name to be the value from the TextBox in the method, so presume it's some different source?

As I said earlier, if you can post your full code it would be useful.
 
Associate
OP
Joined
31 Dec 2002
Posts
458
Here you go:

Code:
Public Class frmClassTest

    Dim custName As String = txtName.Text

    Private Sub addCustomer(name As customer)
        Dim newCustomer As New customer

    End Sub


    Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click

        addCustomer(custName)
        newCustomer.name = txtName.Text
        newCustomer.account = txtAccount.Text
        newCustomer.usage = txtUsage.Text
        newCustomer.type = txtType.Text

        txtDisplayClass.Text = newCustomer.name & vbNewLine & newCustomer.account & vbNewLine * newCustomer.usage _
            vbnewline & newCustomer.type




    End Sub

    Private Sub frmTextTest_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    End Sub

    
End Class

Code:
Public Class customer
    Private Property name As String
    Private Property accountNumber As String
    Private Property usage As String
    Private Property custType As String

End Class
 
Last edited:
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Assuming that's everything you have then there a few things wrong.

Firstly, the custName variable is initialised as a field using the value from the text box. This happens when your form is created, so will always be null.
Second, you're declaring your newCustomer variable in the addCustomer method and then trying to access the variable outside of that method having never returned it.

I'd have a search for some VB.NET tutorials as it seems you have some basic misconceptions on how it works.
 
Associate
Joined
14 Mar 2007
Posts
1,667
Location
Winchester
I don't touch vb.net much but we do something similar in vba for some of the databases at work.

Have the customer class setup as you do with a property for address, age, sex etc etc. This is the template for any customer.

Have a another class which say contains a dictionary object (there is no reason this actually needs to be in a class at all but in my head it keeps it clean - there is probably better objects in vb.net too), the key of the dictionary will be the customer name and the item will be a "copy" of the customer class.

As and when a user enters a customer name in the text box the event adds an item to the dictionary. Something like:

classDict.mydict.add customerNameTextBox.value (this is the key), new clsCustomer (this is the item).

What you have then here is a "copy" of the class which is unique to the customer name and can be writen to and read by referring to the key of the dictionary.

For each of the other controls in your form say the age control you would then simply (in vba at least go):

classdict.mydict.item(customerNametextbox.value).age = ageTextBox.value

This will then add the age of that customer to the age property of the "copy" of the customer class held against the customers name held in the dictionary.

If for any reason you ever wanted to return the value held in the class you just reverse the above:

ageTextBox.value = classdict.mydict.item(customerNametextbox.value).age

You never then actually add anything into the raw class but simply in the "copy" of the class held in the dictionary.

Naturally it isn't as simple as that it would always make sense to see if that customer exists in the dictionary before trying to add something and obviously make sure tat the customer name text box is not blank.

In short though you need to hold your instantiated customer class in a container of one sort or another, in VBA this is a dictionary object. I'm sure vb.net has much better objects, you may be even able to hold a class in an array in vb.net if that is your thing. Either way it has to be held somewhere, as as soon as you instantiate the customer class again the old data will be destroyed.
 
Last edited:
Back
Top Bottom