Hi ,
Can some one please share their thoughts on this Abstract class ?
using System.ComponentModel;
public abstract class BaseClass:INotifyPropertyChanged
{
protected BaseClass();
protected void OnPropertyChahnged();
public event PropertyChangedEventHandler PropertyChanged;
}
The above code is not compiling as it is looking for method implementations , as non-abstract methods must have body {}.
When i make protected BaseClass(); as Protected BaseClass(){}; bulid succeeds. similarly with OnPropertyChanged() method.
Please consider that the methods were not declared as abstract. Without declaring the methods as abstract those methods have empty implementation.(This is what i'm looking to achieve).
I am referring to the above code from .Net 2.0 already existing in one of my proj , just wondering if c#2.0 has some flexibility which allows empty method implementations?
Appreciate your time
cheers,
kumar
Loading
VulpesPosted Apr 27, 2011, 3:16 PM
It's not code which will actually compile and run.
Jean PaulPosted Apr 28, 2011, 2:18 AM
I hope Vulpes is right. The code you have got is just a class abstract generated by IDE.
It should not compile.
So I think the OOPs principles are protected still :)
Thanks,
Jean Paul
kumar kPosted Apr 27, 2011, 7:19 PM
I have created a sample dll with abstract class implementing few methods in it. later added it as a reference.
as you said, "A DLL which when referenced will not show implementations but shows only definitions....using f12or gotodefinition."
Thanks,
Kumar
kumar kPosted Apr 27, 2011, 4:08 PM
Thanks for the info.....I will try to find out the source code to see if the abstract class definition which am seeing with f12 or Goto Definition may be diff with implementations(if that is wat you guys were trying to explain me).
I will post back once i find some source code related to it.
cheers
VulpesPosted Apr 27, 2011, 3:45 PM
The point that Jean and I were trying to make is that, if you're using Goto Definition, it shows just that - the definition of the methods, not their implementation.
kumar kPosted Apr 27, 2011, 3:24 PM
Its an Abstract class definition.....not "Class" ...when we try to write an abstract class with empty methods(); without implementations and without abstract or virtual, compiler will raise error.
-kumar
kumar kPosted Apr 27, 2011, 3:09 PM
here is the catch from your points, when i went there to find out the source code using GotoDefinition(u said not to use this method), i found this referred dll info with this method.
looks like it is built on .net 2.0 (Assembly xyz.dll, v2.0.50727)
I have no source code to look at. But still it is an Abstract class though.How could the code work well?(if referring to a dll and writing an abstract class with empty methods has any exceptions in .net 2.0?)
i'm not at all trying to write such a code but trying to understand the existing code piece.
public abstract class BaseClass:INotifyPropertyChanged
{
protected BaseClass();
protected void OnPropertyChahnged();
public event PropertyChangedEventHandler PropertyChanged;
}
Exisitng code has a protected constructor too.....no matter if the constructor exists or not , but here constructor does exists and it has no body, How could that be?
Assembly xyz.dll, v2.0.50727
Been trying to understand this code. If the provided info helps to shed some light about this mechanism, it will be great.
Once again , thanks for the reply.
cheers,
kumar
Jean PaulPosted Apr 27, 2011, 2:26 PM
I am not sure how the code compiled will without the abstract keyword.
Can you please make sure the following from your side?
1) The previous code you obtained is an original source code and not the one generated by opening "Go to Definition" or F12 key press.
2) If you have the right source code - just verify the command line arguments and the c# compiler version.
My suggestion would be stick to the OOPs structures rather than any syntax benefits.
Regards,
Jean Paul
VulpesPosted Apr 27, 2011, 2:23 PM
Is it possible that the abstract class could have originally been an interface?
kumar kPosted Apr 27, 2011, 2:07 PM
Thanks for your reply.
Thing am looking for is really to achieve this
using System.ComponentModel;
public abstract class BaseClass:INotifyPropertyChanged
{
protected BaseClass();
protected void OnPropertyChahnged();
public event PropertyChangedEventHandler PropertyChanged;
}
This was already written and working fine in one of our projects, am trying to figure out the mechanism that was used to let it function properly without compiler issues/errors.
The code never used neither abstract keyword nor virtual . They were just empty methods without even {} i,e., no body.
how can we achieve this ? am trying to understand the existing code(provided above).
This code also works fine without any issues when i write {} empty body like below without even writing abstract and virtual
using System.ComponentModel;
public abstract class BaseClass:INotifyPropertyChanged
{
protected BaseClass(){}
protected void OnPropertyChahnged(){}
public event PropertyChangedEventHandler PropertyChanged;
}
Again Thanks for your time.
cheers,
Kumar
Jean PaulPosted Apr 27, 2011, 1:46 PM
I would like to recollect the following:
An abstract class can contain non-abstract methods
A constructor should not be abstract
A method can be without body by using the abstract keyword
Use virtual keyword for methods with body that could be overridden in descenders.
The solution from my side for your problem is below, and it is compiling fine:
Regards,
Jean Paul
Jean PaulPosted Apr 27, 2011, 1:46 PM
I would like to recollect the following:
An abstract class can contain non-abstract methods
A constructor should not be abstract
A method can be without body by using the abstract keyword
Use virtual keyword for methods with body that could be overridden in descenders.
The solution from my side for your problem is below, and it is compiling fine:
Regards,
Jean Paul