locked
Using a Switch block with CRM DataTypes RRS feed

  • Question

  • I am reading a CRM Entity extracting column values to populate a DataTable. Example:

       if (myentity[p.Name] is CrmBoolean)
       {
        dr[p.Name] = ((CrmBoolean)myentity[p.Name]).Value;
       }
    
       else if (myentity[p.Name] is CrmFloat)
       {
        dr[p.Name] = ((CrmFloat)myentity[p.Name]).Value;
       }
    
       else if (myentity[p.Name] is Picklist)
       {
        dr[p.Name] = ((Picklist)myentity[p.Name]).Value;
       }
    Etc...

    I'd like to use a Switch block like:

    switch (myentity[p.Name])
    {
       CrmBoolean:
             dr[p.Name] = ((CrmBoolean)myentity[p.Name]).Value;
             break;

      CrmFloat:
             dr[p.Name] = ((CrmFloat)myentity[p.Name]).Value;
             break;

      Picklist:
             dr[p.Name] = ((Picklist)myentity[p.Name]).Value;
             break;

             Etc...
    }

    Is there a way to do this? Can any of you gurus show me how?


    Baruch Barness
    Friday, March 4, 2011 3:45 PM

Answers

  • This is a standard .NET issue. TestClass in the following code could be CrmBoolean or any type

     private void Form1_Load(object sender, EventArgs e)
      {
       testclass test = new testclass();
       method(test);
    
       
      }
    
      private void method(object obj)
      {
       switch (obj.GetType().Name)
       {
        case "testclass":
         MessageBox.Show(obj.GetType().Name);
          //will enter this block and show messagebox "testclass"
         break;
        
        case "CrmBoolean":
    
        case "anyobjectname":
    
       }
      }
    

    Jamie Miley
    Friday, March 4, 2011 4:10 PM
    Moderator

All replies

  • This is a standard .NET issue. TestClass in the following code could be CrmBoolean or any type

     private void Form1_Load(object sender, EventArgs e)
      {
       testclass test = new testclass();
       method(test);
    
       
      }
    
      private void method(object obj)
      {
       switch (obj.GetType().Name)
       {
        case "testclass":
         MessageBox.Show(obj.GetType().Name);
          //will enter this block and show messagebox "testclass"
         break;
        
        case "CrmBoolean":
    
        case "anyobjectname":
    
       }
      }
    

    Jamie Miley
    Friday, March 4, 2011 4:10 PM
    Moderator
  • Thanks Jamie. I guess it was just standard .net.
    Baruch Barness
    Friday, March 4, 2011 4:32 PM