|
For me when choosing between "virtual" or "abstract" depends on the situation that I am faced with, for example.
I recent had to upgrade for our insurance back office the way that premiums were calculated. I created common properties, methods in a base class using abstract to define the class as it was shared resources (properties / methods).
But because each premium calculation needed / processed the information in different ways I chose to use an abstract method to define the Premium method. It wouldn't of been useful to use a virtual as each method was different.
But if 2 of the 3 calculations would of been the same then I would of more than likely of chosen to use virtual methods.
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
thanks, Simon !
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Ahh, the use of virtual or abstract. Okay, here are some thoughts that you might find illuminating in understanding how I tend to think and decompose problems.
If a class is not abstract then you have no choice, you have to make the overridable method virtual. In this case, it means that you have a base class that is useful, in its own right, that supplies a default implementation that you might want to override. Taking a real world example, you have a car that has a number of seats - now, a seat is a useful item in its own right and should be usable as is, but you might decide that you want to provide a specialisation of bucket seats. These are interchangeable with the use of seats, but they may have special methods of fitting. So, in this case, your class structures might look like this:
public class Seat
{
public virtual void FitSeat()
{
}
}
public class BucketSeat : Seat
{
public override void FitSeat()
{
}
} Now, one of the interesting things about overriding methods is the fact that you can call base. and trigger the underlying implementation. This is one of the big problems you face when you first encounter an API - do you call base. and, if so, do you call it at the beginning of your method call or at the end - an incorrect choice here can subtly alter the behaviour or your program and lead to hard to track down bugs. This is why I tend to prefer to use a specialization where I provide an empty On... or Pre.../Post... methods that are called in a predictable fashion. So, if the implementation is empty, this becomes an easy choice - if the class is abstract, make your On/Pre/Post methods abstract - if the class isn't abstract, make them virtual. Here's an example of what I mean:
public class DrawSomething
{
public class Draw()
{
OnDraw();
}
protected virtual void OnDraw()
{
}
} Note that I tend to only use the Pre/Post pattern when there is some setup/teardown options, so I might use this in a persistence module, for instance. This might look something like this:
public abstract class LayoutManager
{
public void Save()
{
PreSave();
PostSave();
}
protected abstract void PreSave();
protected abstract void PostSave();
} I find that this type of pattern removes a lot of the ambiguity that can exist in an API - obviously, there are cases where I'll just make a method virtual/abstract if there is no real implementation in the base class, meaning that calling base. would be irrelevant. I hope this helps.
|
|
|
|
|
Thanks, Pete, I am studying your ideas and code diligently.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
It's difficult, particularly when you start - but I tend towards the "does a default make any sense?" approach.
If the base class can do something sensible and useful then it's a virtual method. If it can't, it's abstract.
So a ToString equivalent would be virtual, but a Save would be abstract.
Does that make sense?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Abstract methods are required; you can choose to do something in them or not. In any event, the class says: I have something to say whether you chose to respond or not. (You're out of milk; like an event?)
Virtual methods are "optional"; although if you do not override them, they "may" exhibit default behavior. The class is saying: I think something needs to be done here; I may do this if you don't override me. (Like phone 911?)
(One can still chose to run the "overridden"; like, I'll help out)
|
|
|
|
|
I have a Combobox with a few items. Now i will start writing in the Combobox and then only the items who fits with my writing should be shown...
for example in my Combobox are These items: "Summer, Sunshine, Winter"
so if i do nothing, all should be shown
if i write an "S" only "summer" and "sunshine" should be shown
if i write an "Su" only "summer" and "sunshine" should be shown
and if i write "Sum" only "Summer" should be shown
can anyone help me please?
|
|
|
|
|
Message Closed
modified 16-Nov-15 6:25am.
|
|
|
|
|
1. He was asking for a combo.
2. He was asking in c#.
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
Long story short, you need to implement this yourself.
For Winforms:
1. you need to keep a list of all items (originally found in the combo)
2. look for the text the user types (listed for combo's TextUpdate event)
3. recreate the combo's items, based on what the user typed; very likely, you will need to also "reopen" the combo (that is, set its DroppedDown to true)
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
Thats my Problem, i can't set the items originally in the Combo, i have to fill the Combo while the code is running.
|
|
|
|
|
1. Note: Pankamauria's second solution might work - I have not used it before though.
2. The combo can be emptied and filled while the code is running - that should not be an issue.
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
You have to try
Autocomplete Combobox[^]
Or You can try
this.comboBox1.AutoCompleteCustomSource.AddRange(new string[] {"summer", "sunshine", "Winter"});
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
Pankaj Maurya
Sr. Software Engineer
Gurgaon, India
|
|
|
|
|
yeah it works
thank you very much!
|
|
|
|
|
your welcome...
Pankaj Maurya
Sr. Software Engineer
Gurgaon, India
|
|
|
|
|
how to call an API in XML in c#
i m working on microsoft visual Studio 2013
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
And that makes absolutely no sense to me at all..
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
Your questions doesn't make any sense at all.
XML is NOT CODE so there is no way to "call an API function" in is.
XML is eXtensible Markup Language used to format data for transmission or temporary storage.
|
|
|
|
|
I've read this question a few times. Each time, I come back to the idea that you are wanting to talk to a web service and what you have at your side is the XML service definition. Is this correct?
|
|
|
|
|
let me tell u in detail
i want to call XML API from OPEN WEATHER website
and then
parse it
and only obtain temperature, humidity and visibilty from there
then all to display in my application
which i have created in C# in MICROSOFT VISUAL STUDIO 2013
it can be displayed in text box , or label etc
and that application
and that i have built is WINDOWS FORM APPLICATION
modified 17-Nov-15 0:13am.
|
|
|
|
|
|
i need to write a program as per the below question
Question : Write a class named Document to represent a document node in a print queue. it should contain a method name that returns the subject of the document and an abstract method called type that returns the document type . from document class derive two concrete classes named word document and pdf document.
just tell me do i need to write the above program with link list ?
how to represent document node ?
here i wrote the code for the above program. just tell me is my code is correct or design as per the above instruction?
code as follows
public class Document
{
public string Subject { get; set; }
public abstract void type();
}
public class WordDocument : Document
{
}
public class PdfDocument : Document
{
}
if i made any mistake then please rectify me. thanks
tbhattacharjee
|
|
|
|
|
Come on now!
Two questions on exactly the same homework?
Give it a try yourself, and see how far you get! You will learn a lot better by trying it than by looking at other code...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
i need help to construct the code for below program.
here is the question
Another class named Print Queue that is implemented as a linked list of Document objects. Print Queue should have a method named Push for adding a document to the end of the list, a method named pop for removing the first document from the list , and a method named DisplayContents for listing all of the documents in the Print Queue, reporting both the Name and Type of each element Print Queue should not use any standard library classes it should be own implementation of linked list.
i coded the documents class this way
public class Document
{
public string Subject { get; set; }
public abstract void type();
}
public class WordDocument : Document
{
}
public class PdfDocument : Document
{
}
if i made any mistake then please rectify me.
but not being able to code for Print Queue class which will use link list.
see this line too -- Print Queue should have a method named Push for adding a document to the end of the list, a method named pop for removing the first document from the list
stack class has Push and Pop method. so how link list can push and pop.......need idea.
help me to construct the code for above scenario.
thanks
tbhattacharjee
|
|
|
|