Uninitialized class variable

Uninitialized class variable

Postby Pixero » Sat Jun 03, 2017 8:21 pm

I'm trying to make a script remember the values the user puts in during a SketchUp session so that he wont have to re enter them every time the script is run.

When for example using:

Code: Select all
module Mymodule
  class Myclass

  def initialize
    #Setting default values
    @myvariable = 25.mm unless @myvariable

    #And so on......

the script works but wont remember the settings when run again.

When using @@myvariable = 25.mm unless @@myvariable
I get a "Uninitialized class variable" error message.

What am I doing wrong?
0
User avatar
Pixero 
Premium Member
Premium Member
 

Re: Uninitialized class variable

Postby sdmitch » Sat Jun 03, 2017 8:33 pm

Pixero wrote:I'm trying to make a script remember the values the user puts in during a SketchUp session so that he wont have to re enter them every time the script is run.

When for example using:

Code: Select all
module Mymodule
  class Myclass

  def initialize
    #Setting default values
    @myvariable = 25.mm unless @myvariable

    #And so on......

the script works but wont remember the settings when run again.

When using @@myvariable = 25.mm unless @@myvariable
I get a "Uninitialized class variable" error message.

What am I doing wrong?


You just need to make a dummy assignment to it just after class declaration.
Code: Select all
module Mymodule
  class Myclass
  @@myvariable=nil
  def initialize
    #Setting default values
    @@myvariable = 25.mm unless @myvariable

    #And so on......
0
Last edited by sdmitch on Sat Jun 03, 2017 9:27 pm, edited 1 time in total.
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch 
PluginStore Author
PluginStore Author
 

Re: Uninitialized class variable

Postby Pixero » Sat Jun 03, 2017 9:05 pm

Should it be @@myvariable = nil and then @myvariable with just one @ or was that a typo?
0
User avatar
Pixero 
Premium Member
Premium Member
 

Re: Uninitialized class variable

Postby sdmitch » Sat Jun 03, 2017 9:24 pm

Pixero wrote:Should it be @@myvariable = nil and then @myvariable with just one @ or was that a typo?


I just copied your code and added the @@myvariable=nil to it. The typo, @myvariable, was in your original code.

It should be @@myvariable everwhere.
0
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch 
PluginStore Author
PluginStore Author
 

Re: Uninitialized class variable

Postby Pixero » Sun Jun 04, 2017 1:23 pm

Thanks, that worked.
0
User avatar
Pixero 
Premium Member
Premium Member
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: askanon, primascape, SeungwooHan and 6 guests

Visit our sponsors: