Help with Set and Get Property Statements and their Array Variables in a Class - Looking for best and right way to do this.

  • Thread starter Thread starter thedillinger
  • Start date Start date
T

thedillinger

Guest
TITLE: Help with Set and Get Property Statements and their Array Variables in a Class - Looking for best and right way to do this.

USING: Windows 7 Professional x64 sp1, Visual Studio 2012 Professional, Visual Basic.NET

NOTES: I do not wish to upgrade Windows or Visual Studio. I am a beginner at VB.NET.

BACKGROUND: I am not certain how to say or word this, I am not certain what I need. I have been playing around with the following in combination = Classes, Properties, Arrays/Variables. Through trial and error and some help searching online and asking here I have figured out how to declare my project variables and even set their values. I now have a number of Property statements, some based on array variables others not. All of these property statements despite being different ways of doing things, the nuances, they all seem to work. I am stuck on these property statements, I do not fully understand how or why these all work, the code is a bit hard to understand. I do not understand the best way or the right way of doing these property statements, either like those I have included, or if there is a better way of doing them I would like to know. I do not know if all of my property statements are correct and error free, they seem to be error free from running my project but i have no idea. Things like placement of brackets(), or even lack of brackets, needing to or not needing to pass byVal/Variables to the Property when its called. This is all a bit confusing to me.

QUESTION1: Do all my property statements work as intended?

QUESTION2: Pick of mine or write a better (best, right) way of doing both 1d and 2d properties.

QUESTION3: If you have anything else constructive or otherwise to say please post it.

PROJECT CODE BELOW: (I have only included below the code that is relevant to my problem, taken from my project. I hope there are no errors or anything missing, I tried my best)


Public Class Game1
Public Property UnitFriendProp As UnitFriend = New UnitFriend


Public Class UnitFriend


Private FriendHealth() As Int32 = {100, 111, 100}

Private FriendHealth2 As Int32() = {200, 200, 333} 'both ways seem valid

Private FriendHealth2x() As Int32 = {300, 444, 300} 'both ways seem valid

Private FriendHealth3(,) As Int32 = New Int32(,) {
{0, 0, 0, 0, 1},
{0, 0, 0, 1, 1},
{0, 0, 1, 1, 1},
{0, 0, 0, 1, 9},
{0, 0, 0, 7, 1}
} 'this works too

Private FriendHealthx(,) As Int32 = New Int32(,) {
{0, 0, 0, 0, 1},
{0, 0, 0, 1, 1},
{0, 0, 1, 1, 1},
{0, 0, 0, 1, 9},
{0, 0, 0, 7, 1}
} 'this works too


Public Property FriendHealthProp(ByVal index As Int32) As Int32 'works (but not best way)
Get
Return FriendHealth(index)
End Get
Set(ByVal value As Int32)
FriendHealth(index) = value 'does work but how??? look at how i set it
End Set
End Property


Public Property FriendHealth2Prop() As Int32() 'works
Get
Return FriendHealth2
End Get
Set(ByVal value As Int32())
FriendHealth2 = value 'does work
End Set
End Property


Public Property FriendHealth2xProp() As Int32() 'works
Get
Return FriendHealth2x
End Get
Set(ByVal value As Int32())
FriendHealth2x = value 'does work
End Set
End Property


Public Property FriendHealth3Prop() As Int32(,) 'works
Get
Return FriendHealth3
End Get
Set(ByVal value As Int32(,))
FriendHealth3 = value 'does work
End Set
End Property


'create a property with byval index1d and index2d
Public Property FriendHealthxProp(ByVal index1D As Int32, ByVal index2D As Int32) As Int32 'also works (but not best way)
Get
Return FriendHealthx(index1D, index2D)
End Get
Set(ByVal value As Int32)
FriendHealthx(index1D, index2D) = value 'also does work but how??? look at how i set it
End Set
End Property


'use the proper naming method/convention for variables with properties. so make a private variable which would be called _Mynamevar and its property would just be called Mynamevar - the underscore _ before variable name just means/tells you it is a private variable, make sure to create variables as private when they have a property.

End Class
End Class


Public Class FifthWorkaroundClass

Public Sub testitnow()
MessageBox.Show(mygame.UnitFriendProp.FriendHealthProp(0))
End Sub

Public Sub testitnow2()
MessageBox.Show(mygame.UnitFriendProp.FriendHealth2Prop(2))
End Sub

Public Sub testitnow2x()
MessageBox.Show(mygame.UnitFriendProp.FriendHealth2xProp(1))
End Sub

Public Sub testitnow3()
MessageBox.Show(mygame.UnitFriendProp.FriendHealth3Prop(3, 4))
End Sub

Public Sub setitnow3()
mygame.UnitFriendProp.FriendHealth3Prop(3, 4) = 285
MessageBox.Show(mygame.UnitFriendProp.FriendHealth3Prop(3, 4))
End Sub

Public Sub setitnow2x()
mygame.UnitFriendProp.FriendHealth2xProp(1) = 266
MessageBox.Show(mygame.UnitFriendProp.FriendHealth2xProp(1))
End Sub

Public Sub setitnow2()
mygame.UnitFriendProp.FriendHealth2Prop(2) = 225
MessageBox.Show(mygame.UnitFriendProp.FriendHealth2Prop(2))
End Sub

Public Sub setitnow()
mygame.UnitFriendProp.FriendHealthProp(0) = 234
MessageBox.Show(mygame.UnitFriendProp.FriendHealthProp(0))
End Sub

Public Sub testitnowx()
MessageBox.Show(mygame.UnitFriendProp.FriendHealthxProp(0, 0))
End Sub

Public Sub setitnowx()
mygame.UnitFriendProp.FriendHealthxProp(0, 0) = 311
MessageBox.Show(mygame.UnitFriendProp.FriendHealthxProp(0, 0))
End Sub
End Class


Public Class Form1
Dim myfifthworkaroundclass As New FifthWorkaroundClass


Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
myfifthworkaroundclass.testitnow()
End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
myfifthworkaroundclass.testitnow2()
End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
myfifthworkaroundclass.testitnow2x()
End Sub

Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
myfifthworkaroundclass.testitnow3()
End Sub

Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
myfifthworkaroundclass.setitnow3()
End Sub

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
myfifthworkaroundclass.setitnow2x()
End Sub

Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
myfifthworkaroundclass.setitnow2()
End Sub

Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
myfifthworkaroundclass.setitnow()
End Sub

Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
myfifthworkaroundclass.testitnowx()
End Sub

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
myfifthworkaroundclass.setitnowx()
End Sub

End Class


Module Module1
Public mygame As New Game1

End Module



Create a Form called Form1. Create buttons on the form with Button Text as follows...

Button6 = "FriendHealth"
Button13 = "Set FriendHealth"
Button7 = "FriendHealth2"
Button12 = "Set FriendHealth2"
Button8 = "FriendHealth2x"
Button11 = "Set FriendHealth2x"
Button9 = "FriendHealth3"
Button10 = "Set FriendHealth3"
Button14 = "FriendHealthx"
Button15 = "Set FriendHealthx"

Continue reading...
 
Back
Top