[Solved] indirect declaration of variables in starbasic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
AndresSolar
Posts: 72
Joined: Sun Jun 26, 2016 5:42 am

[Solved] indirect declaration of variables in starbasic

Post by AndresSolar »

I couldn't find a related topic (other than related to excel) so i'm probably asking something naiv...

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
this (obviously) fails after the first loop.

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
Last edited by robleyd on Fri Jun 07, 2019 9:46 am, edited 1 time in total.
Reason: Tagged [Solved]
LO6.2 on OS-X Mojave 10.14.3
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: indirect declaration of variables in starbasic

Post by JeJe »

Not following you... if you want an array of modules...

Code: Select all




Global modules
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

   redim modules(36)
   for i = 0 to 36
   modules(i)=new module_input
   next
   
   i=0
   names_sim_modules = Array("TimeTransport","Demand","ProductionPv","RemainingDemand1")
   For Each sim_module In names_sim_modules
      init_sim_modules(sim_module,modules(i))
      i=i+1
   Next sim_module

	msgbox modules(2).color_viz
End Sub



Sub init_sim_modules(c_sim_module As String,sim_module)
    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


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
AndresSolar
Posts: 72
Joined: Sun Jun 26, 2016 5:42 am

Re: indirect declaration of variables in starbasic

Post by AndresSolar »

JeJe,

perfect. got it. 1st create the objects, then fill them.
Many thanks!!
LO6.2 on OS-X Mojave 10.14.3
AndresSolar
Posts: 72
Joined: Sun Jun 26, 2016 5:42 am

Re: [Solved] indirect declaration of variables in starbasic

Post by AndresSolar »

Hello JeJe,

just to avoid misinterpretation:
the first attempt had to fail because the named ranges for modules 2 and up were missing.
But as always learning. Thx again for your help

Code: Select all

Global sim_modules
Global setup_sim_modules As Variant
Global Doc As Object
Global Sim As Object

Type module_input
	name_module		  As String
    range_module      As Object
    range_info        As Object
    range_source      As Object
    range_output      As Object
    address_cb_upper  As Object
    address_cb_lower  As Object
    address_cb_right  As Object
    ratio_viz         As Object
    type_viz          As String
    type_viz_default  As String
    color_viz         As String
    color_viz_default As String     
    active_module 	  As Boolean
    show_module       As Boolean
End Type


Sub init_sim
	Dim module_name As String
	Dim i, x As Integer : x = 0
	setup_sim_modules = Array(  "TimeTransport"		,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "StatusPG"			,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "EnergyDemand1"		,"1"	,"1"	,"Blue"		,"Blue"		,"1"	,"1",_
							    "ProductionPV"		,"1"	,"1"	,"Orange"	,"Orange"	,"1"	,"1",_
							    "OvercapacityPV"	,"1"	,"1"	,"Yellow"	,"Yellow"	,"1"	,"1",_
							    "RemainingDemand1"	,"1"	,"1"	,"Blue"		,"Blue"		,"1"	,"1",_
			 				    "ChargingRatePV"	,"1"	,"1"	,"Green"	,"Green"	,"1"	,"1",_
							    "ChargingLossesPV"	,"1"	,"1"	,"Red"		,"Red"		,"1"	,"1",_
							    "RemainingOvercapPV","1"	,"1"	,"Yellow"	,"Yellow"	,"1"	,"1",_
							    "StateOfCharge1"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "ChargingRatePG"	,"1"	,"1"	,"Green"	,"Green"	,"1"	,"1",_
							    "ChargingLossesPG"	,"1"	,"1"	,"Red"		,"Red"		,"1"	,"1",_
							    "BypassRatePG"		,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "BypassLossesPG"	,"1"	,"1"	,"Red"		,"Red"		,"1"	,"1",_
							    "RemainingDemand2"	,"1"	,"1"	,"Blue"		,"Blue"		,"1"	,"1",_
							    "StateOfCharge2"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "DischargeRateMax"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
			 				    "DischargeRateUPS"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "DischargeLossesUPS","1"	,"1"	,"Red"		,"Red"		,"1"	,"1",_
							    "DischargeRateB"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
							    "DischargeLossesB"	,"1"	,"1"	,"Red"		,"Red"		,"1"	,"1",_
			 			        "StateOfCharge3"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
			 			        "RemainingDemand3"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
						        "EnergySold"		,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
						        "EnergyPurchased"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
						        "SuppliedByGenset"	,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1",_
						        "EnergyLost"		,"1"	,"1"	,"Grey"		,"Grey"		,"1"	,"1")

	ReDim sim_modules(Ubound(setup_sim_modules))
	
   	For i = 0 To Ubound(setup_sim_modules) / 7
   		sim_modules(i) = New module_input
		init_sim_modules(setup_sim_modules(x),sim_modules(i), x)
		x = x + 7
   	Next i
	
	msgbox sim_modules(0).color_viz
	setmode
End Sub


Sub init_sim_modules(range_name As String,sim_module, x)
    Doc = ThisComponent
    Sim = Doc.Sheets(0)
	With sim_module
		.name_module       = range_name
		.range_module      = Sim.NamedRanges.getByName(range_name).getReferredCells
		.range_info        = Sim.NamedRanges.getByName("i_"   & range_name).getReferredCells
		.range_source      = Sim.NamedRanges.getByName("s_"   & range_name).getReferredCells
		.range_output      = Sim.NamedRanges.getByName("o_"   & range_name).getReferredCells
		.address_cb_upper  = Sim.NamedRanges.getByName("cbu_" & range_name).getReferredCells
		.address_cb_lower  = Sim.NamedRanges.getByName("cbl_" & range_name).getReferredCells
		.address_cb_right  = Sim.NamedRanges.getByName("cbr_" & range_name).getReferredCells
		.ratio_viz         = Sim.NamedRanges.getByName("r_"   & range_name).getReferredCells
		.type_viz          = setup_sim_modules(x+1)
		.type_viz_default  = setup_sim_modules(x+2)
		.color_viz         = setup_sim_modules(x+3)
		.color_viz_default = setup_sim_modules(x+4)
		.active_module     = setup_sim_modules(x+5)
		.show_module       = setup_sim_modules(x+6)
	End With
End Sub


Sub setmode
	sim_modules(0).color_viz = "White"
	msgbox sim_modules(0).color_viz
End Sub
LO6.2 on OS-X Mojave 10.14.3
Post Reply