|
Is it possible to change the DefaultValue attribute for a property for a derived class without having to re-define the whole property?
|
|
|
|
|
It actually depends on how you plan to use it. Normally, yes, you would have to override the property and attribute it with a different value for the DefaultValueAttribute constructor.
If the object is only to be used with Component Model clients (like the PropertyGrid ) you can implement ICustomTypeDescritor and when a PropertyDescriptor is requested for the property you can return a custom implementation that returns a different instance of DefaultValueAttribute from the PropertyDescriptor.Attributes . See the documentation for ICustomTypeDescriptor for more details. You can return the default implementation for most methods you don't need to override using the TypeDescriptor class (corresponding methods to ICustomTypeDescriptor take an additional parameter that basically says not to use any ICustomTypeDescriptor implementation).
Since you care about the DefaultValueAttribute and this is generally used at design-time (though not necessary), you could also implement a custom designer that does essentially what I mentioned above. If this was a Component derivative you would implement a derivative of ComponentDesigner and override PostFilterAttributes .
Generally speaking, though - it's far easier of a change and easier to maintain with less possibility of bugs to just override the property. It could be as easy as the following:
public class MyBase
{
bool _boolProperty = true;
[DefaultValue(true)]
public virtual bool BoolProperty
{
get { return _boolProperty; }
set { _boolProperty = value; }
}
}
public class MyDerivative : MyBase
{
public MyDerivative()
{
BoolProperty = false;
}
[DefaultValue(false)]
public override bool BoolProperty
{
get { return base.BoolProperty; }
set { base.BoolProperty = value; }
}
} See, there's nothing really to implement - just set the member of the base class. Many classes within the .NET Framework base class library do this when necessary. It's just a cleaner design and doesn't depend on callers having to implement or use rather difficult interfaces.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi!
I have a hashtable and add values from time to time like this:
users.Add("Bob", 4);
users.Add("John", 10);
Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes.
So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John.
What is the best way to do this?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Run a clean-up function off a timer that scans the list for items older than 10 minutes, and deletes them. You'll have to keep track of how old an item is, of course. How you do that is really up to you, but I'd make the "value" portion of the table entry actually a class instance that holds your useful data plus a DateTime value.
|
|
|
|
|
It thought about that idea too, but found it a bit unflexible. But when there's no other way that's how I will do it.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I think the System.Web.Caching.Cache class is what are you looking for ...
You can use it in WinForms application as shown below:
<br />
using System.Threading;<br />
using System.Web;<br />
using System.Web.Caching;<br />
<br />
namespace WindowsApplication8<br />
{<br />
public sealed class WinCache<br />
{<br />
private static HttpRuntime _httpRuntime;<br />
<br />
public static Cache Cache<br />
{<br />
get<br />
{<br />
EnsureHttpRuntime();<br />
return HttpRuntime.Cache;<br />
}<br />
}<br />
<br />
private static void EnsureHttpRuntime()<br />
{<br />
if( null == _httpRuntime )<br />
{<br />
try<br />
{<br />
Monitor.Enter( typeof( WinCache ) );<br />
if( null == _httpRuntime )<br />
{<br />
_httpRuntime = new HttpRuntime();<br />
}<br />
}<br />
finally<br />
{<br />
Monitor.Exit( typeof( WinCache ) );<br />
}<br />
}<br />
}<br />
}<br />
}<br />
<br />
Then you can easily insert the values into WinCache in following way:
<br />
WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);<br />
WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);<br />
DevIntelligence.com - My blog for .Net Developers
-- modified at 17:31 Tuesday 24th January, 2006
|
|
|
|
|
tarasn wrote: I think the System.Web.Caching.Cache class is what are you looking for ...
You can use it in WinForms application as shown below:
I'm not sure if you should be applauded for code reuse or censored for abusing existing code into a place where it has no business being. (mainly because I don't know anything about the web classes)
|
|
|
|
|
I don't see any problem with my answer .You can create Console application and check the code that I sent .Instead reinventing the wheel ( and wasting the time ) you can use well implemented class .I highly recommend you read more on MSDN about Cache class - probably it solves your .Net problem too .
DevIntelligence.com - My blog for .Net Developers
|
|
|
|
|
This looks cool, but what's all the web stuff in there? I don't plan on using any http capabilities in my console application (and thus no WinForms).
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
The WinCache class can be used in console application.
DevIntelligence.com - My blog for .Net Developers
|
|
|
|
|
I am writing a interface with Managed DirectX. I decided to use their build in UI, CustomUI. For one of the buttons, I have it clear the screen and then I need it to play a AVI file right in the window. I found some code that uses
the DirectX AudioVideoPlayback. The example it showed me did it in windows.forms. Here is the link to the example. The problem is I am tyring to figure out an easy
way to implement this into my CustomUI. I need to give the video ownership,
but I am not sure what window I use to give ownership. And can I do this without having to create a Windows.Forms? Thank help or input would be great.
Thanks.
|
|
|
|
|
From experience, Direct3D (which im pretty sure CustomUI is built in) does not like to share sys resources.
I have never used the DirectX AV Playback libraries, but I do know there are lots of posts on other forums like this - maybe you should create two seperate windows, or make a custom designed "flat" button in win.forms?
|
|
|
|
|
So it should be possible to create a second window, using the CreateWindow(); method and then use that to host my video playback?
|
|
|
|
|
Possibly yes, you could use the HWND of the second window to exchange commands between them both.
What are you intending to use the GUI for? If its only simple, then you may want to consider using hotkeys.
|
|
|
|
|
The GUI is going to be pretty simple, but I will need to use the mouse. The problem is I don't think I want to create a second window, but figure out how to place the video inside the CustomUI window. I am not sure if I can create a Windows.Form and embedded that into it?
|
|
|
|
|
I created an object class derived from System.Object and an array class derived from ArrayList. I'm trying to serialize (and deserialize) the entire thing. I found a great guide here.
When I serialized in the same situation in VC++, it automatically serialized all of the objects in the array for me. All I did was write in private variables that are in the list itself, and all the objects inside the list got serialized properly too.
Now, in C#, I read the file after serialization, it picks up the private array list data but it doesn't automatically serialize the objects inside of it. Not only do they not show up in the resulting file, but there's a breakpoint on the individual object serialization which never gets reached.
The question is, is the array list not supposed to automatically serialize it's objects inside of it, or am I not doing something to let it automatically serialize its members? I can serialize the members using a foreach but I wanted to know what the word on this was so I don't re-invent the wheel.
|
|
|
|
|
Did you mark the class inherited from ArrayList with the [Serializable] attribute? As far as I know, serialization cannot be inherited, but I am not sure.
--------
"I say no to drugs, but they don't listen."
- Marilyn Manson
|
|
|
|
|
My class that I derived from ArrayList, yeah. I have private variables associated with the list itself that are declared in the lists' class definition like counts, maximums and minimums. Those get serialized and deserialized so I know C# has no problem finding the list.
|
|
|
|
|
Could you post the code of the 2 classes, please?
--------
"I say no to drugs, but they don't listen."
- Marilyn Manson
|
|
|
|
|
You must put XMLArray and XmlArrayItem attributes as shown in code below:
<br />
[XmlArray ("MyItems"), XmlArrayItem("MyItem", typeof(MyItem))]<br />
public ArrayList MyItems = new ArrayList();<br />
The output XML should be in following form
<MyItems> <br />
<MyItem/><br />
<MyItem/><br />
</MyItems><br />
Of course the MyItem class must be serializable too..
DevIntelligence.com - My blog for .Net Developers
|
|
|
|
|
I'm trying to use the binary formatter instead of XML or Soap.
|
|
|
|
|
I have to do a heavy load printing job...
In order to be ok with the clients, they want first to preview the printing, analize it, and then print it.
In my implementation I use a printPreviewDialog. When I show the dialog 1-2 minutes are wasted in order to generate the preview - this is OK so far... But when I press on the print button in the dialog, all the data that was readed previously is re-readed again wasting another 1-2 minutes...
Is there a way to print directly the data generated by the printPreviewDialog?
protected internal static readonly ... and I wish the list could continue ...
|
|
|
|
|
Hello gurus,
I'd like to know how can I retrieve the previous value of the SelectedIndex of the tab control?
I need to know the previous value before it is modified when the SelectedIndexChanged event of the tab is fired...
Can some one show me a code snipet for doing this please?
Best regards.
Fred.
There is no spoon.
|
|
|
|
|
|
Why can you not just store the data (int index) in a custom variable?
|
|
|
|