Embedding a Variable in a string C# Dotnet

  • From: "Jeff Berwick" <mailinglists@xxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 7 Jan 2009 18:13:27 -0500

Hi Listers,
 
Please bare with me while I attempt to explain what I want to do.
 
I want to build a string that has a variable name inside it.  Then, I want
to send that string to a function which will recognise the variable name,
embedded din the string, and replace it with its value.  Is this possible?
 
By way of an illustration:
 
private void stuff()
{
string firstname;
string lastname;
string result;
 
result = buildString();
MessageBox.Show(result.ToString());
}
 
private string buildString()
{
return "Here is the string with the two variables - firstname and lastname";
}
 
I also tried putting {} around the variable name but, as expected that
didn't work.  Any suggestions on an elegant solution?
 
Thx,
Jeff
 

Other related posts: