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. Simplify this code

Simplify this code

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    if(!reader.IsDBNull(4))
    {
    c.SourceVideoClip=reader.GetString(4).Trim();
    }

    Can the above code, be refactored to use the tertiary operator?

    /\ |_ E X E GG

    E N 2 Replies Last reply
    0
    • E eggie5

      if(!reader.IsDBNull(4))
      {
      c.SourceVideoClip=reader.GetString(4).Trim();
      }

      Can the above code, be refactored to use the tertiary operator?

      /\ |_ E X E GG

      E Offline
      E Offline
      eggie5
      wrote on last edited by
      #2

      Does this work:

      c.SourceVideoClip= (!reader.IsDBNull(4)) ? c.SourceVideoClip = reader.GetString(4).Trim() : null;

      /\ |_ E X E GG

      L 1 Reply Last reply
      0
      • E eggie5

        Does this work:

        c.SourceVideoClip= (!reader.IsDBNull(4)) ? c.SourceVideoClip = reader.GetString(4).Trim() : null;

        /\ |_ E X E GG

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

        No, when false no assignment should occur. Where did the null come from ? The one possibility I see is: c.SourceVideoClip= (!reader.IsDBNull(4)) ? reader.GetString(4).Trim() : c.SourceVideoClip; but even that is not absolutely correct; in several situations it could differ from the original (when the condition is false): when c is null; when SourceVideoClip is a property without a getter; when it is a property with side effects in its getter; when it is a property that is not returning what you set it to (you should not do this, but you could...) when it is volatile, i.e. other threads (or hardware) may cause it to change; There may be a better attempt if something more was known about the c type ! :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        1 Reply Last reply
        0
        • E eggie5

          if(!reader.IsDBNull(4))
          {
          c.SourceVideoClip=reader.GetString(4).Trim();
          }

          Can the above code, be refactored to use the tertiary operator?

          /\ |_ E X E GG

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Sure SELECT param1, param2, param3, param4, ISNULL(SourceVideoClip, '') FROM MyTable c.SourceVideoClip=reader.GetString(4).Trim();


          only two letters away from being an asset

          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