|
N a v a n e e t h wrote: class Vehicle
Why isn't the class abstract?
Here the "Car" instance is down casted into "Vehicle" instance. Is this performance costly ?
Doesn't cost a thing. The down cast is completely safe, so there is no code generated to handle it.
Or an approach with interfaces like the below one is better ?
You only have a use for the interface if you have a need to make a completely different implementation of the vehicle members for any class. If all vehicles will inherit from the Vehicle class anyway, the interface is superflous.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: Why isn't the class abstract?
Yes, I forgot that.
Guffa wrote: You only have a use for the interface if you have a need to make a completely different implementation of the vehicle members for any class.
This is clear Guffa. Thanks
|
|
|
|
|
The compiler knows at compile time that Car is derived from Vehicle, and the runtime allows a reference to a derived class to be stored in a reference to its base class. Compiling with optimizations on and debugging off (a release build) gives the following code:
.method public hidebysig static class Vehicle CreateVehicle() cil managed
{
.maxstack 8
L_0000: newobj instance void Car::.ctor()
L_0005: ret
} Note no cast instructions at all. All it's done is create a new Car and return it.
Going the other way:
public static Car ConvertToCar( Vehicle v )
{
return (Car) v;
}
.method public hidebysig static class Car ConvertToCar(class Vehicle v) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: castclass Car
L_0006: ret
} This time the code can't know at compile time that the parameter v passed into ConvertToCar is actually a Car object, not a Bike or anything else deriving from Vehicle , so it generates a castclass instruction. This instruction checks that the actual type is either a Car or something deriving from Car , if so it modifies the reference as necessary (actually nothing needs to be done, the vtable pointer is always fine for base to derived conversions). If not it throws an InvalidCastException .
If you wanted to get a null reference instead you should use the as instruction - this is better if you're not certain that it's the right class. Adding two more methods:
public static Car TryConvertToCar( Vehicle v )
{
return v as Car;
}
public static bool IsCar( Vehicle v )
{
return v is Car;
}
.method public hidebysig static class Car TryConvertToCar(class Vehicle v) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: isinst Car
L_0006: ret
}
.method public hidebysig static bool IsCar(class Vehicle v) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: isinst Car
L_0006: ldnull
L_0007: cgt.un
L_0009: ret
} Note that using 'is' gives the effective equivalent code to:
public static bool IsCar( Vehicle v )
{
Car c = v as Car;
return c != null;
} Therefore I prefer using 'as' in all cases, because in almost all circumstances I want to manipulate the object as a Car rather than just find out if it is.
As a design question, I'd use a type field rather than inheritance unless I had a reason to use the polymorphism available through inheritance - one or more virtual functions with different behaviour in different classes in the hierarchy. It's simpler to understand and generally produces less indirection and less metadata.
(Code compiled by dropping into a .cs file and running csc /t:library /o+ /debug- vehicles.cs , and decompiled into IL with Reflector[^].)
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
That's great. There is a 5 for you . Thanks
|
|
|
|
|
Hi, I wounder if there is any way to have a cell in a DataGridView to show both a checkbox and text. Or is it possible to have a following text after the checkbox with the DataGridViewCheckBoxCell as with an ordinary win form checkbox?
|
|
|
|
|
Hi akkram
i think u can't directlly.
but you can create custom datagrid view from the base gridw view.
if you want to get sample source code from the Apress.com
thanks
|
|
|
|
|
Hi,
Ive written a small app which runs in the system tray and runs calls from a web service on a timer much like a windows service. I'm using C# Express so i had to write this as a windows application.
Everything is working great.
Only problem is that users are not allowed to exit this application.
Only way to kill the app is by killing the process in task manager.
Anyway if the user tries to shutdown his PC it just doesn't shutdown until the process of the app has been killed.
Anyone have a reason as why this is happening?
Thanks
|
|
|
|
|
what method you are calling to close your application?
try
Application.Exit()
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y<p?jxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Thanks for your response.
The thing is im not calling anything to close the app as the app needs to run all the time unless the pc shuts down.
So basically once the user shuts down the pc my app needs to somehow know that the pc is shutting down so it can exit itself.
But another question is why doesn't windows kill the process because its trying to shutdown?
Thanks
|
|
|
|
|
|
Thanks alot this looks exactly what im looking for.
Will put it to the test
|
|
|
|
|
Hallo...
Are there any possibilities to use PropertyGrid only as "View" ?
That means, the propertyGrid cannot be edited.
Thanks...
|
|
|
|
|
How about setting the Enabled property to false?
He who makes a beast out of himself gets rid of the pain of being a man
|
|
|
|
|
If I set enabled=false, I cannot expand the tree anymore.
|
|
|
|
|
After a bit of Googling, this article seems to mention something that sounds like what you want.
Here.[^]
Scroll down to "Using Attributes in PropertyGrid". I have never really used a PropertyGrid before so can't really help you much more than that.
He who makes a beast out of himself gets rid of the pain of being a man
|
|
|
|
|
You can mark properties as readonly using the ReadOnlyAttribute.
|
|
|
|
|
Hello,
if a commercial product is an option for you, only Smart PropertyGrid will give you the flexibility of choosing a disabled mode for the PropertyGrid. By descending order of "disabled" importance:
1. It can be completely disabled (nothing is actionable, not even the scrollbar).
2. It can be completely disabled (nothing is actionable except the +/- signs and it is scrollable).
3. It can be disabled with the properties still selectable and scrollable.
4. It can minimally be disabled with the property values selectable so that you can copy their value.
The PropertyGrid doesn't handle a direct way to disable everything. You would have to set a hardcoded Readonly attribute on your properties or to publish dynamically a Readonly attribute for your properties with a TypeConverter or Type Descriptor... not very easy for a simple requirement...
Best regards,
Nicolas Cadilhac @ VisualHint
Smart PropertyGrid.Net
Microsoft PropertyGrid Resource List
Free PropertyGrid for MFC
Smart FieldPackEditor.Net / DateTimePicker
|
|
|
|
|
hello,
I am having a listView control with two columns. I want to insert some data first in column 0 and then in coulmn 1. The each column data is not related to other column(means not a subitem of any item).
How can i fill all related data to column 0 in first loop and then in Column 1 of a ListView control?
Also, I want to provide only column separator line in ListView, How to do it?
Thanks.
|
|
|
|
|
You can refer to columns of a listView just like an array:
myListView.Columns[0].Text = "This is the first Column";
Would set the column's header to the above text.
For further info about ListView's other properties, see this article.[^]
He who makes a beast out of himself gets rid of the pain of being a man
|
|
|
|
|
Hello Friends,
I want to save text in a image format it may be JPG or BMP
I have three labels at form now what i want that whatever written of these three labels will save in a single image.
like
''''''''''''''''''''''''''''''''''
Hello-------Label1
Dikshant----Label2
Hi----------Label3
''''''''''''''''''''''''''''''''''
These three labels are in this form so please tell me how can i save these three labels data in a single image...
|
|
|
|
|
Use the DrawToBitmap method of the form to draw the form on a Bitmap object. You can then use the Save method of the Bitmap object to save it in the file format you like.
If you want less than the entire form, put the controls in a Panel , and use the DrawToBitmap method of the Panel .
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hello sir,
Thanks for Answering my Question....
As i am new in C#.NET can you please send me a sample code so i can easily understood..
Thanks in Advance
|
|
|
|
|
The code is basically:
using (Bitmap b = new Bitmap(form1.Width, form1.Height)) {
DrawToBitmap(b, new Rectangle(0, 0, form1.Width, form1.Height));
b.Save(fileName, ImageFormat.Png);
}
You can check the parameters to use in the documentation:
MSDN Library[^]
Tip: search for drawtobitmap method or bitmap class.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Create an instance of the bitmap class, and draw on it the text and then use Bitmap.Save
|
|
|
|
|
I have an application which use update application block 2.0 to update. Everything works fine on the staging server. But when i move my update package on to the production server. I got error : The requested URL does not exist on the server. when applying an update.
Below is the detail i cut from the error log:
Summary for :
======================================
--> MachineName: WAXIANGZ
--> TimeStamp: 2008-2-22 7:36:11
--> FullName: Microsoft.Practices.EnterpriseLibrary.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
--> AppDomainName: EMSWindowsClient.exe
--> WindowsIdentity: ASIAPACIFIC\waxiangz
Exception Information Details:
======================================
Exception Type: Microsoft.ApplicationBlocks.Updater.Downloaders.BitsDownloadErrorException
Code: -2145844844
Context: 5
ContextDescription: The error occurred while the remote file was being processed.
Message: The requested URL does not exist on the server.
Protocol: http
LocalFileName: C:\Program Files\HPInvent\EMS\UAB\downloader\{215E1AD7-9ABA-432f-A952-24BABA556850}\5e4b3b14-30a5-47a5-bdcc-7a0fc173c3ac\updaterconfiguration.config1
RemoteFileName: http://g2w0293.austin.hp.com/EMSUpdateTest/config/updaterconfiguration.config1
TargetSite: Void LogAndThrowException(System.String, System.Exception)
HelpLink: NULL
Source: Microsoft.ApplicationBlocks.Updater
StackTrace Information Details:
======================================
at Microsoft.ApplicationBlocks.Updater.Logger.LogAndThrowException(String message, Exception ex)
at Microsoft.ApplicationBlocks.Updater.Logger.LogAndThrowException(Exception ex)
at Microsoft.ApplicationBlocks.Updater.Downloaders.BitsDownloader.OnJobError(UpdaterTask task, IBackgroundCopyJob pJob, IBackgroundCopyError pError, Exception ex)
at Microsoft.ApplicationBlocks.Updater.Downloaders.BitsDownloader.WaitForDownload(UpdaterTask task, IBackgroundCopyJob backgroundCopyJob, TimeSpan maxWaitTime)
at Microsoft.ApplicationBlocks.Updater.Downloaders.BitsDownloader.Download(UpdaterTask task, TimeSpan maxWaitTime)
Any one have ideas???
|
|
|
|