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. Get constant from column ordinal.

Get constant from column ordinal.

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 4 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.
  • M Offline
    M Offline
    MAW30
    wrote on last edited by
    #1

    I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael

    L B L 3 Replies Last reply
    0
    • M MAW30

      I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The parameter in a switch statement must be an actual constant whose value is available at compile time. Your code is using a variable that is not available to the compiler. You will need to use an if statement.

      1 Reply Last reply
      0
      • M MAW30

        I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        See if this gives you useful ideas:

        public class SwitchOnDTOrdinal
        {
        public SwitchOnDTOrdinal(DataTable table)
        {
        TheTable = table;

            validColumnNames = table.Columns
                .Cast()
                .Select(col => col.ColumnName)
                .ToList();
        }
        
        private List validColumnNames;
        
        private DataTable TheTable;
        
        public void SwitchA(string cname)
        {
            if (! validColumnNames.Contains(cname))
            {
                throw new ArgumentException(@"{cname} is not a valid column name");
            }
        
            switch(TheTable.Columns\[cname\].Ordinal)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        

        }

        «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

        1 Reply Last reply
        0
        • M MAW30

          I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, for Ordinal I agree with what has already been said. However you might consider working with the columns Name or Caption property; if one of those has known values you can still use a simple switch, as C# allows switching on string literals. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          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