Defining objects within a procedure

Anything that doesn't fit elsewhere.
Post Reply
mmoffitt

Defining objects within a procedure

Post 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.
ted
Site Admin
Posts: 6299
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post 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.
mmoffitt

Post 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)

}
Post Reply