Is it possible to declare and initialize variables within a loop?
Aim:
The structure of my current simulation model could be described best using the term "objects", so i thought about to restructure the code using a custom data type "module". there are 37 such modules which can be described with the same set of parameters. all module-parameters are stored in an array. now i'd like to initialize all modules within a loop.
i tried versions of:
Code: Select all
REM ***** BASIC *****
Option Explicit
Global names_sim_modules As Variant
Global Doc As Object
Global Sim As Object
Type module_input
range_module As Object 'range address sim - location of the module in notation Ax-Ay
range_info As Object 'range address sim - location of the module in notation Ax-Ay
range_source As Object 'range address iop - location of the inputs (yesterday, today, tomorrow)
range_output As Object 'range address sim
address_cb_upper As Object
address_cb_lower As Object
address_cb_right As Object
ratio_viz As Object 'to adjust the max. result/year values to 100%
type_viz As String '0=none, 1=lines, 2=bars
type_viz_default As String
color_viz As String 'color of the spark-diagrams
color_viz_default As String
active_module As Boolean '0=inactive, 1= active
show_module As Boolean '0=invisible, 1= visible
End Type
Sub init_sim
Dim sim_module As String
names_sim_modules = Array("TimeTransport","Demand","ProductionPv","RemainingDemand1")
For Each sim_module In names_sim_modules
init_sim_modules(sim_module)
Next sim_module
End Sub
Sub init_sim_modules(c_sim_module As String)
Doc = ThisComponent
Sim = Doc.Sheets(0)
Dim x As String
Dim y As String
x = c_sim_module
Dim sim_module As New module_input
With sim_module
.range_module = Sim.NamedRanges.getByName(c_sim_module).getReferredCells
y = "i_" & c_sim_module
.range_info = Sim.NamedRanges.getByName(y).getReferredCells
y = "s_" & c_sim_module
.range_source = Sim.NamedRanges.getByName(y).getReferredCells
y = "o_" & c_sim_module
.range_output = Sim.NamedRanges.getByName(y).getReferredCells
y = "cbu_" & c_sim_module
.address_cb_upper = Sim.NamedRanges.getByName(y).getReferredCells
y = "cbl_" & c_sim_module
.address_cb_lower = Sim.NamedRanges.getByName(y).getReferredCells
y = "cbr_" & c_sim_module
.address_cb_right = Sim.NamedRanges.getByName(y).getReferredCells
y = "r_" & c_sim_module
.ratio_viz = Sim.NamedRanges.getByName(y).getReferredCells
.type_viz = 1
.type_viz_default = 1
.color_viz = "grey"
.color_viz_default = "grey"
.active_module = 0
.show_module = 0
End With
End Sub
i found a vba-solution for a similar case (together with the advice not to use it...) but that might be irrelevant for StarBasic.
Thx in advance