Click here to Skip to main content
15,846,342 members
Home / Discussions / C#
   

C#

 
GeneralRe: Custom Exceptions - Best Practices Pin
BillWoodruff22-Jan-17 16:10
professionalBillWoodruff22-Jan-17 16:10 
QuestionC# classes Pin
Bobbla1311-Jan-17 0:17
Bobbla1311-Jan-17 0:17 
GeneralRe: C# classes Pin
harold aptroot11-Jan-17 0:35
harold aptroot11-Jan-17 0:35 
GeneralRe: C# classes Pin
#realJSOP11-Jan-17 8:47
mve#realJSOP11-Jan-17 8:47 
GeneralRe: C# classes Pin
OriginalGriff11-Jan-17 9:03
mveOriginalGriff11-Jan-17 9:03 
GeneralRe: C# classes Pin
#realJSOP12-Jan-17 3:06
mve#realJSOP12-Jan-17 3:06 
AnswerRe: C# classes Pin
Daniel Pfeffer11-Jan-17 0:36
professionalDaniel Pfeffer11-Jan-17 0:36 
AnswerRe: C# classes Pin
Pete O'Hanlon11-Jan-17 0:43
subeditorPete O'Hanlon11-Jan-17 0:43 
Classes don't always need methods. Suppose that you have a data model that represents a user.
C#
public class Name
{
  public string Name { get; set; }
  public int Department { get; set; }
  public DateTime BirthDate { get; set; }
}
This model is perfectly fine. It's generally considered good practice to separate out the retrieving of the data from the model, so you might have a separate class that is responsible for retrieving the data and populating the name*. That might look something like this:
C#
public class Users
{
  private IEnumerable<User> users;
  public IEnumerable<User> Users { get { return users; } }
  public void LoadUsers()
  {
    users = new List<User>();
    using (DataAccess dal = new DataAccess())
    {
      IDataReader reader = dal.GetValuesFromStoredProcedure("GetUsers");
      while (reader.Read())
      {
        User user = new User();
        dal.Populate(user.Name, "Name");
        dal.Populate(user.Department, "Department");
        dal.Populate(user.BirthDate, "DOB");
        users.Add(user);
      }
    }
  }
}
A little bit of explanation about that last method before you get worried. DataAccess is the name of a class I have that allows me to do a lot of common SQL data operations - I prefer having my own version, rather than relying on things like Entity Framework. The methods you see being called in there are responsible for retrieving values from a stored procedure, then using that to populate a list of users.

*I meant to add this earlier. Even though the data model class doesn't visibly have methods because you have exposed properties here, the internal code that is created from this has methods because a property internally translates to separate get and set methods; so, Name becomes get_Name() and set_Name(string value) internally.
This space for rent


modified 11-Jan-17 6:07am.

AnswerRe: C# classes Pin
Gerry Schmitz11-Jan-17 9:26
mveGerry Schmitz11-Jan-17 9:26 
QuestionHow to dynamically Add and Close tab items in Browser click on add and close buttons Pin
Aniruddha.A10-Jan-17 19:53
Aniruddha.A10-Jan-17 19:53 
AnswerRe: How to dynamically Add and Close tab items in Browser click on add and close buttons Pin
Afzaal Ahmad Zeeshan11-Jan-17 1:03
professionalAfzaal Ahmad Zeeshan11-Jan-17 1:03 
QuestionUse of LevDan.Exif assembly for DNG files?? Pin
Grahame_200510-Jan-17 0:33
Grahame_200510-Jan-17 0:33 
AnswerRe: Use of LevDan.Exif assembly for DNG files?? Pin
Pete O'Hanlon10-Jan-17 3:30
subeditorPete O'Hanlon10-Jan-17 3:30 
GeneralRe: Use of LevDan.Exif assembly for DNG files?? Pin
Grahame_200510-Jan-17 4:43
Grahame_200510-Jan-17 4:43 
GeneralRe: Use of LevDan.Exif assembly for DNG files?? Pin
Pete O'Hanlon10-Jan-17 5:29
subeditorPete O'Hanlon10-Jan-17 5:29 
QuestionProgrammatically Open A Solution In Current IDE Pin
Kevin Marois9-Jan-17 11:31
professionalKevin Marois9-Jan-17 11:31 
AnswerRe: Programmatically Open A Solution In Current IDE Pin
Afzaal Ahmad Zeeshan11-Jan-17 1:09
professionalAfzaal Ahmad Zeeshan11-Jan-17 1:09 
QuestionNothing happens when CheckBox is checked Pin
Pavlex49-Jan-17 9:00
Pavlex49-Jan-17 9:00 
AnswerRe: Nothing happens when CheckBox is checked Pin
NotPolitcallyCorrect9-Jan-17 9:17
NotPolitcallyCorrect9-Jan-17 9:17 
AnswerRe: Nothing happens when CheckBox is checked Pin
Richard MacCutchan9-Jan-17 23:30
mveRichard MacCutchan9-Jan-17 23:30 
QuestionC# Enumerable Repeat Pin
Pavlex48-Jan-17 5:01
Pavlex48-Jan-17 5:01 
AnswerRe: C# Enumerable Repeat Pin
OriginalGriff8-Jan-17 5:49
mveOriginalGriff8-Jan-17 5:49 
AnswerRe: C# Enumerable Repeat Pin
Pete O'Hanlon9-Jan-17 0:01
subeditorPete O'Hanlon9-Jan-17 0:01 
GeneralRe: C# Enumerable Repeat Pin
harold aptroot9-Jan-17 9:20
harold aptroot9-Jan-17 9:20 
QuestionVisual Studio Debugger Questions Pin
zequion7-Jan-17 22:15
professionalzequion7-Jan-17 22:15 

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.