Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to improve Parallel.For loading speed? Pin
smallkubi17-Nov-15 13:43
smallkubi17-Nov-15 13:43 
GeneralRe: How to improve Parallel.For loading speed? Pin
Simon_Whale18-Nov-15 1:26
Simon_Whale18-Nov-15 1:26 
Questionbest explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
BillWoodruff16-Nov-15 3:58
professionalBillWoodruff16-Nov-15 3:58 
AnswerRe: best explanation of creating a C# class as 'virtual, or 'abstract ... for beginners in the language Pin
OriginalGriff16-Nov-15 4:04
mveOriginalGriff16-Nov-15 4:04 
GeneralRe: best explanation of creating a C# class as 'virtual, or 'abstract ... for beginners in the language Pin
BillWoodruff16-Nov-15 4:20
professionalBillWoodruff16-Nov-15 4:20 
AnswerRe: best explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
Simon_Whale16-Nov-15 4:40
Simon_Whale16-Nov-15 4:40 
GeneralRe: best explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
BillWoodruff18-Nov-15 0:33
professionalBillWoodruff18-Nov-15 0:33 
AnswerRe: best explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
Pete O'Hanlon16-Nov-15 5:38
mvePete O'Hanlon16-Nov-15 5:38 
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:
C#
public class Seat
{
  public virtual void FitSeat()
  {
    // Attach using standard fitments.
  }
}

public class BucketSeat : Seat
{
  public override void FitSeat()
  {
    // Attach using the bucket seat fitments.
  }
}
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:
C#
public class DrawSomething
{
  public class Draw()
  {
    // Do some work....
    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:
C#
public abstract class LayoutManager
{

  public void Save()
  {
    PreSave();
    // Do the actual save work
    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.
GeneralRe: best explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
BillWoodruff18-Nov-15 0:34
professionalBillWoodruff18-Nov-15 0:34 
AnswerRe: best explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
OriginalGriff16-Nov-15 5:42
mveOriginalGriff16-Nov-15 5:42 
AnswerRe: best explanation of using C# 'virtual, or 'abstract methods ... for beginners in the language Pin
Gerry Schmitz16-Nov-15 18:28
mveGerry Schmitz16-Nov-15 18:28 
QuestionHow can I fill a Combobox while writing? Pin
Member 1191673515-Nov-15 23:56
Member 1191673515-Nov-15 23:56 
AnswerMessage Closed Pin
16-Nov-15 0:09
professionalPANKAJMAURYA16-Nov-15 0:09 
GeneralRe: How can I fill a Combobox while writing? Pin
John Torjo16-Nov-15 0:18
professionalJohn Torjo16-Nov-15 0:18 
AnswerRe: How can I fill a Combobox while writing? Pin
John Torjo16-Nov-15 0:16
professionalJohn Torjo16-Nov-15 0:16 
GeneralRe: How can I fill a Combobox while writing? Pin
Member 1191673518-Nov-15 1:47
Member 1191673518-Nov-15 1:47 
GeneralRe: How can I fill a Combobox while writing? Pin
John Torjo18-Nov-15 2:10
professionalJohn Torjo18-Nov-15 2:10 
AnswerRe: How can I fill a Combobox while writing? Pin
PANKAJMAURYA16-Nov-15 0:25
professionalPANKAJMAURYA16-Nov-15 0:25 
GeneralRe: How can I fill a Combobox while writing? Pin
Member 1191673518-Nov-15 2:08
Member 1191673518-Nov-15 2:08 
GeneralRe: How can I fill a Combobox while writing? Pin
PANKAJMAURYA18-Nov-15 17:19
professionalPANKAJMAURYA18-Nov-15 17:19 
Questioncall an api in c# Pin
Member 1212120115-Nov-15 21:25
Member 1212120115-Nov-15 21:25 
AnswerRe: call an api in c# Pin
OriginalGriff15-Nov-15 21:44
mveOriginalGriff15-Nov-15 21:44 
AnswerRe: call an api in c# Pin
Afzaal Ahmad Zeeshan16-Nov-15 2:19
professionalAfzaal Ahmad Zeeshan16-Nov-15 2:19 
AnswerRe: call an api in c# Pin
Dave Kreskowiak16-Nov-15 4:06
mveDave Kreskowiak16-Nov-15 4:06 
AnswerRe: call an api in c# Pin
Pete O'Hanlon16-Nov-15 5:51
mvePete O'Hanlon16-Nov-15 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.