|
If you use partial classes instead of inheritance then you have to repeat the same code in many classes. Code you would have ordinarily inherited.
|
|
|
|
|
Maby I'm still unclear in what I'm trying to do or I'm totally missunderstaning you.
Example: I have a product class that i subclsss in BookProdukt and FilmProduct. They both have Title and Genre as attributes but the book has ISBN number and author while the film has IMDB id and director. Both film and book should have a summary attribute that gives the Title and ISBN or IMDB number.
I would make a general product class that implements the title and genre attributes and two inhereting classes that implements the other respective attributes.
Thus:
Product<br />
- Title<br />
- Genre<br />
- Overrideble summary<br />
<br />
BookProduct<br />
- ISBN<br />
- Author<br />
- Overrides summary (returns ISBN + " " + Title)<br />
<br />
FilmProduct<br />
- IMDB<br />
- Director<br />
- Overrides summary (returns IMDB + " " + Title)<br />
As these objects are populated from a database I have a code generator that can create a file with the propertes for these attributes. As the generator cannot determine how the logic for the summary should be put together I need a way to seperate the logic (summary) from the data (Title, ISBN, etc) in case I need to regenerate the classes and don't want to loose the logic I've coded.
This can be done in (at least) two ways:
Alt1:
partial class FilmProduct<br />
- IMDB<br />
- Director<br />
<br />
partial class FilmProduct<br />
- Summary
Alt:
class BaseFilmProduct<br />
- IMDB<br />
- Director<br />
<br />
class FilmProduct : BaseFilmProduct<br />
- Summary
Now to the original question: Is there any performace gain in using either of the two alternatives?
|
|
|
|
|
Hi,
my first reaction would be: partial classes vs inheritance offers same performance;
but try to stay away from virtual methods: using these costs more.
I suggest you create a small test setup (say a short sequence of calls, running
a million times in a for loop) to compare the alternatives you are
considering; if a simple test does not reveal relevant difference, there probably
is nothing in it.
|
|
|
|
|
Oh... I see. I understand what you are saying now.
Go with the partial class idea.
|
|
|
|
|
Partial classes provide no benefit at runtime, they are a compile-time only feature of the language. At compile time, the compiler takes the collection of partial classes for the type (in your example above, this would be the two partial myClass bodies from myClass.cs and myClass.base.cs) and compiles them to a single runtime type.
As Colin pointed out, partial classes were designed to solve a different set of problems than inheritance and shouldn't be used in place of inheritance.
Partial classes are great when you want to isoloate groups of functionality for the same class (like separating all of the event handler logic) or need to provide the ability for multiple people to work in the same type at the same time without stepping on each other's changes.
The partial class can be in any file, but must be named the same as the other partial classes for that type. This prevents the same partial class from being used by other partial classes. This is what Colin meant by "code bloat". If you have multiple classes that need to share functionality, putting it in a partial class will require multiple partial classes with the same code in each, which will create a lot of problems beyond code bloat (think about fixing the same bug in multiple locations).
Inheritance was designed to provide common characteristics and behavior for multiple derived (inherited) types. The benefit here is that these common traits only need to be written and compiled once.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Hallo,
I have a problem
I use Lucene.Net and the method of IndexReader LastModified returns long, which means the number of seconds from Jan. 1, 1970(or another year, I am not so sure). I know there is a function for the conversion, but just do not know which one. Does anyone here have any idea?
Here is the signature of the above-mentioned method:
public static long LastModified(string directory)
Thanks in advance!
Amatisa
|
|
|
|
|
If the return value is a long, then it's probably the date value in ticks. You can convert it to a DateTime object via: DateTime dt = new DateTime(LastModified(directory));
|
|
|
|
|
Thank you very much.
I have found the solution, but a bit different to your suggestions.
I have just tried your suggestion but it didn't work. The problem lies in the returned value LastModified from IndexReader of Lucene.Net, which is the number of millisenconds from 1970.01.01.
|
|
|
|
|
Hi,
In my application I have developed a visual component with a panel and some controls on it(buttons, picture-boxes etc.). I want to make some parts of the components invisible according to some needs.
So I am making those controls' Visible property false. The problem is I am using this component in several forms however when I build the project all controls' visible property becomes true again.
Component does not remember state of the visible property.
How can I solve the problem
thanks..
aysan ethem narman
|
|
|
|
|
Add properties in the component for the visibility of the children you want to be able to hide. For properties to be remembered for a specific instance of a component, they have to be properties of the component, not properties of a child of the component, as the properties of the child is common for all instances of the component.
---
single minded; short sighted; long gone;
|
|
|
|
|
Thank you very much.
It works.
aysan ethem narman
|
|
|
|
|
Hi
I wrote a small app that receives a string array with file names from windows explorer (using 'Send To').
If i send up to 28 file names every thing is ok. If I try to send 29 or more I get this error message:
"Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the item"
And the app closes. It doesn't even enter the app or the try/catch should have caught it.
Any ideas please?
This is the startup class:
static class Program<br />
{<br />
[STAThread]<br />
static void Main(string[] args)<br />
{<br />
try<br />
{<br />
Application.EnableVisualStyles();<br />
Application.SetCompatibleTextRenderingDefault(false);<br />
Application.Run(new frmMain(args));<br />
}<br />
catch (Exception e)<br />
{<br />
MessageBox.Show(e.Message);<br />
}<br />
finally<br />
{<br />
Application.Exit();<br />
}<br />
<br />
}<br />
}
|
|
|
|
|
|
Only yesterday there was an item (I believe regarding "mailto:") with some buffer limited to some 2048 bytes; I guess you are hitting the same limit.
|
|
|
|
|
Hi, I have a DataTable with 4 columns (id, type, length and note) which i bind to a DataGridView (mydataGridView.DataSource = mydataset.Tables[0];). How can I make the "type" column in my datagridview a DataGridViewComboBoxColumn with 3 values (say "white", "black", "gray") representing the same values stored in the DataTable type column?
Any help would really be appreciated.
|
|
|
|
|
Hello!
I have a form with a TreeView, a TextBox and a Button. I select a node from TreeView, after that I want to write some text in textBox or click the Button.In this moment selection from the TreeView is not active any more.How can I make that to stay active, to see what node I have selected?
10x
|
|
|
|
|
Set HideSelection to false.
|
|
|
|
|
Can anyone help me in reading this data.config changing some of its values and wrting it back in the same xml.
Sample data.config is below
<configuration>
<connectionStringCollection>
<XmlSerializerSection type="test values">
<ConnectionStringDataCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Connections>
<connectionString>
<Name>default</Name>
<ConnectionString>Data Source=test123;User Id=aaa;Password=123;</ConnectionString>
</connectionString>
<connectionString>
<Name>default2</Name>
<ConnectionString>Data Source=test123;User Id=bbb;Password=123;</ConnectionString>
</connectionString>
</Connections>
</ConnectionStringDataCollection>
</XmlSerializerSection>
</connectionStringCollection>
</configuration>
Please help me in doing this with C# or VB.net code
Thanks in advance
Umashanker.K
|
|
|
|
|
Load the XML file into an XmlDocument instance, select the nodes you want to change using its SelectNodes or SelectSingleNode methods and finally change the node value as wanted. The documentation of the mentioned class and methods contains many examples, so you should be able to figure out the coding yourself.
"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." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi!
I want to make an image filter in Visual Studio C# .The function of such filter is that redraw the original image only with its dark pixel.
I used following logic but its too slow and not efficient
for (int i = 0; i < 300; i++)
{
for (int j = 0; j < 200; j++)
{
p = new Pen(bitmap.GetPixel(i, j), 1);
for (int r1 = 0; r1 < 100; r1++)
{
for (int r2 =0; r2 < 100; r2++)
{
for (int r3 = 0; r3 < 100; r3++)
{
if (p.Color ! = Color.FromArgb(255, r1, r2, r3))
{
p.Color = Color.Transparent;
}
g.DrawRectangle(p, 1 * i, 1 * j, 1, 1);
}
}
}
My second question is that how we can manipulates the images in visual studio c# .net ,like convert the image in black &white color,grayscale etc etc
Please send the some code or hints+links
|
|
|
|
|
GDI+ is much faster...
Please read "Image Processing Lab in C#"
MSN:shrinerain@hotmail.com
|
|
|
|
|
Hi,
I want to create a start up application that will check the MYSQL SERVICE RUNNING or Not & IF it is running then it will Kill that & Exit. Else it will run an exe setup file.
Any one Please suggest me how can i check WINDOW SERVICE in DotNet.
& How can i kill APACHE by using command line from my programe.
Thanks
connect me @ panda.biswabhusan@gmail.com
|
|
|
|
|
Hi,
take a look on the class ServiceController (include Reference System.ServiceProcess).
Sample:
ServiceController controller = new ServiceController(<servicename>);
controller.Refresh(); // Controller should read state
if (controller.State == ServiceControllerStatus.Running) // Service is running...
...
you will receive an exception if the specified service does not exist.
|
|
|
|
|
|
Dim service() As System.ServiceProcess.ServiceController
Dim i As Integer
service = System.ServiceProcess.ServiceController.GetServices()
For i = 0 To service.Length - 1
If service(i).ServiceName = "wampapache" Then
If service(i).Status = ServiceControllerStatus.Running Then
' Kill the Service
service(i).Stop()
Application.Exit()
Else
Dim startInfo As System.Diagnostics.ProcessStartInfo
Dim pStart As New System.Diagnostics.Process
startInfo = New System.Diagnostics.ProcessStartInfo("E:\Biswabhusan_Collection\COREPRACTICE\TestService\wamp5_1.6.1.exe")
pStart.StartInfo = startInfo
pStart.Start()
pStart.WaitForExit() 'Your code will halt until the exe file has executed.
End If
End If
Next
solveed - Biswabhusan
|
|
|
|