1

I want to assign an object to a reg variable, but I don't know if I could do it, and if yes, how can I do it, what is the syntax?

For example, I have a class Var:

    class Var {
        constructor {Name values order} {} {
            set mName $Name  
            set mValues $values
            set mOrder $order            
        }
        destructor {
        }
        public method GetName {} {
             return $mName 
        }
        public variable mOrder
        public variable mName
        public variable mValues   
  }

and an object:

 Var::var_

Can I assign an object var to the reg variable?

Something like that:

reg set var/var_ Var::var_
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215

1 Answers1

2

You can store the name of an [incr Tcl] object in any variable. Just assign it as normal with set.

A class variable can be referred to by qualified name: cls::var

If you want a reference to an instance variable that is usable outside the methods of its class, you should use itcl::scope within a method (or the constructor) to generate the token. The format of the token returned is not well documented (and is liable to change).


Trying some of these things out:

package req Itcl
itcl::class Var {
    constructor {Name values order} {} {
        set mName $Name  
        set mValues $values
        set mOrder $order            
    }
    destructor {}
    public method GetName {} {
        return $mName 
    }
    public method GetNameVar {} {
        return [itcl::scope mName]
    }
    public variable mOrder
    public variable mName
    public variable mValues   
}
set foo [Var var_ a b c]
puts $foo
puts [$foo GetName]
append [$foo GetNameVar] [$foo GetName] "rdvark"
puts [var_ GetName]
puts [var_ GetNameVar]

On my system, I get this output:

var_
a
aardvark
@itcl ::var_ ::Var::mName
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • I need to save an object in reg variable, cos when i am saving database, i also saving reg variables,and when i make load to database, i want to get all objects that i created before. – Tamara Bozz Apr 24 '12 at 14:21