Page 1 of 1
Local object references
Posted: Sat Jun 04, 2005 11:06 am
by miller
Hi
Can I declare local object references in procedures and functions? Something like
proc p() { local objref ref
}
??
Thanks
define a new class to hide an objref in a proc or func
Posted: Sat Jun 04, 2005 12:36 pm
by ted
Unfortunately that won't work. The first declaration that a name is an objref
must occur outside of a proc or func. Once a name has been declared an
objref, it can appear in another objref statement inside a proc or func, like this
Code: Select all
objref ref
proc p() {
objref ref
}
If you really need to hide an object that is used inside a func or proc, put the func or
proc inside a new object class and make it public, but leave the objref private.
Code: Select all
begintemplate Hiddenobject
public p, test
objref ref, null // these will be private
proc p() { local i
// let's do something we can test later
ref = new List()
for i=0,$1-1 ref.append(new Vector())
}
proc test() { local i
print "ref is ", ref
if (ref!=null) {
print "its members are"
for i=0,ref.count()-1 print i, ref.object(i)
}
}
endtemplate Hiddenobject
objref foo
foo = new Hiddenobject()
print " "
print "Before calling foo.p(3)"
foo.test()
print " "
foo.p(3)
print " "
print "After calling foo.p(3)"
foo.test()
print " "
print "Now convince yourself that ref is hidden"
print "by typing ref or foo.ref at the oc> prompt."
Hidden objref
Posted: Sat Jun 04, 2005 12:44 pm
by miller
Thanks a lot for your answer.
It is a really good workaround.
Posted: Sat Jun 04, 2005 1:01 pm
by ted
Glad to help. Thanks for submitting your question to the NEURON
Forum, where it can benefit a wide audience.
Please be sure to cite NEURON if your work results in publications.
Of the papers currently listed at
http://www.neuron.yale.edu/neuron/bib/nrnpubs.html
the best general-purpose citation would be one of these:
Hines, M.L. and Carnevale, N.T. The NEURON simulation environment.
In: The Handbook of Brain Theory and Neural Networks, 2nd ed, edited by
M.A. Arbib. Cambridge, MA: MIT Press, 2003, pp. 769-773
Hines, M.L. and Carnevale, N.T. NEURON: a tool for neuroscientists.
The Neuroscientist 7:123-135, 2001
Hines, M.L. and Carnevale, N.T. The NEURON simulation environment.
Neural Computation 9:1179-1209, 1997
Re: define a new class to hide an objref in a proc or func
Posted: Wed Jun 08, 2005 4:49 am
by Johan
ted wrote:
If you really need to hide an object that is used inside a func or proc, put the func or
proc inside a new object class and make it public, but leave the objref private.
In my build, Neuron 5.7.159, I can declare local objects within procedures and functions. This is done with the identifier localobj. It works like a charm and has been a VERY helpfull extension to NEURON. The following hoc code:
Code: Select all
objref s
s = new String("I am global!")
proc foo1(){ localobj s
s = new String("I am local!")
print s.s
}
proc foo2(){
print s.s
}
foo1()
foo2()
will produce the followig output:
I am local!
I am global!
Cheers Johan
Extensions
Posted: Wed Jun 08, 2005 8:11 am
by miller
That's really cool.
Another cool extension would be static localobj references like in C/C++ :-)
Re: define a new class to hide an objref in a proc or func
Posted: Thu Mar 30, 2006 11:17 pm
by eacheon
Johan wrote:ted wrote:
If you really need to hide an object that is used inside a func or proc, put the func or
proc inside a new object class and make it public, but leave the objref private.
In my build, Neuron 5.7.159, I can declare local objects within procedures and functions. This is done with the identifier localobj. It works like a charm and has been a VERY helpfull extension to NEURON. The following hoc code:
Code: Select all
objref s
s = new String("I am global!")
proc foo1(){ localobj s
s = new String("I am local!")
print s.s
}
proc foo2(){
print s.s
}
foo1()
foo2()
will produce the followig output:
I am local!
I am global!
Cheers Johan
Why did this get removed in NEURON 5.8?
Re: define a new class to hide an objref in a proc or func
Posted: Fri Mar 31, 2006 10:42 am
by ted
eacheon wrote:Why did this get removed in NEURON 5.8?
??
It works in 5.8. Have you encountered an instance in which it doesn't?
Re: define a new class to hide an objref in a proc or func
Posted: Fri Mar 31, 2006 1:32 pm
by eacheon
ted wrote:eacheon wrote:Why did this get removed in NEURON 5.8?
??
It works in 5.8. Have you encountered an instance in which it doesn't?
Oh, I thought it is removed. In my case it does not work:
Code: Select all
20:29:53 xxw@alots:~/nrn/lgntc$ nrniv
NEURON -- Version 5.8 2005-10-14 12:36:20 Main (88)
by John W. Moore, Michael Hines, and Ted Carnevale
Duke and Yale University -- Copyright 1984-2005
oc>objref s
oc>s = new String("I am global!")
nrniv: String is not a template
near line 2
s = new String("I am global!")
Re: define a new class to hide an objref in a proc or func
Posted: Fri Mar 31, 2006 2:31 pm
by eacheon
eacheon wrote:ted wrote:eacheon wrote:Why did this get removed in NEURON 5.8?
??
It works in 5.8. Have you encountered an instance in which it doesn't?
Oh, sorry and thanks. String is defined in nrngui.hoc, if I start with nrngui it would be available.