Reputation: 11
Dim Cans(7) As String
Dim money As Decimal
Public Sub Main()
Cans(0) = "Pepsi"
Cans(1) = "Pepsi MAX"
Cans(2) = "Sprite"
Cans(3) = "Mountain Dew"
Cans(4) = "Fanta"
Cans(5) = "Coca Cola"
Cans(6) = "Coke Diet"
Cans(7) = "Coke Vanilla"
End Sub
Private Sub ButtonPepsi_Click(sender As System.Object, e As System.EventArgs) Handles ButtonPepsi.Click
If money >= 0.8 Then
money = money - 0.8
TextBoxItem.TextAlign = HorizontalAlignment.Center
TextBoxItem.Text = "You brought a " & Cans(0)
TextboxCredit.Text = "£" & money
End If
End Sub
Every time I click the button to buy a pepsi, it just says in the textbox "You bought a " and it should say "Pepsi" but it does not work. Anybody got any ideas?
Upvotes: 0
Views: 103
Reputation: 26454
Main is never called, that's why your array elements are all empty string. I think you are mixing console and windows applications. You can place a call to Main in Sub New, after InitializeComponent. Or better, move all that code after InitializeComponent, to avoid confusion.
Or just declare your Cans
array like this:
Dim Cans() As String = {"Pepsi", ..., "Coke Vanilla"}
Then you don't need Main
at all.
Upvotes: 2
Reputation: 172468
VB.NET has many methods of starting your application. Which one is used can be set in the project properties. The most common ones are:
Sub Main: Here, a sub Main in a module is called. Then, it's the sub's responsibility to start forms that should be shown.
Start Form: This is probably the option you are using. A specific form is opened and shown to the user. No Sub Main is executed.
How do you solve your problem? If you want to initialize the array when the window is opened, just use the Form.Load
event for that:
Public Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Cans(0) = "Pepsi"
Cans(1) = "Pepsi MAX"
...
End Sub
Upvotes: 2
Reputation: 391
Try this,
Private Sub ButtonPepsi_Click(sender As System.Object, e As System.EventArgs) Handles ButtonPepsi.Click
Main()
If money >= 0.8 Then
money = money - 0.8
TextBoxItem.TextAlign = HorizontalAlignment.Center
TextBoxItem.Text = "You brought a " & Cans(0)
TextboxCredit.Text = "£" & money
End If
End Sub
Upvotes: 0