-
September 13th, 2012, 05:58 PM
#1
Class, property and list
Here some code about something i seem not to get working...
1) first part is a class and it works
2) button1 works
3) why doenst allow me to retrieve the value of the list i made like bakjeList.count?
It says BakjeList is not declared.. etc, but how to declare it right?
Code:
Public Class Class_BaanAsBakje
REM ************************************************
REM * Fields voor de bakjes van de baan
REM ************************************************
Private mXlineBakje As Single
Private mYlineBakje As Single
Private mWaarde As Single
REM ************************************************
REM * Properties voor de bakjes van de baan
REM ************************************************
Public Property XlineBakje As Single
Get
Return mXlineBakje
End Get
Set(ByVal value As Single)
mXlineBakje = value
End Set
End Property
Public Property YlineBakje As Single
Get
Return mYlineBakje
End Get
Set(ByVal value As Single)
mYlineBakje = value
End Set
End Property
Public Property Waarde As Single
Get
Return mWaarde
End Get
Set(ByVal value As Single)
mWaarde = value
End Set
End Property
Public Sub New(ByVal xlineBakje As Single, ByVal ylinebakje As Single, ByVal waarde As Single)
mXlineBakje = xlineBakje
mYlineBakje = ylinebakje
mWaarde = waarde
End Sub
End Class
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bakjesList As New List(Of Class_BaanAsBakje)
bakjesList.Clear()
ListBox1.Items.Clear()
For i = 0 To 100
Dim bakje As Class_BaanAsBakje = New Class_BaanAsBakje(i, i + 1, i + 3)
bakjesList.Add(bakje)
ListBox1.Items.Add(bakje.XlineBakje & " " & bakje.YlineBakje & " " & bakje.Waarde)
Next
Dim test As Integer
test = bakjesList.Count()
bakjesList.RemoveAt(5)
For i = 0 To bakjesList.Count - 1
ListBox1.Items.Add(bakjesList(i).XlineBakje & " " & bakjesList(i).YlineBakje & " " & bakjesList(i).Waarde)
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Dim bakjesList As List(Of Class_BaanAsBakje)
Dim test2 As Integer
test2 = bakjesList.Count()
End Sub
End Class
-
September 14th, 2012, 06:10 AM
#2
Re: Class, property and list
Your issue has to do with the scope of your object. When you declare an object inside a function, it will only exist for that function. Declare it for the class, and it will exist for the whole class. I would suggest something like....
Code:
Public Class Form1
Private bakjesList As New List(Of Class_BaanAsBakje)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bakjesList.Clear()
ListBox1.Items.Clear()
For i = 0 To 100
Dim bakje As Class_BaanAsBakje = New Class_BaanAsBakje(i, i + 1, i + 3)
bakjesList.Add(bakje)
ListBox1.Items.Add(bakje.XlineBakje & " " & bakje.YlineBakje & " " & bakje.Waarde)
Next
Dim test As Integer
test = bakjesList.Count()
bakjesList.RemoveAt(5)
For i = 0 To bakjesList.Count - 1
ListBox1.Items.Add(bakjesList(i).XlineBakje & " " & bakjesList(i).YlineBakje & " " & bakjesList(i).Waarde)
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim test2 As Integer
test2 = bakjesList.Count()
End Sub
End Class
-
September 14th, 2012, 12:45 PM
#3
Re: Class, property and list
True that was the solution as slowly i start to understand that public is not as public as in VB6......
Thanx for showing your experience, as its clearly a noob error, but as stated before it feels so different to vb6, the learning curve is much different as well....
But I keep continuing.....
-
September 14th, 2012, 01:02 PM
#4
Re: Class, property and list
It is different, but you will find that it is also easier in many ways, and also in many ways more powerful.
Keep plugging away and come back here when you need help
-
September 14th, 2012, 09:52 PM
#5
Re: Class, property and list
Originally Posted by Joska Paszli
True that was the solution as slowly i start to understand that public is not as public as in VB6......
Thanx for showing your experience, as its clearly a noob error, but as stated before it feels so different to vb6, the learning curve is much different as well....
But I keep continuing.....
Actually if you dim a variable inside a function using VB6 you will get an error if you try to reference that variable in a different function. The variable is only known to the function where it is defined and must be defined outside the functions to be visible to the entire form, module or class and must be Public to be seen outside the form, module or class this is the same in VB6 and VB.Net. While there are some differences they are very much the same as well.
Always use [code][/code] tags when posting code.
-
September 15th, 2012, 01:51 AM
#6
Re: Class, property and list
This is issue is at the very heart of OOP. You must understand scope properly in order to use it properly. I'd suggest you get a book on that, or google it :
https://www.google.co.za/search?q=oo...ient=firefox-a
-
September 15th, 2012, 04:15 AM
#7
Re: Class, property and list
true i have 2 books now but still i find it is not getting enough weight in books or examples.... also te most common examples are for such a small, simple example (ofcourse) but a step by step explanation what u can do with it in combination with each other could get more attention.... also the flexibility and power makes it hard but now after 2 weeks/100 hours i see more and more light... a steep learning curve.... i cant say i miss vb6 but i still miss the speed in which i can programm.... it really takes much time
-
September 15th, 2012, 09:53 AM
#8
Re: Class, property and list
Once you have a handle on the VB.Net classes and usage you may find that you can write programs faster in VB.Net and that you can with a little planning reuse a lot of your code.
I think the thing that stumped me the most was the transition from ADO Connections and Recordsets to ADO.Net datasets, datatables and datareaders.
Always use [code][/code] tags when posting code.
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
|