Page 1 of 1

Defining objects within a procedure

Posted: Thu May 26, 2005 10:10 pm
by mmoffitt
In 5.7 I am not able to define objects like Matrix & Vector within procedures, at least not directly (I never tried in earlier versions).

Am I doing something wrong? Or, is that just considered poor coding form?

I noticed that I am able to define them indirectly by using execute(str), where str = "m = new Matrix(2,10)" for example.

Posted: Fri May 27, 2005 12:39 am
by ted
A variable must be declared to be an objref before it can be
used as an objref. Objrefs must be declared at the top level
of the interpreter. Statements that appear within a proc or
func are not at the top level of the interpreter.

So this works

Code: Select all

objref fap
proc foo() {
  fap = new Classname()
}
but this will generate an error msg

Code: Select all

proc foo() {
  objref fap
  fap = new Classname()
}
I believe that this is the way NEURON has worked
since objects were first added to the hoc interpreter.

Posted: Fri May 27, 2005 2:18 am
by mmoffitt
Thanks Ted. I did try the following work around and it seems to work.

proc foo() {

strdef tempstr
sprint(tempstr,"objref fap")
execute(tempstr)

sprint(tempstr,"fap = new Classname()")
execute(tempstr)

}