RE: Embedding a Variable in a string C# Dotnet

By the way, I think what Jeff is interested in is called string
interpolation.  Perl and Ruby have built-in language support for it (no
special function or library is required).  On the .NET platform, the Boo
language
http://boo.codehaus.org
supports this with syntax similar to Ruby.  One can embed
${VariableName} in a string to have that variable interpolated in-line.

Jamal

On
Fri, 9 Jan 2009, Jamal Mazrui wrote:

> Date: Fri, 9 Jan 2009 10:52:23 -0500 (EST)
> From: Jamal Mazrui <empower@xxxxxxxxx>
> Reply-To: programmingblind@xxxxxxxxxxxxx
> To: programmingblind@xxxxxxxxxxxxx
> Subject: RE: Embedding a Variable in a string C# Dotnet
>
> Jeff,
> I hope you got my message referring you to this web page:
> http://msdn.microsoft.com/en-us/library/txafckwd.aspx
>
> I do not think the problem you described may be solved via reflection
> because, as far as I know, it exposes class and instance members, but not
> local variables.  If someone can post a reflection solution, I would be
> glad to be corrected on this since I want to learn more about such
> techniques in .NET.
>
> Although you were posting sample code, let me make sure you are aware that
> your BuildString function would not even have access to the firstname and
> lastname variables, since they are not instance members or local
> parameters.  Based on my reading of the web page mentioned, I think the
> following line may do what you want:
>
> string result = String.Format("Here is the string with the two variables -
> {0} and {1}", firstname, lastname);
>
> If you want to use a seperate function to build the string, I think the
> following may work:
>
> string template = String.Format("Here is the string with the two variables
> - {0} and {1}";
> string result = BuildString(template, firstname, lastname);
>
> string BuildString(string template, string paramfirst, string paramlast) {
> return String.Format(template, paramfirst, paramlast);
> }
>
>
>
> Jamal
>
> __________
> View the list's information and change your settings at
> http://www.freelists.org/list/programmingblind
>
__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind

Other related posts: