473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

help please! when a string contains variable references

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?

Nov 8 '06 #1
8 1164
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
CCLeasing wrote:
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?
Nov 8 '06 #2
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
Nov 8 '06 #3
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.

Nov 8 '06 #4
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
Nov 8 '06 #5
Thanks marc can you elaborate a bit on option one please

thankyou

Marc Gravell wrote:
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
Nov 8 '06 #6
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
Nov 8 '06 #7
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.

Nov 8 '06 #8
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
Nov 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the ...
2
by: m3ckon | last post by:
Hi there, had to rush some sql and am now going back to it due to a slow db performance. I have a db for sales leads and have created 3 views based on the data I need to produce. However one...
3
by: Woody Splawn | last post by:
For reasons I have not yet identified I am having problems with the help system in VS.net 2003. It seems as though it was working all right in the past but it's not now. First of all I can't seem...
12
by: Christo | last post by:
borland c++ 5.01 character constant must be one or two characters long get this when compiling my first c++ program can anyone out there help? it is highlighting this line as the problem ...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
17
by: JT | last post by:
Help me the following C++ question: Write a program to help a local bookshop automate its billing system. The program should do the following: (a)Let the user enter the ISBN, the system will...
5
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. Here is a .php script. Right now, the minimum amount of time (i.e. duration) allowed is 1 month. However, I want to be able to reduce the minimum amount of time to...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
1
by: alfie27 | last post by:
Hi, Here is what the final output to the screen should look like. Below this is the code I have so far. Can someone please help me as to what i need to do next?? I'm really clueless right now. ...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.