Can't figure out making an Array based off of a Class - arrays

What I'm trying to do is create an array from a Class that holds a list of information.
What I've tried so far is this - First, create the Class:
Public Class PowerArray
Public Name As String
Public Action As String
Public Cost As Integer
Public Pool1 As String
Public Pool2 As String
Public Range As String
Public Duration As String
Public Tags As ArrayList = New ArrayList
Public Desc As String
End Class
Then, define a new object that uses PowerArray as type, contained in a Public Class so it can be called in any sub:
Public Class GVar
Public Shared CharPowers(100) As PowerArray
Public Shared PCount As Integer = 0
End Class
But when I try to write to that object, it keeps throwing a Null Reference Exception.
Example:
Trying to write an entry to CharPowers(GVar.Pcount) (where PCount is currently 0, and "txt.PowerName.Text" is any string):
GVar.CharPowers(GVar.PCount).Name = txtPowerName.Text
...throws the exception. This happens no matter which value I try to write to.
I haven't had any major hangups going this route when defining a new object inside a sub using that type (not as an array, but just a simple object), so I'm guessing it's the object being an array that's throwing a fit. I just can't figure out how or why. It clearly exists, it's defined on startup.

Your array in GVar is an array of elements typed as PowerArray with a value of Nothing. You must create an instance of PowerArray and assign it to an element is the array before you can set a field of a PowerArray object.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PA As New PowerArray
GVar.CharPowers(GVar.PCount) = PA
GVar.CharPowers(GVar.PCount).Name = TextBox1.Text
MessageBox.Show(GVar.CharPowers(GVar.PCount).Name)
End Sub
Are you planning on incrementing PCount? You would not need to keep track of where you are in the array if you used List(Of PowerArray).
EDIT
Public Class PowerArray
Public Property Name As String
Public Property Action As String
Public Property Cost As Integer
Public Property Pool1 As String
Public Property Pool2 As String
Public Property Range As String
Public Property Duration As String
Public Property Tags As New List(Of Object)
Public Property Desc As String
End Class
Public Class GVar
Public Shared Property CharPowers As New List(Of PowerArray)
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PA As New PowerArray
PA.Name = TextBox1.Text
GVar.CharPowers.Add(PA)
For Each item As PowerArray In GVar.CharPowers
MessageBox.Show(item.Name)
Next
End Sub

Seems I tripped over an answer. In particular:
Public Class GVar
Public Shared CharPowers(100) As PowerArray
Public Shared PCount As Integer = 0
Shared Sub New()
For i = 0 To CharPowers.GetUpperBound(0)
CharPowers(i) = New PowerArray
Next
End Sub
End Class

Related

How to assign a Class to an array in vb.net

I have a little problem I can't figure out. I want to assign a class to an array but vb.net says, on line 7, that I cannot use the keyword 'New' to someArray(2).
How can I simply achieve what I try to do?
Public Class Form1
Public Class someClass
Public someName As String
Public someNumber As Integer
End Class
Public someRecord As New someClass
Public someArray(2) As New someClass
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
someRecord.someName = "A text"
someRecord.someNumber = 10
someArray(0).someName = "A text"
someArray(0).someNumber = 10
End Sub
End Class
The way it works is that you tell it what the array is made of, and then you need to assign an instance of the class to the array element:
Public Class Form1
Public Class SomeClass
Public someName As String
Public someNumber As Integer
End Class
Public someArray(2) As SomeClass
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim someRecord As New SomeClass
someRecord.someName = "A text"
someRecord.someNumber = 10
someArray(0) = someRecord
' Fill the rest of the array with default instances...
For i = 1 To someArray.Length - 1
someArray(i) = New SomeClass()
Next
End Sub
End Class

VBNet: List of items inside a class, I cant change the value for a single item

My Problem:
I have a Class and a list of other class inside:
Public Class Signal_Type_Read
Private c_signal_count As Integer = 0 ' counter for read signals
Private _items As List(Of Signal_Item)
Private item As New Signal_Item
Sub add_sig()
c_signal_count += 1
items.Add(item)
End Sub
Public Property items() As List(Of Signal_Item)
Get
Return _items
End Get
Set(value As List(Of Signal_Item))
_items = value
End Set
End Property
Function item_counter() As Integer
item_counter = c_signal_count
End Function
Public Sub New()
_items = New List(Of Signal_Item)
End Sub
End Class
Public Class Signal_Item
Private _original_name As String
Public Property Original_name() As String
Get
Return _original_name
End Get
Set(value As String)
_original_name = value
End Set
End Property
'Many other properties
End Class
My problem is when I use in a loop
Public Shared ReadSignals As New Signal_Type_Read
//Part of a Loop to read cells values and store in the variable
ReadSignals.add_sig()
Dim c_index As Integer = ReadSignals.item_counter - 1
ReadSignals.items.item(c_index).Original_name = c_row.Cells(e_Signame).Value
It always changes the "Original_name" Property in all items of my Variable. Where is my error? I want only that oe item is changed.
I found the cause of the problem... I need to create a new instane of item in my ADD_sig() sub
Sub add_sig()
Dim s_item As New Signal_Item
c_signal_count += 1
items.Add(s_item)
End Sub

MS Access VBA using ReDim Preserve to increase the size of an array when needed( as in within a button click event handler method or within a loop )

I'm new to Microsoft Office Professional Plus 2013 Access.
I am developing an application using:
-Microsoft Office Professional Plus 2013 Access
I my VBA Editor, I have the following Class Module:
Option Explicit
Option Compare Database
Private cntrollingPersonFullNameProp As String
Private cntrollingPersonIsNameAddressProvidedProp As String
Private cntrollingPersonIsDOBProvidedProp As String
Private cntrollingPersonIsTaxResidenceProvidedProp As String
Private cntrollingPersonIsControllingPersonTypeProvidedProp As String
Private cntrollingPersonIsSignedAndDatedProp As String
Public Property Get CntrollingPersonFullName() As String
CntrollingPersonFullName = cntrollingPersonFullNameProp
End Property
Public Property Let CntrollingPersonFullName(lCntrollingPersonFullName As String)
cntrollingPersonFullNameProp = lCntrollingPersonFullName
End Property
Public Property Get CntrollingPersonIsNameAddressProvided() As String
CntrollingPersonIsNameAddressProvided = cntrollingPersonIsNameAddressProvidedProp
End Property
Public Property Let CntrollingPersonIsNameAddressProvided(lCntrollingPersonIsNameAddressProvided As String)
cntrollingPersonIsNameAddressProvidedProp = lCntrollingPersonIsNameAddressProvided
End Property
Public Property Get CntrollingPersonIsDOBProvided() As String
CntrollingPersonIsDOBProvided = cntrollingPersonIsDOBProvidedProp
End Property
Public Property Let CntrollingPersonIsDOBProvided(lCntrollingPersonIsDOBProvided As String)
cntrollingPersonIsDOBProvidedProp = lCntrollingPersonIsDOBProvided
End Property
Public Property Get CntrollingPersonIsTaxResidenceProvided() As String
CntrollingPersonIsTaxResidenceProvided = cntrollingPersonIsTaxResidenceProvidedProp
End Property
Public Property Let CntrollingPersonIsTaxResidenceProvided(lCntrollingPersonIsTaxResidenceProvided As String)
cntrollingPersonIsTaxResidenceProvidedProp = lCntrollingPersonIsTaxResidenceProvided
End Property
Public Property Get CntrollingPersonIsControllingPersonTypeProvided() As String
CntrollingPersonIsControllingPersonTypeProvided = cntrollingPersonIsControllingPersonTypeProvidedProp
End Property
Public Property Let CntrollingPersonIsControllingPersonTypeProvided(lCntrollingPersonIsControllingPersonTypeProvided As String)
cntrollingPersonIsControllingPersonTypeProvidedProp = lCntrollingPersonIsControllingPersonTypeProvided
End Property
Public Property Get CntrollingPersonIsSignedAndDated() As String
CntrollingPersonIsSignedAndDated = cntrollingPersonIsSignedAndDatedProp
End Property
Public Property Let CntrollingPersonIsSignedAndDated(lCntrollingPersonIsSignedAndDated As String)
cntrollingPersonIsSignedAndDatedProp = lCntrollingPersonIsSignedAndDated
End Property
In the Form code file,
Dim cntrollingPersonsArray() As CntrollingPerson
Private Sub AddControllingPersonBtn_Click()
Dim cntrlPerson As New CntrollingPerson
cntrlPerson.CntrollingPersonFullName = …….
cntrlPerson.CntrollingPersonIsNameAddressProvided = …..
ReDim Preserve cntrollingPersonsArray(UBound(cntrollingPersonsArray)+ 1)
cntrollingPersonsArray(UBound(cntrollingPersonsArray)) = cntrlPerson
End Sub
The application throws the:
'91' Object variable or With block variable not set
at the following line
cntrollingPersonsArray(UBound(cntrollingPersonsArray)) = cntrlPerson
I've tried a bunch of different code modifications
ReDim Preserve cntrollingPersonsArray(UBound(cntrollingPersonsArray))
or
ReDim Preserve cntrollingPersonsArray(0 to UBound(cntrollingPersonsArray))
or
ReDim Preserve cntrollingPersonsArray(1 to UBound(cntrollingPersonsArray))
Could someone please show me what steps to take in order to correct the aforementioned problem?
Use a collection object instead of an array. All your problems are solved!
EXAMPLE:
Option Explicit
Private cntrollingPersons As New Collection
Private Sub AddControllingPersonBtn_Click()
Dim cntrlPerson As New CntrollingPerson
cntrlPerson.CntrollingPersonFullName = ""
cntrlPerson.CntrollingPersonIsNameAddressProvided = ""
cntrollingPersons.Add cntrlPerson
End Sub
RELATED READING: https://excelmacromastery.com/excel-vba-collections/

I am getting error when accessing variable on another module?

I have a module that is getting values from a datagrid and puts in the tags from each rows into the array of strings. I'm calling that array string on another module but i'm getting object not set to instance of an object. Why? What i'm trying to accomplish is combine all the tag into a array of strings or collection and be able to access it on another module.
'my main module
Public Class myMainModule
Public Shared myArray() As String
......
.......
Public sub doSomething()
Dim myArray As New List(Of String)
For Each row As DataGridViewRow In mydatagrid.Rows
If row.Cells("mycheckbox").Value = True Then
myArray.Add(row.Tag)
End If
Next
End Sub
End Class
'....then i'm calling it from another module:
Public Class myOtherModule
Public sub doit()
For Each value As String In myMainModule.myArray
Debug.Print(value)
Next
End Sub
End Class
You need to initialize your Array before you try to call it. Currently it is Nothing.
Public Class MyMainModule
Public Shared MyArray() As String
Public Shared Sub DoSomething()
Dim myList As New List(Of String)
For Each row As DataGridViewRow In mydatagrid.Rows
If row.Cells("mycheckbox").Value = True Then
myList.Add(row.Tag)
End If
Next
MyArray = myList.ToArray()
End Sub
End Class
Public Class MyOtherModule
Public Sub Foo()
MyMainModule.DoSomething()
For Each value As String In MyMainModule.MyArray
Debug.Print(value)
Next
End Sub
End Class
The other thing too is that you need to watch out for naming. I believe you got confused because you had a field called myArray, but also had a local variabled called myArray. You were working with the local variable that you newed up as List(Of T), not an array.

A singleton is created when a collection-type dependency property is assigned a value

I am attempting to create a dependency object in WPF of type List(Of) containing all the children of the control. Despite taking precautions outlined here it seems that a singleton is created when I assign a value to the property. When I assign a value the value is added multiplied by the number of instances of the class.
Here is my code:
Public Class Period
Inherits ListBox
'-------- PROPERTIES --------'
Private Property Subjects() As List(Of Subject)
Get
Return Me.GetValue(SubjectsProperty)
End Get
Set(value As List(Of Subject))
Me.SetValue(SubjectsProperty, value)
End Set
End Property
Private Shared ReadOnly SubjectsProperty As DependencyProperty = DependencyProperty.Register("Subjects", GetType(List(Of Subject)), GetType(Period), New FrameworkPropertyMetadata(New List(Of Subject)))
Public Sub New()
MyBase.New()
Me.Subjects = New List(Of Subject) 'Reason: stop it from adding values to defult value collection. See:http://msdn.microsoft.com/en-us/library/aa970563.aspx
End Sub
This subroutine is used to refresh the property when a child is added:
Private Sub RefreshSubjectsProperty()
Dim ListOfSubjects As New List(Of Subject)
Dim Subject As New Subject
Dim Entry As New PeriodEntry
For Each Entry In LogicalTreeHelper.GetChildren(Me)
Subject.SubjectName = Entry.SubjectName
Subject.Teacher = Entry.Teacher
Subject.Students = Entry.Students
ListOfSubjects.Add(Subject)
Next
Me.Subjects = ListOfSubjects
End Sub

Resources