help please! when a string contains variable references

  • Thread starter Thread starter CCLeasing
  • Start date Start date
C

CCLeasing

I have a variable string, called formatstring The general format of the
string is : -

"somevariable + "some string text" + some variable2 + "some more text"
.... "

I want to use this variable as the argument for writeline like so : -

writeline(formatstring);

the problem i have is that writeline treats the variable formatstring
as a literal string, and doesn't realise that it includes variables
that needs replacing.

how do i get around this?
 
Maybe I'm just not smart enough to distill the actual problem from your
post, so (unless someone posts the answer in the mean time :)) please
elaborate: post some code and the error you are getting.

The main part I'm confused about is: what do you want to achieve? Do
you need to do something like PHP where you can say "time of the day is
$somescalar at this moment"? Or do you want the parameter to be
evaluated into one string before it's passed on writeline?

Regards,
Jeroen
 
Well, there is no such thing (AFAIK) as a "variable string"... you can use
format place-holders, however, and append the values to the call? Otherwise,
assuming that your variables are strings, you *have* created a standard
string. Format example:

string s1 = "is", s2 = "format example", s3 = "variables";
int count = 4;
e.g. string.Format("this {0} a {1} using {2} {3}", s1, s2, count, s3);

Note that some methods such as Console.WriteLine also offer this indirectly
without having to go through string.Format explicitely...

Marc
 
OK here is what i'm trying to achieve.

I have a windows form which contains various controls. When save is
pressed I need to copy the contents of the controls into a string and
send this to a file.

At the moment i'm constructing the string to be placed on the clipboard
in the usual way: -

clipboardstring = control.text + " ; " + control2.text + "-" etc...

The thing is the format that i need the string in changes and so I need
to be able to write the format into a text file and read this into my
program at runtime.

so lets say on monday the format might be: -

control.text + "; " + control2.text + "-"

then on tuesday it might be

control2.text + "*" + control2.text

now my one line text file on monday would be:
control.text + "; " + control2.text + "-"

and on tuesday my one line text file would be
control2.text + "*" + control2.text

Then when the program runs i would open the file, read the line in and
set it equal to clipboardstring - then i would like to use
writeline(clipboardstring) to construct my line. The problem is that
this will always be treated as a literal string. But i'm using the
string as a mixture of variables and strings.

I hope it's clearer thanks.
 
3 options:
1: agree (in advance) the potential vaiables (and their sequence), and use
the inbuild string.Format, always passing in the same values on the rhs, but
just changing the format-string
2: agree some defined tokens and use string search functions to identify
tokens and resolve the meaning(e.g. %CONTROL1% maps [via your code] to
control.text)
3: use a pile of reflection (or Controls.Find) to resolve fields (Controls),
etc. Messy.

Personally I'd go the first route unless there was a reason not to.

Marc
 
Thanks marc can you elaborate a bit on option one please

thankyou
 
See my first post, dated (to my view) 08/11/06 11:08; replace s1, s2, etc
with control.Text, control1.Text, etc. The "3 options" post was post number
2 from me.

Marc
 
I have been unable to find your relevent previous post.

I am not quite getting your solotuion to my problem.

Can you or someone else elaborate / and or suggest a different
solution.

Thanks.
 
REPOST:

Well, there is no such thing (AFAIK) as a "variable string"... you can use
format place-holders, however, and append the values to the call? Otherwise,
assuming that your variables are strings, you *have* created a standard
string. Format example:

string s1 = "is", s2 = "format example", s3 = "variables";
int count = 4;
e.g. string.Format("this {0} a {1} using {2} {3}", s1, s2, count, s3);

Note that some methods such as Console.WriteLine also offer this indirectly
without having to go through string.Format explicitely...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top