How to declare an array instance of a class, then initialize it and populate the variables in each of the array's classes.

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

thedillinger

Guest
As per title, i am still a beginner.

I have tried many things to solve this but was unable to. I found one promising solution online (not sure where the link is now) but i did not understand the code solution.

My code so far is below, as you can see i am stuck on "Public Unit(50) As ClassUnit" and "Public Sub UnitPopulate()" - all the contents within.

the form has two buttons on it that is all.

I used my project - testingtheories to try out new tings and try and find ways of doing things or workarounds so not all project code is related to my problem.

Thanks for any help anyone can give.


Module ModuleUnit
Public Unit(50) As ClassUnit

Public Sub UnitPopulate()
'this sub does not work has errors
'i need to ask msdn forums how to declare an array instance of a class, then initialize it and populate the variables in each of the array classes.
'i do not want to use a list of, because an array of a class better suits my needs.
'Me.unit = New unit(i) {}
For i As Int32 = 0 To 49


Unit(i).UnitID = 0
Unit(i).UnitName = "Archer"
MessageBox.Show("It ran once+") 'no it never ran through at all
Unit(i).UnitRace = "Elf"
Unit(i).UnitMove = 5
Unit(i).UnitAttack = 3
Unit(i).UnitDefence = 2
Next
MessageBox.Show(Unit(4).UnitName)

End Sub
End Module



Public Class ClassUnit
Public Property UnitID As Int32
Public Property UnitName As String
Public Property UnitRace As String 'or have as id?
Public Property UnitMove As Int32
Public Property UnitAttack As Int32
Public Property UnitDefence As Int32

End Class


Imports System
Imports System.Collections.Generic
'not sure if need the above 2 import lines, it works either way.

Module ModuleTorsion


''Public formA(1) As Class1
Public formA As Class1 = New Class1 With {.class1string = "ddd"}
'And you can create list of Torsion objects and pass it to the sub function

Public formAList As New List(Of Class1)

'New Torsion List
Dim TorsionList As New List(Of Torsion)

Public Sub TorsionRun()
MessageBox.Show("hello")
Console.WriteLine(formA.class1string)
''formA(1).class1string = "ddd"
formA.class1string = "aaa"
Console.WriteLine(formA.class1string)
Console.WriteLine("this works")

formAList.Add(New Class1 With {.class1string = "xxx"}) 'both work but which is proper to use?
formAList.Add(New Class1() With {.class1string = "bbb"}) 'both work but which is proper to use?
''Console.WriteLine(formA(0).class1string)
'Add New Torsion Object
TorsionList.Add(New Torsion() With {.Momentum = 10})
TorsionList.Add(New Torsion() With {.Momentum = 20})
TorsionList.Add(New Torsion() With {.Momentum = 30})

'Retrieving values from List
For Each obj As Torsion In TorsionList
'Response.Write(obj.Momentum.ToString() + "<br/>")
Console.WriteLine(obj.Momentum.ToString() + "<br/>")
Next

For Each obj As Class1 In formAList
Console.WriteLine(obj.class1string.ToString() + "<br/>")
Next
End Sub
''i think the error is because i have not created a new torsion class instance

End Module



Public Class Torsion
'forums.asp.net/t/1609931.aspx?How+to+create+an+array+of+VB+net+objects+from+a+VB+net+class
'Create a List of Objects this way

Dim _momentum As Integer
Public Property Momentum As Integer
Get
Return _momentum
End Get
Set(ByVal value As Integer)
_momentum = value
End Set
End Property
End Class


Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Dim listofclass1 As New List(Of Class1)
'Dim arrayofclass1(,) As Class1 = New Class1
'Dim M() As Integer = New Integer() {}
'Dim arr(9) = New Class1
'Dim formA() As Class1 = New Class1() With {.class1string = "ddd"}
'Dim formA() As Class1 = New Class1() 'With {.class1string = "ddd"}


End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ModuleTorsion.TorsionRun()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ModuleUnit.UnitPopulate()
End Sub
End Class


Public Class Class1
'Dim class1string As String
Public Property class1string As String
End Class

Continue reading...
 
Back
Top