|
what i mean is i am using visual c# express and coding the buttons etc ... the buttons and features come from the toolbox in the designer view , then the c# comes in the coding feature ..
|
|
|
|
|
EliottA wrote: and you would get ... as the second hit (the first is your thread)
Yeah, I've noticed that too. There was a time the answer to most questions was in the first hit; Chris must have done something wrong since now the first hit usually returns a question, not an answer.
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!
|
|
|
|
|
It wasn't meant to be kind. It was meant to point out how incompetent you are and that you could very easily have done this for yourself and not bothered us or shown your ignorance.
only two letters away from being an asset
|
|
|
|
|
well if u didn't want bothering , just carry on , i only asked because i didn't know what it was called ... if you don't want to share your knowledge then why the hell are you on a forum ?
|
|
|
|
|
You don't get it, telling you
back up program = copying files
is not sharing knowledge, it's encouraging stupidity.
|
|
|
|
|
obviously back up means copy , but maybe it was advanced , maybe basic , all u had to say is search this or give an example to share what you know .... i am not in the wrong here , as i asked a simple question that anyone with any kindness would think to themselves and say " well maybe i can give him a bit of help and steer him in the right direction" .... i did try and search my term of back up program but failed ...
|
|
|
|
|
Well you managed to make a lot of friends today didn't you
|
|
|
|
|
This is a common Question. I have given the code many times. Go yo Search Messages and Search for post by me and one of them there is a Code for Backup and restore.
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
I have not used foreach before but I know that the foreach method in what I am trying to do will simplify things emensely. I can adapt after I can see a example.
What I want to do is this:
I have an List<string> of 10 objects so lets call List<string> MyList
foreach (int i = <odd number=""> in MyList.count)
{
Messagebox(Mylist[i])
}
So in the above example It would output 1,3,5,7,9. How would I do this?
Another side point if foreach work not like the above example but like how my other theory is All the ODD number increments will contain "%" the percent sign. But the even values will contain only letters a-z and 0-9.
I don't want to have to do it the way I know how which is double incrementing...
For (i = 0; i < MyList.count; i++)
{
do work
i++
}
|
|
|
|
|
Foreach doesn't loop through the possible indices, but the elements itself... In your first example, you'd actually want something like:
foreach (int i in MyList)
Messagebox(i);
Assuming MyList is a list of integers... You're looping through the contents, not the indices.
In your second example, foreach wouldn't really help, as it's designed to operate on one element at a time, with no knowledge of the next one... You want a plain old "for," but the trick is to remember the syntax:
for ([initializer]; [condition]; [step])
The "step" part doesn't necessarily have to be just incrementing... You could do this:
for (int i = 0; i < MyList.Count; i += 2)
...to work with every other element in the list.
|
|
|
|
|
I appreciate everyone's help here.
To get a better Idea of what I am really trying to do is this:
I receive a path that in a string.
The path contains %USERNAME% in it.
So I need to convert the %USERNAME% to the users login name that windows identifies say "mgeorge"
So in the example c:\apple\%username%\MDB
would become: C:\apple\mgeorge\MDB
Which if I was using dos scripts this would easily work.
But I can't find a easy way of doing this conversion. If there is a simpler way please help me understand.
So I decided I could do something like this as my work around.
I would create a list...
private List<string> IO_SystemVariables()<br />
{<br />
List<string> IO_SystemVariables = new List<string>();<br />
IO_SystemVariables.Add("%USERNAME%");<br />
IO_SystemVariables.Add(Environment.UserName.ToString());<br />
IO_SystemVariables.Add("%MACHINENAME%");<br />
IO_SystemVariables.Add(Environment.MachineName.ToString());<br />
return IO_SystemVariables;<br />
}<br />
The loop thru that list to apply the changes to the string
say something like this:
private static string IO_CheckForSystemVariablePath(String MyPath)<br />
{<br />
List<string> io_SystemVariables = IO_SystemVariables();<br />
for (int i = 0; i < io_SystemVariables.Count; i += 2)<br />
{<br />
if (MyPath.IndexOf(io_SystemVariables[i], 0) > 0)<br />
{<br />
return MyPath.Replace(io_SystemVariables[i],io_SystemVariables[i + 1];<br />
}<br />
<br />
}<br />
<br />
}
Well the code should be close enough to what I was trying to do.
Since every the list 0,2,4,etc will be a variable and the 1,2,5,etc will be it's conversion I figured I would experiment with the foreach loop that I never have been able to figure out.
But if there is an even better way of handling this issue than what I have layed out I am all ears.
|
|
|
|
|
|
just as an example to in code...
Logged in Account username variable = mgeorge
string MyPath = "C:\Apple\%username%\Pictures"
string MyNewPath = Environment.ExpandEnvironmentVariables(MyPath);
the MyNewPath would return me the result "C:\Apple\mgeorge\Pictures"
is this correct if the code is exactly stated above?
I will try it.
|
|
|
|
|
Ok let me throw something else into the equasion
how would I check for if a valid variable Exists before passing the variable?
Would I do option A or B? If neither than what?
Option A)
if (Directory.Exists(Environment.ExpandEnvironmentVariables(MyPath))
{}
else
{}
Should I put option A in a try catch phrase?
Option B)
if (MyPath.IndexOf("%",0) > 0)
{
MyNewPath = Directory.Exists(Environment.ExpandEnvironmentVariables(MyPath)
}
|
|
|
|
|
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.
|
|
|
|
|