Resolved why my member variable is empty?

KWPC

New member
Joined
May 20, 2021
Messages
2
Programming Experience
Beginner
Hi

I have the following object oriented code that I'm trying to figure out why my member variable is empty.
I'm having an issue with a problem I'm working on and I'm seeking some help on what to try. I've got the following code but not sure why my member variable mPath does not seem to get an initial value in my Constructor. I'm not looking for someone to give me the code, but trying to figure out why mPath is empty or what is going on. Any help would be greatly appreciated.
C#:
using System.IO;
using System;

public class Client
{
   public virtual bool ProcessFile(string name)
   {
      return (true);
   }

   public virtual bool ProcessDirectory(string name)
   {
      return (true);
   }
}

public class PrintingClient : Client
{
   public override bool ProcessFile(string name)
   {
      Console.WriteLine("file: {0}", name);
      return (true);
   }

   public override bool ProcessDirectory(string name)
   {
      Console.WriteLine("directory: {0}", name);
      return (true);
   }
}

public class Scanner
{
   public string TheDirectory { get; set; }
   public Client TheClient { get; set; }
   public bool Recurse { get; set; }

   public Scanner(string theDirectory, Client theClient, bool recurse)
   {
      TheDirectory = theDirectory;
      TheClient = theClient;
      Recurse = recurse;
   }

   public Scanner(string theDirectory) :
      this(theDirectory, new Client(), true)
   {
   }

   public Scanner(string theDirectory, bool recurse) :
      this(theDirectory, new Client(), recurse)
   {
   }

   public Scanner() : this(".", new Client(), true)
   {
   }

   public void Execute()
   {
      ScanDirectory(TheDirectory);
   }

   private bool ScanDirectory(string theDirectory)
   {
      string[] files = Directory.GetFiles(theDirectory);

      foreach (string file in files)
         if (!TheClient.ProcessFile(file))
            return (false);

      string[] dirs = Directory.GetDirectories(theDirectory);

      foreach (string dir in dirs)
         if (!TheClient.ProcessDirectory(dir))
            return (false);

      if (Recurse)
      {
         foreach (string dir in dirs)
            if (!ScanDirectory(dir))
               return (false);
      }

      return (true);
   }

   public class LargestFile : Client
   {
      private long mLargestSize = 0;
      private string mLargestName = "";
      public override bool ProcessFile(string name)
      {
         FileInfo fi = new FileInfo(name);

         if (fi.Length > mLargestSize)
         {
            mLargestName = name;
            mLargestSize = fi.Length;
         }
         return (true);
      }

      public void ShowLargest()
      {
         Console.WriteLine("largest-file     : {0}", mLargestName);
         Console.WriteLine("largest-file-size: {0}", mLargestSize);
      }

   }

   public class SearchForFile : Client
   {
      public SearchForFile(string fileName)
      {
         mSearchFor = fileName;
         mPath      = "";
      }
      public override bool ProcessFile(string name)
      {
         FileInfo fi1 = new FileInfo(name);
         mSearchFor = name;
         // mPath = "";

         if (mSearchFor == fi1.Name)
         {
            mPath = fi1.FullName;
            return (false);
         }

         else
         {
            mSearchFor = "Not Found";
            mPath      = "Not Found";
            return (true);
         }
      }

      public void ShowSearchResults()
      {
         Console.WriteLine("Search-for: {0}", mSearchFor);
         Console.WriteLine("File-Location: {0}", mPath);
      }

      public String mSearchFor = "";
      public String mPath = "";

   }
   public static void Main()
   {
      LargestFile lf = new LargestFile();

      SearchForFile sff = new SearchForFile("one");

      var theScanner = new Scanner("c:\\temp", true);

      theScanner.TheClient = lf;

      theScanner.Execute();

      lf.ShowLargest();

      theScanner.TheClient = sff;

      sff.ShowSearchResults();

      Console.Read();
   }
}
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,921
Location
Sydney, Australia
Programming Experience
10+
You need to debug your code. If you don't know how to use the VS debugger, stop what you're doing and learn now.

The first thing I'd do is put breakpoints on all the places that that field can be set and then run the app to see if any are hit. In your case, presumably either none will be hit or else more than one will be hit, with the last one clearing the field. Once you've confirmed that, you will then need to find out why execution doesn't follow the path you expect. To do that, place a breakpoint at some appropriate place early in the execution of the code and then step through it line by line to see where it goes and how that differs from your expectations.

For future reference, when formatting your code you can highlight one or more lines. It would nice if you could do that, particularly in such a large block of code, to draw our attention to the specifically relevant parts, e.g. where you expect your field to be set. You might also explain what you actually expect to happen. If we were to take a cynical approach the answer to "why doesn't my field get set" is "because you never set it". If we don't know what you actually expect to happen and why, how can we tell you why it doesn't actually happen? We shouldn't have to reverse engineer your logic from code that doesn't actually implement that logic.
 

KWPC

New member
Joined
May 20, 2021
Messages
2
Programming Experience
Beginner
You need to debug your code. If you don't know how to use the VS debugger, stop what you're doing and learn now.

The first thing I'd do is put breakpoints on all the places that that field can be set and then run the app to see if any are hit. In your case, presumably either none will be hit or else more than one will be hit, with the last one clearing the field. Once you've confirmed that, you will then need to find out why execution doesn't follow the path you expect. To do that, place a breakpoint at some appropriate place early in the execution of the code and then step through it line by line to see where it goes and how that differs from your expectations.

For future reference, when formatting your code you can highlight one or more lines. It would nice if you could do that, particularly in such a large block of code, to draw our attention to the specifically relevant parts, e.g. where you expect your field to be set. You might also explain what you actually expect to happen. If we were to take a cynical approach the answer to "why doesn't my field get set" is "because you never set it". If we don't know what you actually expect to happen and why, how can we tell you why it doesn't actually happen? We shouldn't have to reverse engineer your logic from code that doesn't actually implement that logic.
Thanks for your response. I actually was using the debugger but all points I tried gave and empty string "" for mPath the member variable I was trying to resolve in my original post. I found my other member variable mSearchFor was working and sending the string back for my showSearchResults method. I definitely should have been more clear and spent more time on posing my question. I have never ever posted here or any other technical forum and wasn't sure what to expect. I certainly will take your advice and provide the reviewer a more direct approach to my questions and expectations.

Thanks for your reply and I will add below the area and what I'm looking for.

In the following subclass from Client I want to find out why I'm getting and empty string in the debugger and in the output for my mPath member variable. This new snippet of code has the ToString() method attached and still no luck unfortunately. My instinct says something is wrong in the constructor of my overriding ProcessFile(string name) or maybe I need to { get; set; } ?. The latest full code is now attached so there is no version confusion.

Lastly I'm expecting the one.bmp to be found and ShowSearchResults should output to the console the path c:\temp\one.bmp. The recursive feature is set to true!

C#:
public class SearchForFile : Client
   {
      public SearchForFile(string fileName)
      {
         mSearchFor = fileName;
         // mPath      = "";
      }
      public override bool ProcessFile(string name)
      {
         FileInfo fi1 = new FileInfo(name);
         mPath = fi1.FullName.ToString();

         if (mSearchFor == fi1.Name)
         {
            mPath = fi1.FullName.ToString();
            return (true);
         }

         else
         {
            mSearchFor = "Not Found";
            mPath      = "Not Found";
            return (true);
         }
      }

      public void ShowSearchResults()
      {
         Console.WriteLine("Search-for: {0}", mSearchFor);
         Console.WriteLine("File-Location: {0}", mPath);
      }

      private String mSearchFor = "";
      private String mPath = "";

   }
   public static void Main()
   {
       LargestFile lf = new LargestFile();

       SearchForFile sff = new SearchForFile("one.bmp");

       var theScanner = new Scanner("c:\\temp", true);

       theScanner.TheClient = lf;

       theScanner.Execute();

       lf.ShowLargest();

       theScanner.TheClient = sff;

       sff.ShowSearchResults();

       Console.Read();
   }
}
 

Attachments

  • SFF.txt
    3.6 KB · Views: 1
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,387
Location
Chesapeake, VA
Programming Experience
10+
You are doing your search on line 47, but you are assigning sff to theScanner.TheClient on line 51.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,921
Location
Sydney, Australia
Programming Experience
10+
In the following subclass from Client I want to find out why I'm getting and empty string in the debugger and in the output for my mPath member variable.
If you're "getting and empty string in the debugger and in the output for my mPath member variable" it is because that is the value of that variable. As I have already said, if that is not what you expect then you need to use the debugger to find out why. You're initialising that field with an empty string so that is obviously going to be the initial value. Where EXACTLY in that code are you expecting a different value to be assigned? Place a breakpoint on that line. Does it get hit? If not then that explains why the value is never set. You then have to work out why it doesn't get hit, so place a breakpoint earlier in the code that you know will be hit and step through the code line by line to see what path of execution it does take and where and why that differs from your expectation. You should know exactly what path you expect execution to take and, if you're using the debugger properly, you should know exactly what path it does take. If you don't know both those things then you need to spend more time working them out. If you do know but you still don't know why then you need to explain ALL the relevant information to us, i.e. where EXACTLY do expectation and reality diverge and EXACTLY what data is in use at the time.
 
Top Bottom