|
I remember that in C++ it was no problem to use a pointer to a function to execute a function (or method), but is this doable in C#? What I'm thinking of doing is having an array of methods within a class and using a index to the array to select which function to execute. My next question would be what happens to my array of functions when I inherit from a base class, will I get the base class pointers or the derived class pointers?
Any thoughts?
|
|
|
|
|
Use delegates. They are essentially enhanced wrappers around function pointers.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
Use delegates they are like safe, secure, object oriented functions pointers.
|
|
|
|
|
Thanks to you both for the advice. They even work just fine in a derived class too. So if a base class has a delagate pointing to a method in the base class and you then override the method in a derived class, the delagate from the derived class really does call the derived method. Excellent!
|
|
|
|
|
Not exactly.
Yes, delegates are the correct thing to use here, but they do not magically change to reference derived methods. That is a function of how you define your methods.
Here's a small example demonstrating this fact:
using System;
class Example {
static void Main() {
Base b = new Derived();
b.Test();
}
}
class Base {
protected delegate void D();
protected virtual void One() {Console.WriteLine("Base.One");}
protected void Two() {Console.WriteLine("Base.Two");}
public void Test() {
D[] methods = {new D(One), new D(Two)};
foreach(D method in methods) method();
}
}
class Derived : Base {
protected override void One() {Console.WriteLine("Derived.One");}
protected new void Two() {Console.WriteLine("Derived.Two");}
}
When executed it outputs:
Derived.One
Base.Two
Virtual dispatch is unrelated to delegates.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
So....so long as the base method is virtual and the derived method is set to override the base method everything should be cool right? I think that was what I was doing when I was playing with these and it seemed to work, or at least it behaved the way I wanted it too.
|
|
|
|
|
Yes, that's exactly correct. Delegates dispatch just like calling the method directly would. They won't make a non-virtual method dispatch in a virtual manner, but as long as the underlying method is virtual and the derived methods override it then everything works the way you want.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Does anyone know a good method of getting the size (i.e. kb) of a <c>Bitmap<c> object ?
I have found height and width, but no actual property that represents the size of bitmap on disk or memory.
Thanks in advance.
R.Bischoff
.NET, Kommst du mit?
|
|
|
|
|
|
If it's on disk and you have the filepath you can create a FileInfo object and look at the FileInfo.Length property.
|
|
|
|
|
|
|
Neither will "w x h x bpp" exactly, as it doesn't account for padding or headers. To be fair, the original poster asked about both in memory and on disk, and the FileInfo answer was certainly correct for the on disk case.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Blake Coverett wrote:
as it doesn't account for padding or headers.
What is a couple of bytes between friends?
We are all friends here, arent we?
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
I've noticed that Access has a neat text box embedded in the context menu that you can use to enter a filter for your records. I find this extremely swift and handy (shame they don't do the same for search as well) and I was wondering if there is any relatively easy way to incorporate this type on control in my C# project? I'm pretty sure the regular menu controls won't do it so I'm guess I'd have to write a custom control, but does anybody have a good idea of how much work would be involved so I can decide if it's really worth the effort for my application. I've never tried writting my own controls before!!
Thanks
|
|
|
|
|
The standard menu just does not work with embedded child controls. I tried this myself, and the menu class won't budge to display an embedded control --I guess this could be, at least in Windows XP, because the Windows is Layered and thus possible painted on a memory buffer. It's not even possible to spy on the Window menu because is protected by the system. Just setting up the hook for getting the handle to menu window is a task in itself too.
Most custom menu controls don't have support for this either. And yes, you need to write your own custom control and deal with alll the painting and handling of the keyboard to make the menu behave like a standard menu. If you have the time the it is a good learning excercise.
If you don't you can always check the SharpLibrary popup menu control which does offer that feature.
Regards,
Carlos
http://www.sharplibrary.com
|
|
|
|
|
Thanks for the advice. I guess I'll live without for the time being
|
|
|
|
|
The Fluid UI Toolkit[^] menu/commandbar will have this, but it does not exist yet. We are making progress on Fluid, and the commandbar system is high on our list. So bookmark our site, and check back periodically for downloads.
We will also be posting articles on CodeProject about each of our controls as they are completed.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
I'll keep an eye on it. Thanks.
|
|
|
|
|
|
Hey Nish,
Sure, I love CodeProject. I just did not appreciate the geeks fighting me over my own code.
Regards,
Carlos.
|
|
|
|
|
|
To me, it's just a matter of having an implied license. Basically, you thought the license was one thing, we thought it was another, and it happens we were correct. But the best thing to do is to have the license be stated beforehand, which is what Chris is working on doing for future CP articles/code.
Good luck with your commercial product!
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
I'm trying to access functions of a C# interface from an extened SP written in C++ (Unmanaged) and I'm having problems retrieving the complete string upon reception in the interface.
I am using the MarshalAs attrib but for some reason, I cannot get this to work correctly...
Here is waht I'm doing...
Extended stored proc C++
__int64 nReturnCode;
char Text[20] = ".Net Rules";
pInterface->raw_Command((BSTR)Text, &nReturnCode);
C# interface...
public interface IMyInterface
{
long Command([In, MarshalAs(UnmanagedType.LPWStr)] string strText)
//could be the type that I'm using, but I have tried many and stil cannot get this to work...
};
public clase MyClass : IMyInterface
{
public long Command(string strText)
{
MessageBox.Show(strText);
return 0;
}
};
Messagebox: "????????s"
Please help me out with this one
Cheers
Ribs
Andre Cote
Software Development Manager
Position Technology Inc.
780 Industrial Blvd.
St-Eustache, Quebec, Canada J7R 5V3
Phone: (450) 491-7444
Fax: (450) 491-2509
mailto:acote@postech.ca
http://www.postech.ca
|
|
|
|
|
First, I know nothing about ASP coding or anything like that. What I do know, I need a better way to organize my photos. I have tried many programs, all cumbersome and not friendly to use.
So with that, I am looking at installing this program for my photo album pages.
What I need, is to hire someone to set it all up. I can do 50% up front (upon ref.), another 25% when you show me it works. The last 25% paid when done.
You will need to show/instruct me on how to upload the photos and how the program works.
My site is desertdrifter.com. It is hosted by hostsave.com. If need be, I will change host providers to get this up and running.
Serious inquires only.
I can be reached at jbriggs@desertdrifter.com.
Joel Briggs
www.desertdrifter.com
|
|
|
|