Local object references
Local object references
Hi
Can I declare local object references in procedures and functions? Something like
proc p() { local objref ref
}
??
Thanks
Can I declare local object references in procedures and functions? Something like
proc p() { local objref ref
}
??
Thanks
-
- Site Admin
- Posts: 6305
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
define a new class to hide an objref in a proc or func
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
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.
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
}
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."
-
- Site Admin
- Posts: 6305
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
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
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
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: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.
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()
I am local!
I am global!
Cheers Johan
Extensions
That's really cool.
Another cool extension would be static localobj references like in C/C++ :-)
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
Why did this get removed in NEURON 5.8?Johan wrote: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: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.will produce the followig output: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()
I am local!
I am global!
Cheers Johan
-
- Site Admin
- Posts: 6305
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: define a new class to hide an objref in a proc or func
??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
Oh, I thought it is removed. In my case it does not work: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?
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
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.