5 Replies - 297 Views - Last Post: 02 February 2022 - 10:37 AM Rate Topic: *---- 1 Votes

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 390
  • Joined: 13-March 12

How do I find where this variable is being set in VS2019?

Posted 27 January 2022 - 12:38 PM

I wrote a little test
        [TestMethod]
        public void DeletesAFileFromTheFTPServer()
        {
            FTPAccess access = new FTPAccess();
            string fn = "//Process1/220111.csv";
            _ = access.DeleteFile(fn);
            Assert.IsTrue(access.FileExists(fn) == false);
        }



I once assigned the value "DeletMe.txt" to the fileName. I had an empty text file I used as the file to delete. It workded fine so I moved on. When I ran the program the DeleteFile method failed.

        public bool DeleteFile(string fileName)
        {
            try
            {
                FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(FTPServer + fileName));
                req.Method = WebRequestMethods.Ftp.DeleteFile;
                //req.Credentials = new NetworkCredential(usr, pwd);
 
                using (FtpWebResponse response = (FtpWebResponse)req.GetResponse())
                {
                    return true;
                }

            }
            catch (WebException wx)
            {
                FtpWebResponse resp = (FtpWebResponse)wx.Response;
                if (resp.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                {
                    return false;
                }
            }
            return false;
        }



It failed because the file was unavailable. The file was unavailable because the fileName was = "//Process1/DeleteMe.txt." But in the test you can clearly see that the variable passed is supposed to be "//Process1/220111.csv." But it seems I cannot assign a value to a string variable. So fn, or fileName, or fName (I have tried all three) is not defined when the DeletFile method is called. When the test gets to the DeleteFile method the variable fileName = "//Process1/DeleteMe.txt."

I have searched for DeleteMe.txt in the Entire Solution and it was not found. I searched for fileName and it was found exactly where it is being used and that's it.

I was going to include screen shots from Debugging here, but, apparently I can't.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I find where this variable is being set in VS2019?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16481
  • View blog
  • Posts: 65,329
  • Joined: 12-June 08

Re: How do I find where this variable is being set in VS2019?

Posted 27 January 2022 - 01:06 PM

When you searched did you do an 'alt+f2' to find all references on the variable? Alternatively right click on it and 'find all references'?

Alternatively you would do the same on the method name, and see where it's being called. You may have missed something.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,430
  • Joined: 05-May 12

Re: How do I find where this variable is being set in VS2019?

Posted 27 January 2022 - 06:18 PM

Instead of just running the test, debug the test. Step through the code including stepping into the method call.
Was This Post Helpful? 0
  • +
  • -

#4 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 390
  • Joined: 13-March 12

Re: How do I find where this variable is being set in VS2019?

Posted 31 January 2022 - 10:18 AM

View PostSkydiver, on 27 January 2022 - 07:18 PM, said:

Instead of just running the test, debug the test. Step through the code including stepping into the method call.

I did just that. That is where the screen shots that I cannot post come from. I also looked up all the places the methods in question are called from. For the test that was in the oringinal post, I deleted it, built the project and retyped a new method. I did not paste it. So the original problem wen away. Now the error is 'the name thisFile is not valid in this context.' thisFile is the name of the variable in the signature. It no longer has the value of DeleteMe, now it is just not in context. The Method DoesThisFileExist is displaying the same problem.

        [TestMethod]
        public void ChecksForFileExists()
        {
            FTPAccess ax = new FTPAccess();
            bool exists = ax.DoesFileExist(@"\Process1\220111Trash.CSV");
            Assert.IsTrue(exists);
        }



       public bool DoesFileExist(string thisFile)  //Error 'thisFile' Does not exist in the current context.
        {
            try
            {
                FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(FTPServer + thisFile));  //break point here 
                                                                                                      //
                req.Method = WebRequestMethods.Ftp.GetFileSize;
                FtpWebResponse resp = (FtpWebResponse)req.GetResponse();
                if (resp.ContentLength > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (WebException wx)
            {
                FtpWebResponse resp = (FtpWebResponse)wx.Response;
                if (resp.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                {
                    return false;
                }
            }
            return false;
        }




Something else. When stepping through a method, each line about to be executed is highlighted in yellow. As I step through these methods, only half of the line is highlighted. In both mentioned methods, the file name being passed is not passed. It is set at the moment the method is called, it just doesn't make it through the call.

Can't wait to hear from y'all.
Was This Post Helpful? 0
  • +
  • -

#5 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 390
  • Joined: 13-March 12

Re: How do I find where this variable is being set in VS2019?

Posted 01 February 2022 - 05:54 AM

Update: While running the program, the methods (except DeleteThisFile) work fine. I unloaded the Test project and reloaded it. No change in behavior.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,430
  • Joined: 05-May 12

Re: How do I find where this variable is being set in VS2019?

Posted 02 February 2022 - 10:37 AM

To me it almost feels like the PDB for your code doesn't match up with the code. If you have any antivirus running, try disabling it during a clean build followed immediately by a debugging session.

I would also suggest doing a repair on your VS install as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1