Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is this acceptable practice

Is this acceptable practice

Scheduled Pinned Locked Moved C#
csharpdatabasequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wayne Phipps
    wrote on last edited by
    #1

    There is a method of the DataTable object that I require to access from various methods of my class basically to retrieve the column name as a specific index. I have created a private property in my class to access this method and defined it similar to as follows: private string ColumnName { get {return DataTable.Columns[ColumnNumber].ColumnName.ToString();} set {DataTable.Columns[ColumnNumber].ColumnName = value;} } Is this acceptable practice? Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

    P 1 Reply Last reply
    0
    • W Wayne Phipps

      There is a method of the DataTable object that I require to access from various methods of my class basically to retrieve the column name as a specific index. I have created a private property in my class to access this method and defined it similar to as follows: private string ColumnName { get {return DataTable.Columns[ColumnNumber].ColumnName.ToString();} set {DataTable.Columns[ColumnNumber].ColumnName = value;} } Is this acceptable practice? Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

      P Offline
      P Offline
      perlmunger
      wrote on last edited by
      #2

      This is certainly "acceptable", however, you might ask yourself "what is the benefit?". Considering that your DataTable and ColumnNumber fields appear to be instance variables, there's no reason when you need that specific column name you can't just call it directly as in DataTable.Columns[ColumnNumber].ColumnName. If what you want is a short cut method, create some methods like this (that aren't properties).

      private string GetColumnNameAt( int index )
      {
      if( DataTable.Columns[index] != null )
      return DataTable.Columns[index].ColumnName;
      }

      private void SetColumnNameAt( int index, string name )
      {
      if( DataTable.Columns[index] != null )
      DataTable.Columns[index].ColumnName = name;
      }

      Let me know if you need clarification. Best Regards. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups