|
I am sorting an array of object arrays (string,int). It will work fine is the string is only one character long, but if it is two it will error. I'm not sure why or how it errors. I have done some testing, but still don't understand how to fix it. Any help is appreciated. Thanks in advance.
private object[,] SortByFirst (object[,] RevAr) {
object[,] NewAr = new object[3,2];
for (int i = 0; i < (RevAr.Length / 2); ++i) {
int NewArLoc = (RevAr.Length / 2);
--NewArLoc;
string TestStr = RevAr[i,0].ToString();
for (int j = 0; j < (RevAr.Length / 2); ++j) {
if (i != j) {
string tempStr = RevAr[j,0].ToString();
int intTest = string.Compare(TestStr, tempStr, true);
if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "")) {
--NewArLoc;
}
}
}
NewAr[NewArLoc,0] = RevAr[i,0];
NewAr[NewArLoc,1] = RevAr[i,1];
}
//MessageBox.Show (NewAr[0,0].ToString() + NewAr[0,1].ToString() + "\n" + NewAr[1,0].ToString() + NewAr[1,1].ToString() + "\n" + NewAr[2,0].ToString() + NewAr[2,1].ToString());
//MessageBox.Show (NewAr[2,0].ToString());
return NewAr;
}
The inital array could look like
{ {"A",0} {"C",6} {"B",12} }
This array would work, and would be returned like
{ {"A",0} {"B",12} {"C",6} }
ps. I don't know how to post code correctly still, so sorry for the way my post looks.
Tim
|
|
|
|
|
What error message do you get?
If you use framework 2.0 you could use a sorted dictionary instead:
SortedDictionary<string, int> items = new SortedDictionary<string, int>();<br />
items.Add("A", 0);<br />
items.Add("C", 6);<br />
items.Add("B", 12);
Voila. Sorted.
---
b { font-weight: normal; }
|
|
|
|
|
It doesn't really stop the code, it's just that the finished product isn't correct if the string has more than one character. I'm still on framework 1.1.
This code is use to update revision levels. So if the current revision is A, the new one is B. So if the current one is Z, the new one is AA. We show three revision levels on our drawings. Sometimes they are in the correct order, and sometimes they are not, so I'm sorting them first before I update the revisions.
So say I have revisions 'AA', 'Z', 'Y' (in order top to bottom). It will say that the new revision is 'Z' and will order them 'Z', 'Y', 'Z', in this order from top to bottom, when it should be 'AB', 'AA', 'Z'.
This is why I'm thinking something is wrong in the sorting code I posted.
Tim
|
|
|
|
|
If you want to sort them in that way, you have to first compare the length of the strings, then compare the values. If you just compare the values, "AA" comes before "Z".
---
b { font-weight: normal; }
|
|
|
|
|
I thought that is what I was doing here, and I wrong?
int intTest = string.Compare(TestStr, tempStr, true);
if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "")) {
--NewArLoc;
}
I saying that the NewArLoc is equal the to the length of the array minus one. Then if the length of the string I'm working with 'TestStr' is shorter that the one I'm testing it against 'tempStr', or the value is less (value from string.Compare), then minus one. I want the lowest string value to be first in the array.............
I see what you mean now. Let me see what I can come up with. My test line isn't correct. It will test the length, and then if 'TestStr' is longer, then it will test the value of the compare, which it shouldn't. I will have to rewrite that part.
Thanks.
Tim
|
|
|
|
|
Thanks Guffa. Here is how I solved it, incase only looks at this.
int intTest = string.Compare(TestStr, tempStr, true);
if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "" && TestStr.Length == tempStr.Length)) {
--NewArLoc;
}
This is the portion I added to my check.
&& TestStr.Length == tempStr.Length
Tim
|
|
|
|
|
You can make it a bit more efficient by only doing the compare when needed, and check the length of tempStr instead of comparing it to an empty string:
if (TestStr.Length < tempStr.Length || (TestStr.Length == tempStr.Length && tempStr.Length > 0 && string.Compare(TestStr, tempStr, true) < 0)) {<br />
NewArLoc--;<br />
}
As the condition is evaluated using shortcut, the compare is only done if the lengths are equal and tempStr is not empty.
---
b { font-weight: normal; }
|
|
|
|
|
True. Thanks of the tips Guffa. I'm just starting, so help/tips are always appreciated.
Tim
|
|
|
|
|
How would one implement the "save value, and restore on leaving scope" mechanism in C#
i.e. the one you do in C++ as:
class IntSaver
{
public:
CIntSaver(int & i) : m_orgValue(i), m_ptr(&i) {}
~CIntSaver() { *m_ptr(m_orgValue) {}
private:
int m_orgValue;
int * m_ptr;
}
void Foo()
{
CIntSaver sve_(Repunkulator.CurrentPosition);
Repunkulator.CurrentPosition = rand();
}
IDisposable and a using (x) { ... } statement?
If so, is the "messing around with the finalizer" and Dispose(bool) still the way to go?
-- modified at 13:02 Tuesday 3rd October, 2006
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify! || Fold With Us! || sighist
|
|
|
|
|
You can't use a finalizer that way in .NET, as the finalizer isn't called when the object goes out of scope, but rather when the object is being taken care of by the garbage collector.
IDisposable and using would work. Then you would restore the value in the Dispose method.
The finalizer is usually used as a backup for the disposal, so that the object will be cleaned up when it's collected in case the programmer failed to call Dispose. In this particular case you would probably not want to do anything in the finalizer. If the programmer fails to use the class correctly, it would probably cause more confusion if you change the value back at a later, more or less randomly chosen, time.
You would implement the class for a reference type instead of an int. If you stored a pointer to an int, you would have to make sure that the int stayed pinned in memory while the class exists. For a local variable in a method that would not be a problem as it resides on the stack, but for a member of a class it would, as the garbage collector may move objects around at any time. If you use a reference to an object instead of a pointer that is not a problem, as the reference would be updated if the object moves.
---
b { font-weight: normal; }
|
|
|
|
|
I have a program that need to call a function in another application. How can I do it?
Thanks,
Leo
|
|
|
|
|
In what language is the other program writen? Does it exposes any public methods trough a DLL?
--------------------------------------------------------
My portfolio & development blog
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
The answer depends on the other application. Is it a .NET application? Is it a native application? Does it use COM?
|
|
|
|
|
I have been searching around for about 2 hours and decided to resort to posting a question. Sorry because I know this question has to of been answered before.
Ok....so my question is how do i manipulate a number so that it is a multiple of 10....
such as .... 57 = 60
The number 57 would go to the number 60.
To my knowledge I would use the Math.Something, but I searched through the C# variables and did not find anything that would work.
Anyhelp would be greatly appreciated.
|
|
|
|
|
Well, there's Math.Round, but you may have to do a little work to get what you want:
double multiple = 10;
double value = 57;
double result = Math.Round(value / multiple) * multiple;
Console.WriteLine(result);
|
|
|
|
|
Ok, thank you for your response.
I knew there had to be a way to use Math.Round and get the answer, but i wasn't clever enough to think of your equation. Thanks for the response maybe now I'll be able to finish my homework.
|
|
|
|
|
Well....I would suppose you do something like this
int number = 57;
int remainder= number%10;
if(remainder< 5)
{
number += remainder;
}
else
{
number -= remainder;
}
That will probably give you the best results...and if you're doing this for homework, that's probably the way they wanted you to do it in the first place
-- modified at 15:17 Tuesday 3rd October, 2006
|
|
|
|
|
This is my take, it's easier than the other ones:
int bla = 57;
int rem = (bla % 10);
bla += (rem == 0) ? 0 : 10 - rem;
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I have created a windows service that looks for the existence of a running process and if the process does not exist it will start it. So far this works great, however I have a scenario where sometimes this process will be running multiple times for different customers. When it is kicked off for a customer, their ID is passed as an argument when the process starts up. Is there any way to see what arguments are being used in a running process? I have looked at using remoting, but it seems like overkill to get the info that I need. I also looked at setting the MainWindowTitle so I can look at that, but that doesn't seem to work either. In a nutshell, I want to say is process A running, Yes then for who?
Any help would be appreciated.
Thanks
Sean
|
|
|
|
|
Couldn't you just use a Mutex. Have the process create a Mutex with a name that includes its customer Id. If the expected mutex exists, the customers process is running.
This may be simpler than running through all the processes and has the added benefit of preventing duplicate executions of an individual customers application.
|
|
|
|
|
The mutex looks like it will work great. Thanks
Sean
|
|
|
|
|
UTRocketFan wrote: In a nutshell, I want to say is process A running, Yes then for who?
Maybe you mean Process.Username property? It's available only in .Net 2.0 though.
Regards
|
|
|
|
|
The project I'm currently working on high level has 3 primary projects:
<solution>
|-> Definitions [dll]
|-> ClientConsole [exe]
|-> ServerConsole [exe]
Definitions project has no references. ClientConsole is an exe that will be distributed on clients and references "Definitions" project.
ServerConsole is an exe that will be distributed on server(s). But the requirements allow, server to present itself as a client and thus be an acting "client".
My question:
How do I add "ClientConsole" as a reference to the "ServerConsole" project as the server needs to be able to instantiate the client related objects (UI, other classes). The "Add Reference" fails since "ClientConsole" is of type exe.
I can NOT make "ClientConsole" a class-library and add a new additional EXE project just for launching the ClientConsole.
I have tried all of the following:
1. Keep ClientConsole an exe, and as a "Post build event" on "ClientConsole" project to copy and rename itself as a DLL to the references folder of "ServerConsole" project. This does not work because ...
a) Visual Studio locks the DLLs and thus the new DLL cannot be copied to the destiation folder
b) Even if VS does not lock the file, the intellisense will not reflect the changes in ClientConsole when editing code for ServerConsole when "ClientConsole" is updated
2. Change ClientConsole to a DLL project, and add it as a project reference to ServerConsole. I'll have solved my issue with intellisense and VS picking up the correct DLLs. And as an icing on the cake, set the "post build event" on "ServerConsole" project to rename the "ClientConsole.dll" file to "ClientConsole.exe". This is an ideal solution for me, but this also bomb because ...
a) when the client is compiled as a DLL, the entry poing info "Main()" is not defined so renaming the DLL to an EXE throws error that it's an invalid Win32 app.
I'm sure I'm not the first one to encounter this error. What do you suggest?
Thanks,
- Malhar
|
|
|
|
|
malharone wrote: I can NOT make "ClientConsole" a class-library and add a new additional EXE project just for launching the ClientConsole.
Why not?
malharone wrote: I'm sure I'm not the first one to encounter this error. What do you suggest?
State the obvious and suggest you move the relevant parts to a new Class Library and have ClientConsole become a bootstrap to the controls and forms in the new class library.
|
|
|
|
|
Colin Angus Mackay wrote: Why not?
Unfortunately, where I work, it is the "Business" that makes tbe technical decision. They want to have a simplified client-side deployment process. One additional DLL and the hell breaks loose!
They've dictated that clients should have "Definitions.dll" and the "ClientConsole.exe" only. Rest of the 3rd party dependent references are already installed, so the installer only copies 2 files on the client machine.
Colin Angus Mackay wrote: State the obvious
I was hoping that from the post build event of "ServerConsole", somehow I could recompile the ClientConsole as a WinExe application.
|
|
|
|
|