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

C#

 
AnswerRe: How to access java class files through .net environment? Pin
jschell18-Jun-13 8:37
jschell18-Jun-13 8:37 
AnswerRe: How to access java class files through .net environment? Pin
Pete O'Hanlon18-Jun-13 22:28
mvePete O'Hanlon18-Jun-13 22:28 
Questionkey event Pin
riyaspj18-Jun-13 3:15
riyaspj18-Jun-13 3:15 
AnswerRe: key event Pin
Richard MacCutchan18-Jun-13 4:48
mveRichard MacCutchan18-Jun-13 4:48 
Questioncode repeat Pin
John_Tadros18-Jun-13 0:57
John_Tadros18-Jun-13 0:57 
AnswerRe: code repeat Pin
Abhinav S18-Jun-13 1:03
Abhinav S18-Jun-13 1:03 
GeneralRe: code repeat Pin
John_Tadros18-Jun-13 1:06
John_Tadros18-Jun-13 1:06 
GeneralRe: code repeat PinPopular
Pete O'Hanlon18-Jun-13 2:09
mvePete O'Hanlon18-Jun-13 2:09 
A common way to do this is to create a class library that you use to hold common methods. Suppose, for instance, that you have a piece of code to check whether or not an integer is between two values, and you use this in many projects, you could add the code to a static class like this:
C#
public namespace RangeExtensions
{
  public static class Range
  {
    public static bool IsInRange(this int value, int startRange, int endRange)
    {
      return value >= startRange && value <= endRange;
    }
  }
}
This is known as an extension method, and as long as you add the DLL that contains this code and add the namespace declaration as a using statement to any class that uses it, you can do things like this:
C#
private void GetValueAndValidateIt()
{
  int value = GetValueFromSomewhere();
  if (value.IsInRange(0, 10))
    Console.WriteLine("The value is between 0 and 10");
  else
    Console.WriteLine("Drat it you fool, you just blew up the planet");
}
Now, if you drop the this statement from the Extension method, you would call it like this:
C#
private void GetValueAndValidateIt()
{
  int value = GetValueFromSomewhere();
  if (Range.IsInRange(value, 0, 10))
    Console.WriteLine("The value is between 0 and 10");
  else
    Console.WriteLine("Drat it you fool, you just blew up the planet");
}
So, what's the difference? Well, the this keyword at the start of a method declaration parameter list indicates to the compiler that this is an extension method; that is, a method that allows you to extend the functionality of an existing type.
I was brought up to respect my elders. I don't respect many people nowadays.

CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

AnswerRe: code repeat Pin
Keith Barrow18-Jun-13 2:16
professionalKeith Barrow18-Jun-13 2:16 
GeneralRe: code repeat Pin
Pete O'Hanlon18-Jun-13 5:11
mvePete O'Hanlon18-Jun-13 5:11 
Question"An assembly with the same simple name has already been imported" error message Pin
impeham17-Jun-13 4:42
impeham17-Jun-13 4:42 
AnswerRe: "An assembly with the same simple name has already been imported" error message Pin
jschell17-Jun-13 8:03
jschell17-Jun-13 8:03 
GeneralRe: "An assembly with the same simple name has already been imported" error message Pin
impeham18-Jun-13 3:48
impeham18-Jun-13 3:48 
AnswerRe: "An assembly with the same simple name has already been imported" error message Pin
Eddy Vluggen17-Jun-13 8:27
professionalEddy Vluggen17-Jun-13 8:27 
GeneralRe: "An assembly with the same simple name has already been imported" error message Pin
impeham18-Jun-13 3:51
impeham18-Jun-13 3:51 
GeneralRe: "An assembly with the same simple name has already been imported" error message Pin
Eddy Vluggen18-Jun-13 9:32
professionalEddy Vluggen18-Jun-13 9:32 
AnswerRe: "An assembly with the same simple name has already been imported" error message Pin
jschell18-Jun-13 7:42
jschell18-Jun-13 7:42 
AnswerRe: "An assembly with the same simple name has already been imported" error message Pin
johannesnestler21-Jun-13 0:20
johannesnestler21-Jun-13 0:20 
GeneralRe: "An assembly with the same simple name has already been imported" error message Pin
impeham21-Jun-13 5:00
impeham21-Jun-13 5:00 
Questionhow do i implement barcode in C# Pin
Resma Rahiman17-Jun-13 0:44
Resma Rahiman17-Jun-13 0:44 
AnswerMy Vote of 1 Pin
Keith Barrow17-Jun-13 1:51
professionalKeith Barrow17-Jun-13 1:51 
QuestionError in WPF Pin
Nick_Frenk16-Jun-13 21:54
Nick_Frenk16-Jun-13 21:54 
AnswerRe: Error in WPF Pin
Keith Barrow16-Jun-13 23:02
professionalKeith Barrow16-Jun-13 23:02 
GeneralRe: Error in WPF Pin
Nick_Frenk16-Jun-13 23:56
Nick_Frenk16-Jun-13 23:56 
GeneralRe: Error in WPF Pin
Keith Barrow17-Jun-13 0:17
professionalKeith Barrow17-Jun-13 0:17 

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.