Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Accessing an array within a class using itcl

Posted by lanceh (lanceh), 11 April 2008
Having some trouble creating an array within a class using incr tcl.

Could someone point out what I'm overlooking?

package require Itcl
namespace import itcl::*

class pm_modules {
     public variable primary_status
     public variable redundant_status
     public variable health_status
     
     constructor {{ps ""} {rs ""} {hs ""}} {
           set primary_status $ps
           set redundant_status $rs
           set health_status $hs
     }
}

class chassis {
     public variable mfg_name
     public variable prod_name
     public variable arr_chassis

     constructor {{mfg ""} {prod ""}} {
           set mfg_name $mfg
           set prod_name $prod
           array set arr_chassis {
                 pm_modules pm1
                 pm_modules pm2
           }
           parray arr_chassis
     }
     method set_arr_chassis {} {
           set arr_chassis(pm_modules pm1) [] ;#How to set variable in class pm_modules?
           set arr_chassis(pm_modules pm2) [] ;#How to set variable in class pm_modules?
#            parray arr_chassis
     }
     method get_arr_chassis {} {
           return [array get arr_chassis]
     }
}

#Create instance.
     chassis unit1
#Set then print value for chassis's mfg_name.
     unit1 configure -mfg_name "Simon"
     puts "mfg: [unit1 cget -mfg_name]"
#Set then print value for instance pm1 and pm2.
     unit1 set_arr_chassis

Posted by admin (Graham Ellis), 13 April 2008
I think you left some square brackets out!

Here's an updated version of your script which creates an array within the object; I've also printed out the array names.

Code:
package require Itcl
namespace import itcl::*

class pm_modules {
public variable primary_status
public variable redundant_status
public variable health_status
 
constructor {{ps ""} {rs ""} {hs ""}} {
 set primary_status $ps
 set redundant_status $rs
 set health_status $hs
}
}

class chassis {
public variable mfg_name
public variable prod_name
public variable arr_chassis

constructor {{mfg ""} {prod ""}} {
 set mfg_name $mfg
 set prod_name $prod
 array set arr_chassis {
  pm_modules pm1
  pm_modules pm2
 }
 parray arr_chassis
}
method set_arr_chassis {} {
 set ss [pm_modules pm1]
 set arr_chassis($ss) sv
 set ss [pm_modules pm2]
 set arr_chassis($ss) sv
}
method get_arr_chassis {} {
 return [array names arr_chassis]
}
}

#Create instance.
chassis unit1
#Set then print value for chassis's mfg_name.
unit1 configure -mfg_name "Simon"
puts "mfg: [unit1 cget -mfg_name]"
#Set then print value for instance pm1 and pm2.
unit1 set_arr_chassis
puts [unit1 get_arr_chassis]


Posted by lanceh (lanceh), 14 April 2008
Thanks Graham.

How can I set the public variables in the class pm_modules...like primary_status, redundant_status, and health_status...if the code to set them is called from within the method set_arr_chassis?




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho