Saving Control Values to Class Object Property - vba

I have this userform that I has a bunch of controls on it, and the control name corresponds to a property name that i have defined inside OrderPrompts.
When the user clicks on the save button, I am trying to save all those control values back to the original object OrderPrompts.
Here's the order prompts classs
Option Explicit
Private pSKU As String
Private pWidth As String
Private pHeight As String
Private pDepth As String
Private pLeftSwing As String
Private pRightSwing As String
Private pLeftFinishedEnd As String
Private pRightFinishedEnd As String
Private pToeKickHeight As String
Private pAdjShelfQty As String
Private pLeftStileWidth As String
Private pRightStileWidth As String
Private pTopRailWidth As String
Private pBottomRailWidth As String
Private pExtendLeftFFDown As String
Private pExtendLeftFFUp As String
Private pExtendRightFFDown As String
Private pExtendRightFFUp As String
Private pExtendTopRail As String
Private pExtendBottomRail As String
Private pBayHeightCalc As String
Private pBay1Height As String
Private pBay2Height As String
Private pBay3Height As String
Private pBay4Height As String
Private pBay5Height As String
Private pBayWidthCalc As String
Private pBay1Width As String
Private pBay2Width As String
Private pBay3Width As String
Private pBay4Width As String
Private pBay5Width As String
Private pDrawerFrontCalc As String
Private pTopDrawerFront As String
Private pSecondDrawerFront As String
Private pThirdDrawerFront As String
Private pBottomDrawerFront As String
Public Property Get SKU() As String
SKU = pSKU
End Property
Public Property Let SKU(Value As String)
pSKU = Value
End Property
Public Property Get Width() As String
Width = pWidth
End Property
Public Property Let Width(Value As String)
pWidth = Value
End Property
Public Property Get Height() As String
Height = pHeight
End Property
Public Property Let Height(Value As String)
pHeight = Value
End Property
Public Property Get Depth() As String
Depth = pDepth
End Property
Public Property Let Depth(Value As String)
pDepth = Value
End Property
Public Property Get LeftSwing() As String
LeftSwing = pLeftSwing
End Property
Public Property Let LeftSwing(Value As String)
pLeftSwing = Value
End Property
Public Property Get RightSwing() As String
RightSwing = pRightSwing
End Property
Public Property Let RightSwing(Value As String)
pRightSwing = Value
End Property
Public Property Get LeftFinishedEnd() As String
LeftFinishedEnd = pLeftFinishedEnd
End Property
Public Property Let LeftFinishedEnd(Value As String)
pLeftFinishedEnd = Value
End Property
Public Property Get RightFinishedEnd() As String
RightFinishedEnd = pRightFinishedEnd
End Property
Public Property Let RightFinishedEnd(Value As String)
pRightFinishedEnd = Value
End Property
Public Property Get ToeKickHeight() As String
ToeKickHeight = pToeKickHeight
End Property
Public Property Let ToeKickHeight(Value As String)
pToeKickHeight = Value
End Property
Public Property Get AdjShelfQty() As String
AdjShelfQty = pAdjShelfQty
End Property
Public Property Let AdjShelfQty(Value As String)
pAdjShelfQty = Value
End Property
Public Property Get LeftStileWidth() As String
LeftStileWidth = pLeftStileWidth
End Property
Public Property Let LeftStileWidth(Value As String)
pLeftStileWidth = Value
End Property
Public Property Get RightStileWidth() As String
RightStileWidth = pRightStileWidth
End Property
Public Property Let RightStileWidth(Value As String)
pRightStileWidth = Value
End Property
Public Property Get TopRailWidth() As String
TopRailWidth = pTopRailWidth
End Property
Public Property Let TopRailWidth(Value As String)
pTopRailWidth = Value
End Property
Public Property Get BottomRailWidth() As String
BottomRailWidth = pBottomRailWidth
End Property
Public Property Let BottomRailWidth(Value As String)
pBottomRailWidth = Value
End Property
Public Property Get ExtendLeftFFDown() As String
ExtendLeftFFDown = pExtendLeftFFDown
End Property
Public Property Let ExtendLeftFFDown(Value As String)
pExtendLeftFFDown = Value
End Property
Public Property Get ExtendLeftFFUp() As String
ExtendLeftFFUp = pExtendLeftFFUp
End Property
Public Property Let ExtendLeftFFUp(Value As String)
pExtendLeftFFUp = Value
End Property
Public Property Get ExtendRightFFDown() As String
ExtendRightFFDown = pExtendRightFFDown
End Property
Public Property Let ExtendRightFFDown(Value As String)
pExtendRightFFDown = Value
End Property
Public Property Get ExtendRightFFUp() As String
ExtendRightFFUp = pExtendRightFFUp
End Property
Public Property Let ExtendRightFFUp(Value As String)
pExtendRightFFUp = Value
End Property
Public Property Get ExtendTopRail() As String
ExtendTopRail = pExtendTopRail
End Property
Public Property Let ExtendTopRail(Value As String)
pExtendTopRail = Value
End Property
Public Property Get ExtendBottomRail() As String
ExtendBottomRail = pExtendBottomRail
End Property
Public Property Let ExtendBottomRail(Value As String)
pExtendBottomRail = Value
End Property
Public Property Get BayHeightCalc() As String
BayHeightCalc = pBayHeightCalc
End Property
Public Property Let BayHeightCalc(Value As String)
pBayHeightCalc = Value
End Property
Public Property Get Bay1Height() As String
Bay1Height = pBay1Height
End Property
Public Property Let Bay1Height(Value As String)
pBay1Height = Value
End Property
Public Property Get Bay2Height() As String
Bay2Height = pBay2Height
End Property
Public Property Let Bay2Height(Value As String)
pBay2Height = Value
End Property
Public Property Get Bay3Height() As String
Bay3Height = pBay3Height
End Property
Public Property Let Bay3Height(Value As String)
pBay3Height = Value
End Property
Public Property Get Bay4Height() As String
Bay4Height = pBay4Height
End Property
Public Property Let Bay4Height(Value As String)
pBay4Height = Value
End Property
Public Property Get Bay5Height() As String
Bay5Height = pBay5Height
End Property
Public Property Let Bay5Height(Value As String)
pBay5Height = Value
End Property
Public Property Get BayWidthCalc() As String
BayWidthCalc = pBayWidthCalc
End Property
Public Property Let BayWidthCalc(Value As String)
pBayWidthCalc = Value
End Property
Public Property Get Bay1Width() As String
Bay1Width = pBay1Width
End Property
Public Property Let Bay1Width(Value As String)
pBay1Width = Value
End Property
Public Property Get Bay2Width() As String
Bay2Width = pBay2Width
End Property
Public Property Let Bay2Width(Value As String)
pBay2Width = Value
End Property
Public Property Get Bay3Width() As String
Bay3Width = pBay3Width
End Property
Public Property Let Bay3Width(Value As String)
pBay3Width = Value
End Property
Public Property Get Bay4Width() As String
Bay4Width = pBay4Width
End Property
Public Property Let Bay4Width(Value As String)
pBay4Width = Value
End Property
Public Property Get Bay5Width() As String
Bay5Width = pBay5Width
End Property
Public Property Let Bay5Width(Value As String)
pBay5Width = Value
End Property
Public Property Get DrawerFrontCalc() As String
DrawerFrontCalc = pDrawerFrontCalc
End Property
Public Property Let DrawerFrontCalc(Value As String)
pDrawerFrontCalc = Value
End Property
Public Property Get TopDrawerFront() As String
TopDrawerFront = pTopDrawerFront
End Property
Public Property Let TopDrawerFront(Value As String)
pTopDrawerFront = Value
End Property
Public Property Get SecondDrawerFront() As String
SecondDrawerFront = pSecondDrawerFront
End Property
Public Property Let SecondDrawerFront(Value As String)
pSecondDrawerFront = Value
End Property
Public Property Get ThirdDrawerFront() As String
ThirdDrawerFront = pThirdDrawerFront
End Property
Public Property Let ThirdDrawerFront(Value As String)
pThirdDrawerFront = Value
End Property
Public Property Get BottomDrawerFront() As String
BottomDrawerFront = pBottomDrawerFront
End Property
Public Property Let BottomDrawerFront(Value As String)
pBottomDrawerFront = Value
End Property
Here's how OrderPrompts is defined inside the form control
Public Property Get OrderPrompts() As clsOrderPromptRow
Set OrderPrompts = pOrderPrompts
End Property
Public Property Let OrderPrompts(Value As clsOrderPromptRow)
Set pOrderPrompts = Value
End Property
Here's the code I have for the button click
Private Sub btnSave_Click()
Dim Prompt As Control
Dim PageIndex As Long
For PageIndex = 0 To Me.TabControl.Pages.Count - 1
For Each Prompt In TabControl.Pages(PageIndex).Controls
'MsgBox (TypeName(Prompt))
'CallByName(Me.ProductPromptMapping, PromptControl.ControlName, VbGet)
Select Case TypeName(Prompt)
Case "TextBox"
CallByName Me.OrderPrompts, Prompt.Name, VbLet, Prompt.Text
Case "OptionButton"
CallByName Me.OrderPrompts, Prompt.Name, VbLet, Prompt.ControlFormat.Value
Case "CheckBox"
CallByName Me.OrderPrompts, Prompt.Name, VbLet, CStr(Prompt.Value)
Case "ComboBox"
CallByName Me.OrderPrompts, Prompt.Name, VbLet, Prompt.Value
End Select
Next
Next
MsgBox (OrderPrompts.Width)
Me.Hide
End Sub
The problem i am having is with saving the Checkbox, Combobox, and OptionButton values back to the object. The textbox value works just fine. So I am not sure what to fix. I keep getting either a type mismatch error or null not supported.
Any help is appreciated. Thanks in advance.

The short answer is to use Prompt.Object.Value for all the controls.
Public Sub PrintControls()
Dim Prompt As Control
Dim PageIndex As Long
For PageIndex = 0 To Me.TabControl.Pages.Count - 1
For Each Prompt In TabControl.Pages(PageIndex).Controls
Debug.Print "Name:", Prompt.Name, "Value:", Prompt.Object.Value
Next
Next
End Sub
I really liked your concept so I wrote my own class to Save and Load control values. My class saves references to the controls and their values for easy retrieval. Download Workbook
When the Save button is clicked a reference to each control on the MultiPage is saved into a Scripting Dictionary. The Dictionary is then added to an ArrayList and the Dictionary's index in the ArrayList is added to a ListBox. When the ListBox is clicked the current settings are saved and the previous settings, corresponding to the Listbox's Value, are loaded.
Class: SettingsDictionaryClass
Option Explicit
Private ControlSetting As Object
Private mTabControl As MSForms.MultiPage
Sub Init(TabControl As MSForms.MultiPage)
Set mTabControl = TabControl
Set ControlSetting = CreateObject("System.Collections.ArrayList")
End Sub
Function Save() As Long
Dim subDict As Object: Set subDict = CreateObject("Scripting.Dictionary")
Dim ctrl As MSForms.Control
Dim PageIndex As Long
With mTabControl
For PageIndex = 0 To .Pages.Count - 1
For Each ctrl In .Pages(PageIndex).Controls
subDict.Add ctrl, ctrl.Object.Value
Next
Next
End With
Save = ControlSetting.Count
ControlSetting.Add subDict
End Function
Sub LoadValues(ListIndex As Long, Optional SaveCurrent As Boolean)
Dim ctrl
Dim Settings As Object
If SaveSetting Then Save
Set Settings = ControlSetting.Item(ListIndex)
For Each ctrl In Settings
ctrl.Value = Settings(ctrl)
Next
End Sub
Userform: Userform1
Option Explicit
Private SettingsDictionary As SettingsDictionaryClass
Private Sub btnSave_Click()
lboSettings.AddItem SettingsDictionary.Save
End Sub
Private Sub lboSettings_Click()
SettingsDictionary.LoadValues lboSettings.Value, True
End Sub
Private Sub UserForm_Initialize()
Set SettingsDictionary = New SettingsDictionaryClass
SettingsDictionary.Init TabControl
End Sub

Related

How to hide expand button in a propertygrid in Windows Forms

I have written my on editor for a property in a propertyGrid. Everything works as expected, but for best user experience i want to hide the expand button on the propertygrid for this property. Can someone tell me how to do that?
I am using a custom typeconverter
Public Class PropertyLanguageSetupEditor
Inherits UITypeEditor
Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
Try
Dim svc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
Dim settings As LanguageSettings = value
Dim oldSettings As LanguageSettings = CType(value, LanguageSettings).Clone
If Not svc Is Nothing And Not settings Is Nothing Then
Dim form As New PropertyLanguageSetupWindow(settings)
If svc.ShowDialog(form) = DialogResult.OK Then
value = form.Data
Else
value = oldSettings
End If
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return MyBase.EditValue(context, provider, value)
End Function
End Class
And this is de property which uses the typeConverter
<DisplayName("Properties Translation Settings"), Editor(GetType(PropertyLanguageSetupEditor), GetType(System.Drawing.Design.UITypeEditor)), TypeConverter(GetType(BrowsableTypeConverter)),
Description("Settings for the output text"), BrowsableTypeConverter.BrowsableLabelStyleAttribute(BrowsableTypeConverter.LabelStyle.lsEllipsis),
Category("10 - DXF Export"), Browsable(True)>
Public Property TranslationSettings As LanguageSettings = New LanguageSettings
My BrowsAbleTypeConverter
Public Class BrowsableTypeConverter
Inherits ExpandableObjectConverter
Public Enum LabelStyle
lsNormal
lsTypeName
lsEllipsis
lsText
End Enum
Public Class BrowsableLabelStyleAttribute
Inherits System.Attribute
Private eLabelStyle As LabelStyle = LabelStyle.lsEllipsis
Public Sub New(ByVal LabelStyle As LabelStyle)
eLabelStyle = LabelStyle
End Sub
Public Property LabelStyle() As LabelStyle
Get
Return eLabelStyle
End Get
Set(ByVal value As LabelStyle)
eLabelStyle = value
End Set
End Property
End Class
Public Class BrowsableLabelTextAttribute
Inherits System.Attribute
Private strText As String = ""
Public Sub New(ByVal value As String)
strText = value
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal value As String)
strText = value
End Set
End Property
End Class
Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
Return True
End Function
Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
Dim Style As BrowsableLabelStyleAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelStyleAttribute))
If Not Style Is Nothing Then
Select Case Style.LabelStyle
Case LabelStyle.lsNormal
Return MyBase.ConvertTo(context, culture, value, destinationType)
Case LabelStyle.lsTypeName
Return "(" & value.GetType.Name & ")"
Case LabelStyle.lsEllipsis
Return "(...)"
Case LabelStyle.lsText
Dim text As BrowsableLabelTextAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelTextAttribute))
If text IsNot Nothing Then
Return text.Text
End If
End Select
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class
Your TypeConverter derives from ExpandableObjectConverter, which means subproperties will be visible because of its GetPropertiesSupported method returning true.
In your TypeConverter, you simply need to override GetPropertiesSupported and return false.

How to get all json values with newtonsoft

I'm having a hard time getting through all the JSON values ​​of a string, can anyone help me?
I'm only getting a single value, where am I going wrong?
My codes
Dim address As String = "http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim json = (reader.ReadToEnd)
Dim objs As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim objsSorteio As Sorteio = JsonConvert.DeserializeObject(Of Sorteio)(json)
For Each nums In objsSorteio.Premios
MsgBox(nums.ToString)
Next
Classes
Public Class Premio
Public Property Faixa() As String
Get
Return m_Faixa
End Get
Set(value As String)
m_Faixa = Value
End Set
End Property
Private m_Faixa As String
Public Property NumeroGanhadores() As Integer
Get
Return m_NumeroGanhadores
End Get
Set(value As Integer)
m_NumeroGanhadores = Value
End Set
End Property
Private m_NumeroGanhadores As Integer
Public Property Valor() As Double
Get
Return m_Valor
End Get
Set(value As Double)
m_Valor = Value
End Set
End Property
Private m_Valor As Double
End Class
Public Class Sorteio
Public Property NumSorteio() As Integer
Get
Return m_NumSorteio
End Get
Set(value As Integer)
m_NumSorteio = Value
End Set
End Property
Private m_NumSorteio As Integer
Public Property Numeros() As List(Of Integer)
Get
Return m_Numeros
End Get
Set(value As List(Of Integer))
m_Numeros = Value
End Set
End Property
Private m_Numeros As List(Of Integer)
Public Property Premios() As List(Of Premio)
Get
Return m_Premios
End Get
Set(value As List(Of Premio))
m_Premios = Value
End Set
End Property
Private m_Premios As List(Of Premio)
Public Property Ganhadores() As List(Of Object)
Get
Return m_Ganhadores
End Get
Set(value As List(Of Object))
m_Ganhadores = Value
End Set
End Property
Private m_Ganhadores As List(Of Object)
End Class
Public Class RootObject
Public Property NumeroConcurso() As Integer
Get
Return m_NumeroConcurso
End Get
Set(value As Integer)
m_NumeroConcurso = Value
End Set
End Property
Private m_NumeroConcurso As Integer
Public Property Acumulou() As Boolean
Get
Return m_Acumulou
End Get
Set(value As Boolean)
m_Acumulou = Value
End Set
End Property
Private m_Acumulou As Boolean
Public Property EstimativaPremio() As Double
Get
Return m_EstimativaPremio
End Get
Set(value As Double)
m_EstimativaPremio = Value
End Set
End Property
Private m_EstimativaPremio As Double
Public Property ValorAcumulado() As Double
Get
Return m_ValorAcumulado
End Get
Set(value As Double)
m_ValorAcumulado = Value
End Set
End Property
Private m_ValorAcumulado As Double
Public Property Data() As String
Get
Return m_Data
End Get
Set(value As String)
m_Data = Value
End Set
End Property
Private m_Data As String
Public Property RealizadoEm() As String
Get
Return m_RealizadoEm
End Get
Set(value As String)
m_RealizadoEm = Value
End Set
End Property
Private m_RealizadoEm As String
Public Property DescricaoAcumuladoOutro() As String
Get
Return m_DescricaoAcumuladoOutro
End Get
Set(value As String)
m_DescricaoAcumuladoOutro = Value
End Set
End Property
Private m_DescricaoAcumuladoOutro As String
Public Property ValorAcumuladoOutro() As Double
Get
Return m_ValorAcumuladoOutro
End Get
Set(value As Double)
m_ValorAcumuladoOutro = Value
End Set
End Property
Private m_ValorAcumuladoOutro As Double
Public Property DataProximo() As String
Get
Return m_DataProximo
End Get
Set(value As String)
m_DataProximo = Value
End Set
End Property
Private m_DataProximo As String
Public Property ValorAcumuladoEspecial() As Double
Get
Return m_ValorAcumuladoEspecial
End Get
Set(value As Double)
m_ValorAcumuladoEspecial = Value
End Set
End Property
Private m_ValorAcumuladoEspecial As Double
Public Property Arrecadacao() As Double
Get
Return m_Arrecadacao
End Get
Set(value As Double)
m_Arrecadacao = Value
End Set
End Property
Private m_Arrecadacao As Double
Public Property Sorteios() As List(Of Sorteio)
Get
Return m_Sorteios
End Get
Set(value As List(Of Sorteio))
m_Sorteios = Value
End Set
End Property
Private m_Sorteios As List(Of Sorteio)
End Class
I can not go through all the "Numeros" and also "Premios"
You are over complicating this unnecessarily.
using the URL provided the exposed json data was copied and plugged into a utility site like http://jsonutils.com/. It generated the models with auto properties. Note that it converted collections to array as apposed to Lists like you have in your classes.
Public Class Premio
Public Property Faixa As String
Public Property NumeroGanhadores As Integer
Public Property Valor As Double
End Class
Public Class Sorteio
Public Property NumSorteio As Integer
Public Property Numeros As Integer()
Public Property Premios As Premio()
Public Property Ganhadores As Object()
End Class
Public Class RootObject
Public Property NumeroConcurso As Integer
Public Property Acumulou As Boolean
Public Property EstimativaPremio As Double
Public Property ValorAcumulado As Double
Public Property Data As String
Public Property RealizadoEm As String
Public Property DescricaoAcumuladoOutro As String
Public Property ValorAcumuladoOutro As Double
Public Property DataProximo As String
Public Property ValorAcumuladoEspecial As Double
Public Property Arrecadacao As Double
Public Property Sorteios As Sorteio()
End Class
Basically the same as what you had originally with better readability. Feel free to convert tha arrays back to list if that is your preference.
The first desrialization is already giving you the necessary object. Drill into the properties in order to access the values you need.
'''other code removed for brevity
Dim objs As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim objsSorteioList As List(Of Sorteio) = objs.Sorteios.ToList()
For Each objsSorteio In objsSorteioList
For Each prems In objsSorteio.Premios
MsgBox(prems.ToString)
Next
For Each nums In objsSorteio.Numeros
MsgBox(nums.ToString)
Next
Next

Declare and and define Property in VB

I have two Class and I want to save my data into arrays form text box like this:
Students.Name(txtID.Text-1).MathMark = txtMark.Text
but I get error: Object reference not set to an instance of an object
my code is:
Dim StudentsNumber as Integer = txtstdnum.Text
Dim Students as New StudentsInf(StudentsNumber)
Students.Name(txtID.Text-1).MathMark = txtMark.Text
Public Class StudentsInf
Private mName() As String
Sub New(ByVal StudentNumbers As Integer)
ReDim mName(StudentNumbers-1)
End Sub
Public Property Name(ByVal Index As Integer) As LessonsMark
Get
Return mName(Index)
End Get
Set(ByVal Value As LessonsMark)
mName(Index) = Value
End Set
End Property
End Class
Public Class LessonsMark
Private mMathMark() As Object
Public Property MathMark() As Object
Get
Return mMathMark
End Get
Set(ByVal Value As Object)
mMathMark = Value
End Set
End Property
End Class
This:
Private mName() As String
needs to be:
Private mName() As LessonsMark
then you have to create the objects in your constructor, something like:
Sub New(ByVal StudentNumbers As Integer)
ReDim mName(StudentNumbers - 1)
For i As Integer = 0 To StudentNumbers - 1
mName(i) = New LessonsMark()
Next
End Sub
then it looks like your LessonsMark class is declaring an array of objects when it looks like it should be just a string property:
Public Class LessonsMark
Private mMathMark As String
Public Property MathMark As String
Get
Return mMathMark
End Get
Set(ByVal Value As String)
mMathMark = Value
End Set
End Property
End Class

Array in object vba

I have 2 classes
Class1
Private pClass2Arr(10) as Class2
Public Property Get class2Arr() as Class2()
class2Arr = pClass2Arr
End Property
Public Property Let class2Arr(mClass2Arr() as Class2)
pClass2Arr = mClass2Arr
End Property
Class2
Private pStr1 as String
Private pStr2 as String
Public Property Get str1() as String
str1 = pStr1
End Property
Public Property Let str1(mStr1 as String)
pStr1 = mStr1
End Property
Public Property Get str2() as String
str2 = pStr2
End Property
Public Property Let str2(mStr2 as String)
pStr2 = mStr2
End Property
And Id' like to do
Dim a as Class1
Set a = New Class1
a.class2Arr(0).str1 = "test"
Debug.Print a.class2Arr(0).str1
And I have an error because get property on class2Arr have no arguments
You just declared array, but not initialized it Private pClass2Arr(10) as Class2. I've added Private Sub Class_Initialize() event in Class1:
Private pClass2Arr(10) As Class2
Public Property Get class2Arr() As Class2()
class2Arr = pClass2Arr
End Property
Public Property Let class2Arr(mClass2Arr() As Class2)
pClass2Arr = mClass2Arr
End Property
Private Sub Class_Initialize()
Dim i As Byte ' change Byte to Integer if your array contains more than 255 elements
For i = LBound(pClass2Arr) To UBound(pClass2Arr)
Set pClass2Arr(i) = New Class2
Next
End Sub
and then you can use it like this:
Sub test()
Dim a As Class1
Set a = New Class1
a.class2Arr()(0).str1 = "test"
Debug.Print a.class2Arr()(0).str1
End Sub

how to loop inside every tables/columns in a dbml file to create new partial class with more information about every columns

I have a class that add extra information about a column for linq2sql (see code below)
right now, I have to explicitly tell what column I want that info on, how would you put that code with a loop on every column in every table from a dbml file?
I was doing my test on a very very small DB, now I have to implement it on a much more bigger database and I really don't want to do it manually for every tables/columns
it will take hours.
how it's being used:
Partial Class Contact ''contact is a table inside a dbml file.
Private _ContactIDColumn As ExtraColumnInfo
Private _ContactNameColumn As ExtraColumnInfo
Private _ContactEmailColumn As ExtraColumnInfo
Private _ContactTypeIDColumn As ExtraColumnInfo
Private Sub OnCreated()
initColumnInfo()
End Sub
Private Sub initColumnInfo()
Dim prop As PropertyInfo
prop = Me.GetType.GetProperty("ContactID")
_ContactIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactName")
_ContactNameColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactEmail")
_ContactEmailColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactTypeID")
_ContactTypeIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Nothing
End Sub
Public ReadOnly Property ContactIDColumn() As ExtraColumnInfo
Get
Return _ContactIDColumn
End Get
End Property
Public ReadOnly Property ContactNameColumn() As ExtraColumnInfo
Get
Return _ContactNameColumn
End Get
End Property
Public ReadOnly Property ContactEmailColumn() As ExtraColumnInfo
Get
Return _ContactEmailColumn
End Get
End Property
Public ReadOnly Property ContactTypeIDColumn() As ExtraColumnInfo
Get
Return _ContactTypeIDColumn
End Get
End Property
End Class
what is ExtraColumnInfo:
Public Class ExtraColumnInfo
Private _column As ColumnAttribute
Private _MaxLength As Nullable(Of Integer)
Private _Precision As Nullable(Of Integer)
Private _Scale As Nullable(Of Integer)
Private _IsIdentity As Boolean
Private _IsUniqueIdentifier As Boolean
Sub New(ByVal column As ColumnAttribute)
Dim match As Match
_column = column
Match = Regex.Match(DBType, "^.*\((\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_MaxLength = If(match.Success, Convert.ToInt32(match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Precision = If(Match.Success, Convert.ToInt32(Match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Scale = If(match.Success, Convert.ToInt32(match.Groups(2).Value), Nothing)
match = Regex.Match(DBType, "^.*\bIDENTITY\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsIdentity = match.Success
match = Regex.Match(DBType, "^.*\bUniqueIdentifier\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsUniqueIdentifier = match.Success
End Sub
'others available information
'AutoSync
'Expression
'IsVersion
'Name
'Storage
'TypeId
'UpdateCheck
'maybe more
Public ReadOnly Property CanBeNull() As Boolean
Get
Return _column.CanBeNull
End Get
End Property
Public ReadOnly Property IsDbGenerated() As Boolean
Get
Return _column.IsDbGenerated
End Get
End Property
Public ReadOnly Property IsPrimaryKey() As Boolean
Get
Return _column.IsPrimaryKey
End Get
End Property
Public ReadOnly Property DBType() As String
Get
Return _column.DbType
End Get
End Property
Public ReadOnly Property MaxLength() As System.Nullable(Of Integer)
Get
Return _MaxLength
End Get
End Property
Public ReadOnly Property Precision() As System.Nullable(Of Integer)
Get
Return _Precision
End Get
End Property
Public ReadOnly Property Scale() As System.Nullable(Of Integer)
Get
Return _Scale
End Get
End Property
Public ReadOnly Property IsIdentity() As Boolean
Get
Return _IsIdentity
End Get
End Property
Public ReadOnly Property IsUniqueIdentifier() As Boolean
Get
Return _IsUniqueIdentifier
End Get
End Property
End Class
The only thing that comes to mind, and it's a far from optimal solution, would be to include this functionality into a base class and declare partials for all of the generated classes so that they inherit from your base class.
I'm sure there are also other ways to do this, but none that I know of that are native to the designer.
I took the T4 template and modified it a little to automatically put inside the generated class the column information,
in the import I added
Imports System.Reflection
Imports System.Text.RegularExpressions
at line 228, added these line:
<# foreach(Column column in class1.Columns) {#>
Private _<#=column.Member#>Column As ExtraColumnInfo
Public ReadOnly Property <#=column.Member#>Column() As ExtraColumnInfo
Get
Return _<#=column.Member#>Column
End Get
End Property
<# }#>
at line 298 I added
Dim prop As PropertyInfo
<# foreach(Column column in class1.Columns) {#>
prop = Me.GetType.GetProperty("<#=column.Member#>")
_<#=column.Member#>Column = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
<# }#>
at the end of the file, I added my own class