DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

How to modify global variable in a function?

Solved!
Go to solution

Hello,

 

I want to modify a globalvariable in a function, at first I did it this way:

 

class Global_output_class

GlobalDim("Correlation_Status,fail_part,End_Exp")
dim pouet

Correlation_Status = 12
Call Correlation()
pouet = Correlation_Status

Function Correlation()
  Dim Global_output_class_sub
  Set Global_output_class_sub = new Global_output_class
  
  Correlation_Status = 1
  fail_part = 2
  End_Exp = 3
  
  
  Global_output_class_sub.CorrelationStatus = Correlation_Status
  Global_output_class_sub.failpart = fail_part
  Global_output_class_sub.EndExp = End_Exp
  set Correlation = Global_output_class_sub
End function

 

 

In this case: correlation_status receive the value 12, then I go to my function correlationn() where it became 1

Then it goes out of the subfunction and takes the previous value from the program(12) ( I dont want that)

 

To solve the problem I made it this way:

 

class Global_output_class
public CorrelationStatus
public failpart
public EndExp
end class 

GlobalDim("Correlation_Status,fail_part,End_Exp")

Correlation_Status = 12
Set Global_Output = Correlation()
Correlation_Status = Global_Output.CorrelationStatus
fail_part = Global_Output.failpart
End_Exp = Global_Output.EndExp
pouet = Correlation_Status

Function Correlation()
  Dim Global_output_class_sub
  Set Global_output_class_sub = new Global_output_class
  
  Correlation_Status = 1
  fail_part = 2
  End_Exp = 3
  
  
  Global_output_class_sub.CorrelationStatus = Correlation_Status
  Global_output_class_sub.failpart = fail_part
  Global_output_class_sub.EndExp = End_Exp
  set Correlation = Global_output_class_sub
End function

 

This way my global value are recopied in themselves after leaving the subprogram

 

I got a lot of variables, is there any easier way so the global variable modified in a function keep the value after leaving the function?

 

Thanks for help,

 

Fred

0 Kudos
Message 1 of 4
(4,918 Views)
Solution
Accepted by topic author fmanuel

Hi Fred,

 

it is possible to use a global defined variable but the better way is to use to use a funtion call (or procedure call) with parameters. Please find first the good solution for a funcion call with parameter and the sub-optimal way with an global valiable:

 

dim oParameter
set oParameter = new cGlobal_output_class

oParameter.Correlation_Status = 12

msgbox "Correlation_Status before Call Correlation: " & oParameter.Correlation_Status
Call Correlation(oParameter)
msgbox "Correlation_Status after Call Correlation: " & oParameter.Correlation_Status

'-------------------------------------------------------------------------------
Function Correlation(oPara)
  msgbox "Correlation_Status in the FUNCTION before change: " & oPara.Correlation_Status
  oPara.Correlation_Status = 1
  oPara.fail_part = 2
  oPara.End_Exp = 3
  msgbox "Correlation_Status in the FUNCTION after change: " & oPara.Correlation_Status
End function

'-------------------------------------------------------------------------------
class cGlobal_output_class
  dim Correlation_Status,fail_part,End_Exp
end class

 

 

call GlobalDim("oPouet")

dim oPouet
set oPouet = new cGlobal_output_class

oPouet.Correlation_Status = 12

msgbox "Correlation_Status before Call Correlation: " & oPouet.Correlation_Status
Call Correlation()
msgbox "Correlation_Status before Call Correlation: " & oPouet.Correlation_Status

'-------------------------------------------------------------------------------
Function Correlation()
  msgbox "Correlation_Status in the FUNCTION before change: " & oPouet.Correlation_Status
  oPouet.Correlation_Status = 1
  oPouet.fail_part = 2
  oPouet.End_Exp = 3
  msgbox "Correlation_Status in the FUNCTION after change: " & oPouet.Correlation_Status
End function

'-------------------------------------------------------------------------------
class cGlobal_output_class
  dim Correlation_Status,fail_part,End_Exp
end class

 

Greetings

Walter

 

0 Kudos
Message 2 of 4
(4,907 Views)

Thanks, the secund seems much better for me 🙂

 

I just wonder, what means the little "o" in "oPouet" and the little "c" in "cGlobal_output_class"?

0 Kudos
Message 3 of 4
(4,902 Views)

I put always the type of a variable in front of the name (c is a class, o is an object, i is integer, ...)

 

Greetings

Walter

0 Kudos
Message 4 of 4
(4,897 Views)