|
I'm probably blind, but doesn't .NET provide a quicksort or other sorting algorithm? Sure, they're a dime a dozen on the web, but I prefer a solution that's built into the framework before reaching elsewhere.
And yes, I know about Array.Sort and its variations. But I want to sort a field in a DataTable and I need a custom comparator.
[edit]Heh. Found this[^] Linq example (second post)
[/edit]
Marc
modified on Wednesday, October 8, 2008 8:47 AM
|
|
|
|
|
|
|
|
Please describe your problem in detail
Cheers!!
Brij
|
|
|
|
|
Hello,
I'd appreciate some suggestions how to solve the problem exposed below.
I apologize in case that's too easy and I cannot see the solution myself.
Thank you in advance.
<code>
using System;
namespace KeepAliveEventsExample
{
/// <summary>
/// How to garbage-collect an instance which is kept alive only by
/// event handlers without removing the handlers explicitly?
///
/// Client runs a procedure. It holds a reference to an
/// object able to raise an event. Inside the method (Run)
/// an instance is created which can subsrcibe to the event.
/// The event handler keeps the subscriber alive even if the subscriber
/// reference is not reachable by the client.
/// In order to allow the garbage collection an "Unregister()" method removes
/// explicitly the event handler.
/// We wish a solution in order to get to the same result without calling any explicit method
/// like Unregister()
/// </summary>
/// <remarks>
/// - No Dispose Pattern
/// - No new Threads
/// </remarks>
class Program
{
static void Main(string[] args)
{
Client client = new Client();
client.Run();
Console.WriteLine("RUN FINISHED. PRESS A KEY");
Console.ReadKey();
client = null;
Client.FullCollect("LAST COLLECTION");
Console.WriteLine("FINISHED");
Console.ReadKey();
}
}
/// <summary>
/// A client of publisher / subscriber
/// It keeps a reference to the publisher but not to the subscriber
/// </summary>
/// <remarks>
/// While a reference to the publisher is held, no reference
/// to the subscriber is reachable outside the method "Run()"
/// </remarks>
class Client
{
/// <summary>
/// The client holds a reference to the event publisher
/// </summary>
private ObjectWithEvents obj;
public Client()
{
obj = new ObjectWithEvents();
}
public void Run()
{
Observer _subscriber = new Observer(obj);
//we should see _subscriber reacting
obj.DoSomething(5);
obj.DoSomething(10);
//we would like to eliminate this method
_subscriber.Unregister();
//the subscriber is not reacheable [only through the event in obj]
_subscriber = null;
//here we would like _Subscriber NOT TO REACT without having to do anything
obj.DoSomething(15);
//We would like to be able to collect _Subscriber here
FullCollect("COLLECTION 1");
}
~Client()
{
Console.WriteLine("FINALIZING CLIENT");
}
/// <summary>
/// Try to trigger possible collections in all generations.
/// </summary>
/// <param name="message"></param>
public static void FullCollect(string message)
{
for (int j = 0; j <= GC.MaxGeneration; j++)
{
for (int i = 0; i <= GC.MaxGeneration; i++)
{
GC.Collect(i, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
}
}
Console.WriteLine(message);
}
}
/// <summary>
/// Subscribes to the event of ObjectWithEvents
/// </summary>
/// <remarks>
/// Instances of this object might be kept for the whole application lifetime
/// or just used for performing some operations.
///
/// Implementing the Dispose Pattern is not ok because in the real
/// application this would have a deeper meaning for the user
/// </remarks>
class Observer
{
/// <summary>
/// We can hold a reference to the publisher. This won't generate a problem
/// Also shows that, if we want, we have the publishers available
/// </summary>
ObjectWithEvents _publisher;
public Observer(ObjectWithEvents eventPublisher)
{
_publisher = eventPublisher;
//here we register to the event. This instance is going to
//survive garbage collection because the publisher keeps it alive
//through this handler
_publisher.ValueChanged += new EventDefinition(handler);
}
/// <summary>
/// Unregister the event handler in order to allow GC
/// We would like clients not to have to call this method
/// </summary>
public void Unregister()
{
_publisher.ValueChanged -= new EventDefinition(handler);
}
void handler(object sender, int value)
{
Console.WriteLine("VALUE CHANGED TO: {0}", value);
}
~Observer()
{
Console.WriteLine("FINALIZING SUBSCRIBER");
}
}
/// <summary>
/// Event definition
/// </summary>
/// <param name="sender"></param>
/// <param name="value"></param>
delegate void EventDefinition(object sender, int value);
/// <summary>
/// An object able to raise an event
/// </summary>
/// <remarks>
/// Instances are unaware of subscribers
/// </remarks>
class ObjectWithEvents
{
int _value;
public event EventDefinition ValueChanged;
public void DoSomething(int value)
{
_value = value;
//raise the event.
if (ValueChanged != null)
ValueChanged(this, _value);
}
~ObjectWithEvents()
{
Console.WriteLine("FINALIZING PUBLISHER");
}
}
}
</code>
|
|
|
|
|
Either keep the Unregister, or implement IDisposable with an if(registered) Unregister(); pattern.
If you really, really, really want to get rid of this unregister then I suggest you try to knock up a generic event proxy that uses weak references to refer to the observer, and can unregister and drop itself when the weak reference goes dead.
|
|
|
|
|
Hello,
thank you very much for your answer. The solution with a weak reference was already implemented and it is good to know that it was not only the outcome of a "disperate" programmer (...me). The problem is that some subscribers should survive when they are reacheable directly (not only through the event) so the client should provide that kind of information. Again the client has to think about memory management somehow. It would be perfect to be able to find out where the instance (subscriber) is referenced and check if only the events are keeping it alive. In case of other references the proxy could be restored. But I don't know how (and if it is possible). Anyway you gave me the courage to keep "Unregister" or the like...and this is big value.
|
|
|
|
|
Here is an excellent article about this topic published on codeproject recently: Weak Events in C#[^]
|
|
|
|
|
Thank you very much. That's the kind of information I was looking for. Also refering to another answer, now I know how the proxy was meant (and that was NOT the way I implemented killing any possible new idea...)
|
|
|
|
|
You are welcome
|
|
|
|
|
Hello
I have a column in crystal report which can display both float values as well as integer values...
Using formatting i set the decimal notation as 000.000 it is working fine for float.. but for integer values also it displays decimal part (like 5.000) I want it to be displayed like an integer 5
Any options available in reports/??
Thanks in Advance...
Salai
|
|
|
|
|
You set a formula to the number of decimal places in number Custom Style dialog.
In that formula you resolve if the number is integer or float. If it's an integer you return 0 (meaning no decimal places) and if it's a float you return 3 if you want three decimals.
Another option is to create a new formula field and use type conversion functions etc such as CStr. Then place this field into the report.
The need to optimize rises from a bad design
|
|
|
|
|
Mika....
Thanks it worked fine...
Thank you so much
Salai
|
|
|
|
|
You're welcome
The need to optimize rises from a bad design
|
|
|
|
|
Hi all,
I am migrating a C++ application to C#. One of the classes can read/write DBF (dBase) files.
The question is:
Does it worth to migrate this class?
What are all the ready-made .net alternatives?
Do these alternatives have some deployment requirements?
Thanks in advance.
Best regards,
Jaime.
|
|
|
|
|
This might be able to help you: http://www.codeproject.com/KB/bugs/LoadDBF.aspx[^]
Regards,
Thomas Stockwell
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.
Visit my Blog
|
|
|
|
|
Some comment there confirm my concern:
From article: However, try to find one that does not use the MS Jet driver or perhaps the MS FoxPro driver. I tried myself and couldn't find one
So, I think I will use my own library, that doesn't need to load the entire table into a datatable. Thanks for the link.
Best regards,
Jaime.
|
|
|
|
|
Hi...
I have a value in oracle DB like "3.50324616E-38"..
when I read this value from db in Vc.net using odbcDataReader I get an error like
"Value was too large or small for a decimal"..
The error is encountered in thi line
varname = RdrIns->default::get(row+icnt) ;
i tried many methods of Datareader..like .Getint32, GetInt64 GetDecimal etc..
no use
any help.???
Salai
|
|
|
|
|
Did you try a double?
led mike
|
|
|
|
|
Yaa I tried....
tried all the methods supported by the OdbcDataReader...
Salai
|
|
|
|
|
It doesn't work because of exactly what the error message says. The smallest value a Decimal type can have is (+/-)1E-28. You're trying the move the decimal point 10 places more to the left than the Decimal type can handle.
|
|
|
|
|
Thanks Dave...
I understud... Is there any other method to handle this data.. The datatype of that column in Oracle is Binary_FLoat. While oracle can store that data why cant .Net access that data??.. I tried with
Rdr->GetData
Rdr->GetDecimal
Rdr->Getint16,32
Rdr->GetDouble
Rdr->GetFloat
etc....
Nothing works...
Salai
|
|
|
|
|
salaikumar wrote: While oracle can store that data why cant .Net access that data??
Was .NET written by Oracle?? No. There's nothing that says Microsoft has to support every feature of every database or anything else that might come .NETs way.
You have to use the Double data type to store the value. It's range is (copied from MSDN documentation):
-1.79769313486231570E+308 through -4.94065645841246544E-324 † for negative values;
4.94065645841246544E-324 through 1.79769313486231570E+308 † for positive values
|
|
|
|
|
Thanks Dave..
When I tried with double, it returns error for other columns..(Since it is a generic function). Atlast I handled this problem by having a try catch with over flow exception and replaced this value with zero to display in reports since this value is very small.. Thanks for your concern to reply.
Salai
|
|
|
|