|
Have you considered using Array.Reverse method?
string original = "original";
char[] reverseString = original.ToCharArray();
Array.Reverse(reverseString);
original = string.Empty;
for (int i = 0; i < reverseString.Length; i++)
original += reverseString[i];
I will use Google before asking dumb questions
|
|
|
|
|
summing up to new string(Array.Reverse(original.ToCharArray())
or even better a sequence of original.ToCharArray() , Array.Reverse() and new string(char[])
-- modified at 15:58 Thursday 22nd November, 2007
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
No, you can't do like that. The Array.Reverse method doesn't return the reversed array, it reverses the array in place.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
You're right of course. So I'd better turn it into a small method...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Of course, as a quick and dirty hack you could always use a recursive method. I'm not saying you should, but you could. I've seen this done in so many homework assignments and coursework samples.
|
|
|
|
|
The recursive method is pretty neat, actually. Who can resist a one-liner?
Not very efficient, though.
public string Reverse(string value) { return value.Length > 1 ? Reverse(value.Substring(1)) + value.Substring(0, 1) : value; }
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
There you go then. The last step is to turn this into a delegate and you're laughing.
|
|
|
|
|
Well, delegates can be used in many ways...
public string Reverse(string value) {
string result = string.Empty;
new List<char>(value.ToCharArray()).ForEach(delegate(char c) { result = c.ToString() + result; });
return result;
}
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
i have a static custom class that keeps track of app settings by reading a db, i periodically retrieve a new list, and want to compare that to the static class (both are the same type) how would i go about doing that, would CompareTo() work for this?
|
|
|
|
|
Have you considered creating a custom Equals or CompareTo method?
If not you should have. The Equals method from object class compares instances not the data within the objects.
I will use Google before asking dumb questions
|
|
|
|
|
hi.. regarding to this link in your website.. http://www.codeproject.com/csharp/charting.asp
i downloaded the source file.. and when i do it in my laptop.. the textbox with value come out... but when i click on the graph button... the page just disappear.. waht is wrong here?
|
|
|
|
|
I think it would be a better idea to ask the author of the article, don't you think?
I will use Google before asking dumb questions
|
|
|
|
|
Did you remember to fill your laptop screen with enough ink?
"On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't.
"I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it."
-Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.
|
|
|
|
|
hi.. the author is Dave Brighton... but i cannot find his related email in his profile.. how do i connect to him?
what is the "ink" mean?thanks
|
|
|
|
|
I want to design a dll, which could be used by few applications, but this dll may not be run (or not be fully functioned) at same time. the requirement is:
1. the first application can create an instance of the dll with fully function.
2. while first application is runing, start second application, it may create an instance, but some of the function is disabled, or it may not create an instance of the dll. until the application 1 closed (or release the control), then the second application can use the fully function of the dll.
Can anybody tell me how I can design this kind of the dll project?
It seems that static variable in the class could not be used for this purpose. static variable is only valid with in an application, but could not be share within few applications. Is it true?
Thank you in advance.
lk
|
|
|
|
|
Sounds suspiciously like you need a singleton, but then again its not. Why on earth would you want something like this?
you can have something similar to the following:
<br />
private static int _NumberOfInstances = 0;<br />
<br />
Private static void InstanceCheck()<br />
{<br />
if(_NumberOfInstances > 1)<br />
throw new TooManyInstancesException();<br />
else<br />
_NumberOfInstances++;<br />
<br />
}<br />
<br />
public void Function1()<br />
{<br />
try<br />
{<br />
InstanceCheck()<br />
...<br />
...<br />
} <br />
catch (TooManyInstancesException tmiex)<br />
{<br />
throw tmiex;<br />
}<br />
}<br />
Each of your methods would be checking the number of instances that are already available. You can then limit the activities based on that.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
"There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib
"Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
|
|
|
|
|
hi
you could use a mutex[^] to check if the library is already in use or not.
greets
m@u
|
|
|
|
|
How can he use a mutex if static variables are not being persisted into different instances of the DLL?
Jeff
|
|
|
|
|
they don't have to.
if you start an application that creates a mutext with a specific name, and then start a second application that tries to create a mutex with the same name, the second application will fail.
in the example of the link i posted you should see how to do it.
|
|
|
|
|
Thank you for your reply, m@u.
If an application window was closed, but the process is still running due to the application software probelm(I encountered such application programs), and I start the same application again, or I start another application using a same library, will the mutex make the application fail?
I still do not know how to use mutex. I shall study how to use it.
lk
|
|
|
|
|
That's good to know! Thanks,
Jeff
|
|
|
|
|
This sounds like it will be significantly more tricky than you would like. The biggest problem is if an application using the DLL crashes, or if that application is killed by the OS. Then it will not be signaled that the resource has been freed, and then NOBODY could use the extended functionality of the DLL. The only way I see this working is if you send a process handle every time you instantiate the dll. Then, when an instance of that dll is requested, you check to see if any of the processes you THINK are using the DLL are still running, and, if you find some that are not, then you would remove those from the list. This can still fail if it happens that one of your processes died, and some new process started that was assigned the same process handle, so you may also want to check the process name, class, etc.
In addition to all the work mentioned above, since the static variables are apparently not being persisted, you would have to either store all that information in the system registry or on the file system elsewhere, and use some other method of thread synchronization (because there would be no common code-based objects, you could not simply lock a static variable like usual).
If you don't care about the exception conditions, then just increment and decrement a counter in the system registry every time you run the constructor and destructor for your dll class, respectively (again, synchronization is required).
I can't personally think of a better (simpler) way to do this, but if you come up with something, let us all know.
Jeff
|
|
|
|
|
Thank you for your reply.
I had thought about using windows regitery or file system elsewhere, but I have to give it up, because NOBODY can use the library if something happen and the library is not released, such as suddenly power off, or the application is killed by OS.
So I am seeking a way to have a running record at runtime.
lk
|
|
|
|
|
Actually, what you want to do can be done quite easily if you know the trick. As you have found out by now, you can't actually have a singleton DLL because it runs in the process space of the calling application and is isolated from other instances of itself. So, to achieve this you would host this DLL in a remotable object which uses the WellKnown type of Singleton.
This[^] article should help you.
|
|
|
|
|
Thank you, pete. I will study and try it.
lk
|
|
|
|