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. Gawd but I hate the way MS changed null handling...

Gawd but I hate the way MS changed null handling...

Scheduled Pinned Locked Moved C#
2 Posts 1 Posters 177 Views
  • 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.
  • OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote last edited by
    #1

    A chunk of code:

                string[] files = Directory.GetFiles(@"D:\Test data\Invoices");
                foreach (string oldPath in files)
                    {
                    string oldName = Path.GetFileNameWithoutExtension(oldPath);
                    string[] parts = oldName.Split(" - ");
                    int id = int.Parse(parts[0].Substring(parts[0].Length - 3));
                    string newName = $"Invoice {id:000} - {parts[1]}";
                    string newPath = Path.Combine(Path.GetDirectoryName(oldPath), oldPath.Replace(oldName, newName));
                    if (oldPath != newPath)
                        {
                        System.IO.File.Move(oldPath, newPath);
                        }
                    }
    

    It's not pretty, but it's a one off run to change demo file names to include leading zeros:

    Invoice   1 - Customer name.inv
    

    becomes

    Invoice 001 - Customer name/inv
    

    And MS doesn't like it, I get a warning:

    Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.
    

    But ... and this is the really stupid bit ... I know it's never null, AND SO DOES THE COMPILER! How can I tell? If I hover over oldPath I get "oldPath is not null here"! But it still gives me a green squiggle and a warning message.

    Yes, I can use #pragma to disable and restore the warning, but ... Stupid, stupid ...

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    1 Reply Last reply
    0
    • Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote last edited by Richard Deeming
      #2

      It's not moaning about oldPath; it's moaning about the first parameter, which is the return value of Path.GetDirectoryName(oldPath).

      Looking at the source, that method is declared as returning string?, not string. The documenation says:

      The returned value is null if the specified path is null, empty, or a root (such as "\", "C:", or "\\server\share").

      In this instance, since you know that's not the case, you can suppress the warning with the "damnit" (or "Janet Weiss") operator:

      string newPath = Path.Combine(Path.GetDirectoryName(oldPath)!, oldPath.Replace(oldName, newName));
      

      However, since you're not looking at subdirectories, you know that all files will be in the same directory. So wouldn't it make sense to pass in the known directory instead?

      string newPath = Path.Combine(@"D:\Test data\Invoices", ...
      

      Also, I don't think that Path.Combine is really doing what you think. You're passing in a full path as the second parameter, so the first parameter will effectively be ignored. I suspect you meant to pass newName as the second parameter, rather than oldPath.Replace(oldName, newName):

      string newPath = Path.Combine(@"D:\Test data\Invoices", newName);
      

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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