سؤال

I want to add an instance variable programatically to an existing class using Refactoring Browser:

| theClass className |
className := #MyClass.
theClass := (RBClass existingNamed: className)
                model: (RBNamespace new classNamed: className; yourself);
                yourself.
theClass addInstanceVariable: 'testIVar'

but the class is not modified with the new instance variable, what I am missing?

هل كانت مفيدة؟

المحلول

You forgot to execute your refactoring. Try this

| model className theClass iVarName |
className := #MyClass.
iVarName := 'testIVar'.
model := RBNamespace new classNamed: className; yourself.
theClass := (RBClass existingNamed: className)
                model: model;
                yourself.
(RBAddInstanceVariableRefactoring 
        model: model
        variable: iVarName
        class: theClass) execute.

you may want to add authomatic accession methods (getter and setter) for your new instance variable

(RBCreateAccessorsForVariableRefactoring 
        model: model
        variable: iVarName
        class: theClass 
        classVariable: false) execute
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top