I am having some problems when I define a macro whose content has the lstlisting environment. For example, (assume ! is the escape char inside the lstlisting environment)
\def\mycode#1{
\begin{lstlisting}
class Person {
private int !{\bf #1}!;
int getAge(){
return !#1!;
}
}
\end{lstlisting}
}
When I use \mycode{age} I get an error.
Is there any way to fix this (note that I've tried lrbox environment but without success)?
What I really want is some macro definition stuff that simply replaces the arguments in the text and copies the resulting content to where the macro is called. That is, where the following line appeared
\mycode{age}
LaTeX would replace it by
\begin{lstlisting}
class Person {
private int !{\bf age}!;
int getAge(){
return age;
}
}
\end{lstlisting}
and start processing of \begin{lstlisting} ... \end{lstlisting}.
This should be simple, but unfortunately it seems too complicated (at least for me).

lstlistingsis likeverbatim, so it cannot go in the argument to another command. – egreg Jan 24 '12 at 13:21