|
JollyMansArt wrote: how would I check for if a valid variable Exists before passing the variable?
If you have to do that, then I suggest enumerating the environment variables, populating a Dictionary with the names and values, and using Contains to test whether or not the name exists.
But...
What are you going to do if it doesn't exist?
What if the variable name is embedded among other things or is recursive or something?
There may be times when you need to do that, and the technique will likely be useful in other situations, but it seems (to me) that what you want in this case is ExpandEnvironmentVariables.
|
|
|
|
|
My suggestion would be to use a Dictionary instead of a list.
Dictionary<string,string> IO_SystemVariables = new Dictionary<string,string>();
IO_SystemVariables.Add("USERNAME", Environment.Username.ToString());
...
return IO_SystemVariables;
And in your check/replace:
foreach (string key in io_SystemVariables.Keys)
{
string replaceText = string.Format("%{0}%", key);
if (MyPath.Contains(replaceText))
MyPath = MyPath.Replace(replaceText, io_SystemVariables[key]);
}
Basically, this creates a lookup, so you can plug in a key and get a value... In this case, plug in "USERNAME" and get the username you entered. You can use foreach to loop through all of the keys you've defined, then replace each of them. I added a little code to add the percent signs in the loop, to remove some redundancy in the entry.
Technically using a List is more efficient in this case, but a Dictionary is more readable and understandable.
|
|
|
|
|
if you need a list of translations, say a1->a2, b1->b2, c1->c2, ...
then instead of adding a1, a2, b1, b2, c1, c2, ... to a simple list
add (a1, a2), (b1, b2), (c1, c2), ... to either a simple list (that would take some class or struct to hold two items), or to a dictionary.
The net result would be you can iterate the entire collection (and use a foreach), you don't have to skip every other entry.
Dictionary<string,string> dict=new Dictionary<string,string>();
dict.Add("%USERNAME%", Environment.UserName);
dict.Add("%MACHINENAME%", Environment.MachineName);
...
foreach(string key in dict.Keys) s=s.Replace(key, dict[key]);
BTW: don't use ToString() on something that already is a string!
PS: letting Windows do the substitutions, as PIEBALD showed you, is the better approach in this case.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Thursday, September 17, 2009 7:37 PM
|
|
|
|
|
JollyMansArt wrote: foreach (int i = in MyList.count)
{
Messagebox(Mylist[i])
}
foreach(object o in MyList)
{
MessageBox(o);
}
That's the whole point of foreach, you don't have to use an index to look anything up.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
RTFM[^]
In the second piece of bad code you present; use i+=2 to increment by two.
|
|
|
|
|
You know, we get our share of morons around here, but this guy seems to me to have asked a reasonable question, albeit one that is indeed well covered in any online example you might find.
PIEBALDconsult wrote: In the second piece of bad code you present; use i+=2 to increment by two.
I believe his point is entirely that if you increment i inside a for loop, the loop also increments it, creating a jump of 2. That evaluation is correct.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Who are you and what have you done with Christian?
|
|
|
|
|
ROTFL - you know, I worry that as the top poster, I may somehow be setting the tone. The tone around here is too negative. I am happy to tell someone they need to buy a book, or that they are being rude, or that they plainly have no idea what they are doing. That people who barely know how to type, take jobs on rentacoder, then ask us to do the work, bugs the hell out of me. But, a beginner is still a beginner, and if they are asking to learn, not because they are being paid to code, then they can be as lost as they like, that's what these forums are for. Sometimes when you're just trying to learn, knowing where to find help is half the problem, and getting a human response can help more than trying to read pages and pages of MSDN.
I do agree that any example of foreach on the web would have made clear where he was going wrong, but so long as he's trying to learn, I'm glad he's here and happy to help. If he'd said 'I need to use foreach to go over these numbers and add them for an accounting package, and it's urgent', THEN I would have been frustrated at his post.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Well said, Christian. My thoughts exactly.
|
|
|
|
|
I have done coding for a long while mostly in VB 6.0.
Not really the .NET level but basic structure I understand. I usually always have a work around of my own that will do the job that I want. However I also know the real power some of these other functions have even if I quite don't know how to use them yet. So from time to time I look for new ways to do my coding. I could read book and sites all day long and for understanding XML to read and write parameters back and forth in my applications I have. Unfortunately there is something I am still not grasping to figuring out how to effectively use xml in my applications. But I usually do research my questions first before posting and even after Posting.
I understood that the foreach loop looped through objects such as if I had a string that contained each everything I am now saying in this post. The foreach would jump to each instance in the string where a word is specified for it to find. I have looked at examples where the fore each is used as List<> Ierritions.
But until I can go threw a working example in something I am trying to do for the first time. I can understand the concepts all day long but it gets me no where. So I ask to learn where my thinking and logic goes wrong.
Some of my code is for personal projects, some is for my company I work with. But as I mentioned earlier. I always have a work around in my code to make it work even if it is the long way. But the only way to get better is to ask and learn from others.
|
|
|
|
|
Yeah, I know, and I agree, but while my better self was cringing at the sight of that attempt at a foreach , the rest of me was typing.
Christian Graus wrote: getting a human response can help more than trying to read pages and pages of MSDN.
And yet, I learn more from books and other documentation than from other beginners. In fact his second example demonstrates the point; many beginners would learn to put i++ in the third part of the for loop and then not progress from there, not be curious about what else could go there*. Such a practitioner has not learned how to use a for loop.
* Perhaps you recall a couple of times recently someone was trying to write a Regular Expression to parse a for loop, but didn't understand the scope of the undertaking.
|
|
|
|
|
I understand I am greatful for any suggestions anyone can provide. Sometimes I feel like a moron for not understanding a few things like this and xml. I am very much greatful for the patients. Above I gave more info on my issue. Is there a better way of handling what I am wanting to do. The increment or 2 works and is fine. That method I had working I was figuring I would try a new approach and try to learn how the foreach works, to improve my coding.
|
|
|
|
|
For (i = 0; i < MyList.count;i++)
{
do work
i++
}
You can remove the i++ from the for statement as above and it only increment once
only two letters away from being an asset
|
|
|
|
|
will some one sugest me good books for XML With Windows C#
or some links releated to this topic
|
|
|
|
|
Google XmlDocument, and perhaps read up on the DOM in general at the W3 pages. Then read up on XPath, so you know how to look for nodes.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
A good book on C# will have information on XML.
Any good book on XML will not focus on any particular implementation language or environment.
|
|
|
|
|
send me sample code or procedure to design in VS2008.
|
|
|
|
|
|
|
very eloquent.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Ramesh Reddy11111 wrote: send me sample code or procedure to design in VS2008.
How do you plan to pay the money?
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Ramesh Reddy11111 wrote: send me sample code or procedure to design in VS2008.
you are rude as well as incompetent. The MessageBox cannot be changed, you need to create a new control ( not that you deserve an answer )
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
i written bellow code under printDocument1_PrintPage event
Font f = new Font("Arial", 14, FontStyle.Regular);
Brush b = Brushes.Red;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
e.Graphics.DrawString(this.dataGridView1.Rows[i].Cells[j].Value+" ", f, b,j * 5,i * 5);
// e.Graphics.DrawString(this.dataGridView1[i, j] + " ", f, b,j*5, i * 5);
}
}
under print button click event code as
printDocument1.DocumentName = "authors";
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
problem is i am getting screen with some red color. i am not getting datagrid rows
hep me
|
|
|
|
|
maybe 5*5 is too small to hold your cell content. Try a width>=50 and a height>=15
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
|