|
The Interface should only specify those members that all implementations must have.
The classes may have members that are not in the interface.
|
|
|
|
|
HI, thanks for your reply.
Because iam using the same interface for the two classes. In which classone require some methods and classtwo requires some methods from the interface.
Previous the methods are common for both the classes so it is fine.
Now my problem is do i need to have to separate interfaces, one for each class.
Md.Karimulla
|
|
|
|
|
Do you really need your classes to implement an interface? I don't think you do...
If you must have an interface and then implement it, create interfacetwo that inherits interfaceone , and add the additional methods in interfacetwo .
|
|
|
|
|
interface is like "template" for your class, if your classes implement that interface, both of your classes must implement all methods and properties defined in it.
so if your "class one" have some methods other than defined in the interface and these methods only use for "class one" then just add them in "class one" and no need to create them in your interface, vice versa.
But you must be aware for casting that object. i.e:
public inteface MyInterface{
void IntefaceMethod();
}
public class ClassOne : myInterface
{
public void IntefaceMethod(){
}
public void ClassOneMethod(){
}
}
public class ClassTwo : myInterface
{
public void IntefaceMethod(){
}
public void ClassTwoMethod(){
}
}
MyInterface one = new ClassOne();
MyInterface two = new ClassTwo();
one.IntefaceMethod();
two.IntefaceMethod();
((ClassOne)one).ClassOneMethod();
((ClassTwo)two).ClassTwoMethod();
hope it helps
dhaim
ing ngarso sung tulodho, ing madyo mangun karso, tut wuri handayani. "Ki Hajar Dewantoro"
in the front line gave a lead, in the middle line build goodwill, in the behind give power support
|
|
|
|
|
mdkarimulla wrote: Now my problem is do i need to have to separate interfaces, one for each class.
What are you trying to achieve?
|
|
|
|
|
I dont think you should keep a single class specific methods in the interface. If you do so, then whole purpose of creating interface is lost IMHO. In case you really want to have something like this, then you can go for Abstract class but it will then not allow you to inherit any other class. The abstract class might look like this:
abstract class SomeClass {
abstract public void CommonMethod();
public void Class1_Method(){
}
public void Class2_Method(){
}
}
But then also the purpose of having an abstract class goes in vain IMHO.
Loading signature. Please wait...
|
|
|
|
|
mdkarimulla wrote: I need to write the implementation of this methods in classone also like
If you have to implement something only to fulfill syntactical requirements, then you can be absolutely sure that something is wrong! Always and ever, no exception!
In your specific situation, you can do one of two things:
Make a new interface Interface2 that inherits from Interface1 . Then implement Interface1 in ClassOne (in other words: just leave it as it is...). In Interface2 you declare all the additional methods that you need for ClassTwo . Then let ClassTwo implement Interface2 instead of 1 . This solution of the problem uses Inheritance (it's specializing a certain behaviour).
You can achieve the same by using Aggregation (adding up certain groups of behaviour): Let Interface2 declare only the additional methods and let ClassTwo implement both interfaces.
Generally, it is said that Aggregation should be preferred over Inheritance, but that's not a strict rule but merely a general guideline. It depends fully on what sounds more reasonable when you try to describe it in natural language.
Regards
Thomas
www.thomas-weller.de
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. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Without knowing the exact requirements, I'd just add the additional methods into classtwo and leave the interface as is.
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Is it possible for the GC to collect a static variable if it determines that no further references to it exist? For example:
class LockFile {
static FileStream lockFile;
static LockFile ()
{
lockFile = File.Open(".lock", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
}
} If the LockFile class is being loaded somewhere in my code, could the file stream be collected before AppDomainUnload?
|
|
|
|
|
AFAIK, static variables are not garbage collected. It will be cleaned up when the application domain unloads.
|
|
|
|
|
|
If the variable is private, and not referenced in the class (except in the static constructor), would this still be the case?
Thank you.
|
|
|
|
|
I'm not sure. But why on earth would you want to do this?
A private static variable that isn't referenced anywhere. What are you using it for?
To be honest, I wouldn't be at all surprised if the compiler just removes it.
Simon
|
|
|
|
|
Well, I'm loading an unmanaged library and maintaining a SafeHandle over it. The handle must be maintained as long as the application is running, and must be released when the application is unloaded. First I thought about using GC.KeepAlive(libHandle) in some public static methods. But I'm wondering whether I really need to call GC.KeepAlive .
Thanks for your help.
|
|
|
|
|
Ahh, I see what you are trying to do.
My assumption would be that static variables do not get collected. Even if you don't currently have any references too them, because at any point, you could have a reference by using them for something as they are accessible from everywhere.
But, like I said, if private and it's not used anywhere in the class, I wouldn't be all the surprised if the compiler just removed it.
You're probably going to struggle to get an answer to this. It's quite specific. Possibly try asking on the MSDN forum for the CLR, perhaps someone in MS could give you an answer.
Alternatively, I believe the GCHandle class can be used to track object lifetime, so you could test it out.
Good luck.
Simon
|
|
|
|
|
Thanks a lot! I'll try to check with MSDN.
Meanwhile, I made this workaround, tried stopping it from within the debugger (to simulate AppDomainUnload), and it worked OK. I'm not sure how safe it is, but it seems to be working fine.
class MyApp {
private static SafeHandle handle = new MyAppSafeHandle();
public static KeepAlive() { GC.KeepAlive(handle); }
}
class MyLibSafeHandle : SafeHandle {
public MyLibSafeHandle()
: base(IntPtr.Zero, true)
{ MyApp.KeepAlive(); }
protected override ReleaseHandle() {
externRelease(handle);
MyApp.KeepAlive();
}
}
|
|
|
|
|
Hey there!
I'm kinda new to C sharp so if what I'm asking is obvious, then I'm sorry.
I'm trying to use "WebRequest.RequestUri" to get the URI my program is currently crawling, it works fine as i am able to write out the URI to the console, but when i try to save the URI to a variable it goes wrong.
The code:
HttpWebRequest fetchpage = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse fetchinfo = (HttpWebResponse)fetchpage.GetResponse();
//It is here it goes wrong
string URL = fetchinfo.ResponseUri;
The error: "Cannot implicitly convert type 'System.Uri' to 'string'"
How do i get it to save the URL to a variable?
|
|
|
|
|
string URL = fetchinfo.ResponseUri.ToString(); /ravi
|
|
|
|
|
ResponseUri is type of Uri class so you can't assign it to string. If you need string representation of Uri instance either call ToString() on it or use properties provided by Uri class.
|
|
|
|
|
Just use .ToString()
Good Luck
Prateek
|
|
|
|
|
|
i want to copy a file from a location to some specific location in c#?
|
|
|
|
|
Have you tried
System.IO.File.Copy("Source", "Destination"); Docs on MSDN[^]
Simon
|
|
|
|
|
Simon Stevens wrote: System.IO.File.Copy("Source", "Destination");
Hey I tried it but I got FileNotFoundException. Can u plz e-mail me full project at troll@hotmail.com ? It's very urgntz. Thanks
|
|
|
|
|