Click here to Skip to main content
15,898,987 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: code repeat Pin
Keith Barrow18-Jun-13 2:16
professionalKeith Barrow18-Jun-13 2:16 
If the code is repeated in the same controller you should extract out to a single method. From your description it sounds like you have the same block of code in several controllers, in which case you extract out the method into a common base controller and inherit from it. e.g.

C#
public abstract class MyBaseController
{
    protected void CommonCodeMethod()
    { 
    }
}

public class MyController1 : MyBaseController
{
    public void Foo()
    {
         CommonCodeMethod()
    } 
}

public class MyController2 : MyBaseController
{
    public void Foo()
    {
         CommonCodeMethod()
    }
}



You should put the base in it's own file (as your controllers probably are). You might not need the abstract on the class, and you might want to make CommonMethod public or internal.

If the function is used outside of controllers, then the base class isn't suitable: create a separate class that contains the common method, try to give the class used a descriptive name (for example, if the common methods all create ViewModel called FooViewModel, consider FooViewModelCreator as the class name and Create as the methodname). If you have several such methods, try to keep ones that fit naturally together in the same class, so the class is only really responsible for one category of thing and you have [potentially] several classes.

[Edit]
Countered Univotes. Note sure why your question was downvoted, seemed to me to be a sensible newbie question - perhaps the downvoter could elucidate.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”


Sir Thomas More (1478 – 1535)


modified 18-Jun-13 8:44am.

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 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 0:28
Nick_Frenk17-Jun-13 0:28 

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.