|
Kevin Marois wrote: I don't understand why because the Dictionary is defined to accept a
List<Action<object>> . So the 2 lines above should work,
right? What am I doing wrong here?
No. An object is not a T. Use T instead of object and it will work.
|
|
|
|
|
Except that you can't define
private Dictionary<Type, List<Action<object>>> internalList;
with a generic. So what's the right way to do this?
Everything makes sense in someone's mind
|
|
|
|
|
Kevin Marois wrote: Except that you can't define
private Dictionary<Type, List<Action<object>>> internalList;
with a generic. So what's the right way to do this?
Yes, you can:
private Dictionary<Type, List<Action<T>>> _dict = new Dictionary<Type, List<Action<T>>>();
This has to be a member of the T class obviously. Does that dictionary exist somewhere else?
|
|
|
|
|
It's a private field on the class, so you can't define it with T unless the class itself is defined with T. But that's not what I'm looking for here.
Everything makes sense in someone's mind
|
|
|
|
|
It isn't? It sounds like you are trying to stick different types of T into the same dictionary. You can't. That's very wrong. The whole point of generics is that you have a strongly typed class of type T. If you could stick different types of T into the same dictionary, it wouldn't be strongly typed and how would the compiler know which type of T to do stuff on?
I'm thinking you are trying to refactor things in a way that don't make sense if you are trying to do that.
modified 27-Mar-12 12:42pm.
|
|
|
|
|
Just to second Sledgehammer's point, your main problem here is that you are trying to store a whole load of different Action<T> handlers (i.e. handlers for different object types) in the same list. If you think about how you'd try to actually read handlers back out of this list again (in order to fire them, for example) you'll realise that with your current design you can't possibly know which type of handler you have.
If you actually need a central registry of different handler types, I think you need to do something like
class MainStorageClass {
Dictionary<Type, BaseRegistry> registries;
public void Register<T>(Action<T> callback) {
Type messageType = typeof(T);
Registry<T> registry;
if (registries == null)
{
registries = new Dictionary<Type, BaseRegistry>();
}
if (!registries.ContainsKey(messageType))
{
registry = new Registry<T>();
internalList.Add(messageType, registry);
}
else
{
registry = internalList[messageType];
}
registry.Actions.Add(callback);
}
private abstract class BaseRegistry {}
private class Registry<T> : BaseRegistry {
internal List<Action<T>> Actions { get; private set; }
internal Registry() { Actions = new List<Action<T>>(); }
}
}
You could technically do it with Delegate as the base type for Action, but List<Delegate> doesn't superclass List<Action<T>>, so it's less ugly this way, I think. Note that BaseRegistry doesn't actually do anything apart from act as a superclass you can declare the dictionary as using; you could use Dictionary<Type, object> and it would work the same, but I think that is unclear and permits you to make bad mistakes.
And now you can look up like
public List<Action<T>> Find<T>(){
if(registries == null) return null;
BaseRegistry registry;
if(!registries.TryGetValue(typeof(T), out registry)) return null;
return (registry as Registry<T>).Actions;
}
... which I think is quite clean.
Edit: Changed Find slightly, I don't think the original would compile and you need a cast.
modified 27-Mar-12 11:28am.
|
|
|
|
|
So I've got these UpdatePanel(s):
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateButton2" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:Label runat="server" id="DateTimeLabel1" />
<asp:Button runat="server" id="UpdateButton1" onclick="UpdateButton_Click" text="Update" />
<asp:Label runat="server" id="statusSpan"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" id="UpdatePanel1" updatemode="Conditional">
<ContentTemplate>
<asp:Label runat="server" id="DateTimeLabel2" />
<asp:Button runat="server" id="UpdateButton2" onclick="UpdateButton_Click" text="Update" />
<asp:Label runat="server" id="statusSpan2"/>
</ContentTemplate>
</asp:UpdatePanel>
And I've got this code behind:
protected void UpdateButton_Click(object sender, EventArgs e)
{
DateTimeLabel1.Text = DateTime.Now.ToString();
DateTimeLabel2.Text = DateTime.Now.ToString();
statusSpan.Text="FIRED BY (blahblahblahbeeddy blah)";
}
As you can see (the 'blah blah blabbeddy bla' section), I'd like to somehow have the codebehind either catch, be aware of, or report back what Element fired or called the codebehind method. Is this possible?
|
|
|
|
|
cknight725 wrote: object sender
have you looked at the sender at all? apply GetType() or ToString() to it once, to discover what it is; when it is something you can handle, cast ir to its actual type and use it any way you like.
|
|
|
|
|
Thanks!! It was staring me right in the face probably for much longer than I'd care to admit. For completeness, and perhaps to help others out this is my solution:
protected void UpdateButton_Click(object sender, EventArgs e)
{
System.Web.UI.WebControls.DropDownList caller = (System.Web.UI.WebControls.DropDownList)sender;
string senderSrc=caller.ID.ToString();
if (senderSrc=="DropDown1")
{
{
else
{
}
}
|
|
|
|
|
You're welcome.
|
|
|
|
|
I'd like to query a 64bit OS with my 32bit OS and see how the 64bit OS actually stores bits or bytes.
Does the OS stuff 8 bytes into each double long, to a point that if you do not have a multiple of 8 with the number of bytes you need for your string, you have 1 or more empty bytes in the last double long? Are all strings just byte arrays with terminating 0's?
|
|
|
|
|
I'm no expert on this, but...
turbosupramk3 wrote: Are all strings just byte arrays with terminating 0's?
That's likely language/framework dependent. In .net, strings are not NULL-terminated (and may include NULLs).
turbosupramk3 wrote: stuff 8 bytes into each double long
I'm fairly sure that only applies to registers. Or when defining a structure.
Why do you ask? It probably shouldn't matter to everyday programming.
|
|
|
|
|
This isn't every day programming, I'm converting a 64 bit to a 32 bit D word
|
|
|
|
|
None of that made sense. What do you really want?
|
|
|
|
|
|
|
Thanks for helping me through this Eddy, I've got it working now
I can return a hex or decimal value of a Dword
|
|
|
|
|
Cool; hope to see an article on it soon
Bastard Programmer from Hell
|
|
|
|
|
Check the other thread for code (as well as anyone that comes up with this in a search), I don't think I'm qualified for writing an article, but I'd be happy to do so
|
|
|
|
|
turbosupramk3 wrote: Check the other thread for code
Nice, thanks for sharing
turbosupramk3 wrote: I don't think I'm qualified for writing an article, but I'd be happy to do so
Why wouldn't you qualify?
I'd imagine that some people would be very happy if they find an article with the code to download and some explanatory text on the problem being solved. It's quite painless actually, especially with the template[^].
Bastard Programmer from Hell
|
|
|
|
|
I'm self taught and haven't been writing code that long, so I guess that is why I wrote that. I've read those articles before and those people are geniuses!
I'll try and clean up the class and check out the template though, thank you for that link. I'm guessing it will then be peer reviewed and cleaned up if needed.
|
|
|
|
|
turbosupramk3 wrote: I've read those articles before and those people are geniuses!
Some of them are, no doubt. Everything is hard, until it's solved - you didn't get that much responses to your query, did you?
turbosupramk3 wrote: I'll try and clean up the class and check out the template though, thank you for that link. I'm guessing it will then be peer reviewed and cleaned up if needed.
Cool - and yes, and then it'll be out in the public.
Bastard Programmer from Hell
|
|
|
|
|
How do I change the pixel format of any input image to any other pixel format for eg convert to grayscale , true color RGB , paletted(8bpp) , grayscale with alpha , true color with alpha(RGA32).
Any C#.Net solution for the above?
Regards,
Shailesh H
|
|
|
|
|
|
pleasse give me the name of some book for C# beginer
|
|
|
|