Can anyone look at my code and tell me how I messed up my class?

Leopardfist

Member
Joined
Nov 25, 2014
Messages
14
Programming Experience
Beginner
Comments explain how its messed up, or what is going wrong, but I don't know what I did wrong.

C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace testappForHDProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the Employee's Username: ");
            string uName = Console.ReadLine();
            Console.WriteLine("Please enter the Employee's Profile Server: ");
            string server = Console.ReadLine();
            Console.WriteLine("Please enter the Computer Number: ");
            string sysName = Console.ReadLine();
            paths ppath = new paths();
            ppath = buildPaths(uName, server, sysName);
            verify(ppath);
            Console.WriteLine("Starting Job now!");
            //My code I entered here is not working like I expected.
            //I entered the first couple names of my "ppath" class
            //but it does not pop-up any properties or methods for 
            //me to use to interact with it, it is like it does not recognize ppath.
            //Below, when I built the buildPaths function, they worked
            //fine for me, and intellisense would pop them up and let me auto enter
            //with enter or (). But here, nothing auto pops up.


            //Any ideas what I have done wrong? Code here is incomplete, but the below
            //code should give you an idea of how I built my class, and what my 
            //other function does in assigning values to each class property in the path class.
            //I assumed I could call them and use methods on them... like ppath.profile.rename();
            //or ppath.profile.delete(); 
            
            


        }


        private static void verify(paths path)
        {
            bool check = Directory.Exists(path.profile);
            if (check)
            {
                return;
            }
            else
            {
                Console.WriteLine("The data you entered was not correct, a complete path could not be verified!");
                Environment.Exit(0);
            }
        }


        static paths buildPaths(string uName, string server, string sysName)
        {
            paths path = new paths();
            path.appData = "\\\\" + server + "\\Users\\" + uName + "\\Application Data";
            path.citrix = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Citrix";
            path.citrixTemp = "\\\\" + server + "\\Users\\" + uName + "\\CitrixTemp";
            path.cookies = "\\\\" + server + "\\Users\\" + uName + "\\Cookies";
            path.icaClient = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\ICAClient";
            path.microsoft = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Microsoft";
            path.profileUnity = "\\\\" + server + "\\Users\\" + uName + "\\ProfileUnity";
            path.sun = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Sun";
            path.profile = "\\\\" + server + "\\Profiles\\" + uName;
            path.profileOld = path.profile + ".old";
            path.profileV2 = path.profile + "V2";
            path.systemProfile = "\\\\" + sysName + "\\c$\\Documents and Settings\\" + uName;
            path.profiles = "\\\\" + server + "\\Profiles";
            path.users = "\\\\" + server + "\\Users";
            return (path);
        }
    }
    class paths
    {
        public string profile { get; set; }
        public string profileV2 { get; set; }
        public string profileOld { get; set; }
        public string citrixTemp { get; set; }
        public string profileUnity { get; set; }
        public string cookies { get; set; }
        public string citrix { get; set; }
        public string icaClient { get; set; }
        public string sun { get; set; }
        public string microsoft { get; set; }
        public string appData { get; set; }
        public string systemProfile { get; set; }
        public string users { get; set; }
        public string profiles { get; set; }




        static bool reName(string path)
        {
            Directory.SetCurrentDirectory(path.profiles);
            if (Directory.Exists(path.profileOld))
            {
                Directory.Delete(path.profileOld);
            }
            Directory.Move(path.profile, path.profileOld);
            return true;
        }
        static void Delete(string path)
        {
            Directory.SetCurrentDirectory("..\\" + path);
            try
            {
                Directory.Delete(path);
            }
            catch (Exception)
            {
                Console.WriteLine("Error Deleting :" + path);
            }
        }


    }


}
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
First of all, your code does not compile due to multiple errors in the reName method (all boiling down to the same issue).

Next your methods are private (it's the default if you don't specify another protection like public) and that is why they don't show.
Your methods are also static and static methods are used differently.

So first you need to make your methods public if you want to be able to access them.
C#:
[B] public [/B]static void Delete(string path)
Next you must decide how you want to access the methods. To use static methods, use the class name (not the instantiated object) followed by the method name. E.g
C#:
paths.Delete("some string");
But I don't think that that is what you want, after all you instantiated an object called ppath and you want your methods to work on that object. In which case you need to take the static keyword out of the method declaration

C#:
[B] public [/B]void Delete(string path)
and next you can use
C#:
ppath.Delete("some string");

And next, to (more or less) answer the question in the other thread. For your current approach, you can create a method in your paths class to delete the profile directory.
C#:
public void DeleteProfile()
{
    try
    {
        Directory.Delete(profile);
    }
    catch (Exception)
    {
        Console.WriteLine("Error Deleting :" + profile);
    }
}
And you can use it like
C#:
ppath.DeleteProfile();
 

Leopardfist

Member
Joined
Nov 25, 2014
Messages
14
Programming Experience
Beginner
wim, first of all a big thank you for the work you put into your reply. Your information was very very helpful, and my understanding of the Static Keyword was not that in depth, and now I really understand how it effects a method. I think your first comment was due to me not copying the entire code in, which I have done below. I get no errors but then again, I am not compiling it either, as these directories do not exist on my current machine, so hard to test.

Now, I have swapped my methods to public in my custom class as you suggested. But I still have the issue... and it was working before so I dont know how I broke it. When I type the name of my instantiated class, I get no intellisense popup showing it identifies it. Previously, when I built the method that defines these properties buildPaths(), the intellisense would pop up and let me choose my properties from a list.

Now it will not even do it in that method, and my properties were still public. I added public to my methods as you suggested.

I added comments in code, to explain exactly what I had changed since it did work. I swapped the instantiated name from path to ppath, because I noticed path is a method in System.IO. I did not want to use that. I also tried commenting out my new methods in my path class, to see if they were messing it up and it still doesnt work.

I have a suspicion, that when I changed the ppath, that it did not update for some reason.... OR when I changed how I instantiated ppaths it may have messed it up. I commented in the code, on how I previously instantiated it using
C#:
paths path = buildPaths(uName, server, sysName);

\\but I changed it to:
paths ppath = new paths();
ppath = buildPath(uName, server, sysName);
But below is my complete code, and alot more notes with specifics.
C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace testappForHDProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the Employee's Username: ");
            string uName = Console.ReadLine();
            Console.WriteLine("Please enter the Employee's Profile Server: ");
            string server = Console.ReadLine();
            Console.WriteLine("Please enter the Computer Number: ");
            string sysName = Console.ReadLine();
            paths ppath = new paths();//this line and the next, used to be one line. "paths path = buildPaths(uName, server, sysName);"
            ppath = buildPaths(uName, server, sysName);//but I changed when my intellisense stopped popping up, and made it like this. Also changed to ppath.
            verify(ppath);
            Console.WriteLine("Starting Job now!");
            //My code I entered here is not working like I expected.
            //I entered the first couple names of my "ppath" class
            //but it does not pop-up any properties or methods for 
            //me to use to interact with it, it is like it does not recognize ppath.
            //Below, when I built the buildPaths function, they worked
            //fine for me, and intellisense would pop them up and let me auto enter
            //with enter or (). But here, nothing auto pops up.


            //Any ideas what I have done wrong? Code here is incomplete, but the below
            //code should give you an idea of how I built my class, and what my 
            //other function does in assigning values to each class property in the path class.
            //I assumed I could call them and use methods on them... like ppath.profile.rename();
            //or ppath.profile.delete(); 
            


        }


        private static void verify(paths path)
        {
            bool check = Directory.Exists(path.profile);
            if (check)
            {
                return;
            }
            else
            {
                Console.WriteLine("The data you entered was not correct, a complete path could not be verified!");
                Environment.Exit(0);
            }
        }


        static paths buildPaths(string uName, string server, string sysName)
        {
            paths path = new paths();
            path.appData = "\\\\" + server + "\\Users\\" + uName + "\\Application Data";
            path.citrix = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Citrix";
            path.citrixTemp = "\\\\" + server + "\\Users\\" + uName + "\\CitrixTemp";
            path.cookies = "\\\\" + server + "\\Users\\" + uName + "\\Cookies";
            path.icaClient = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\ICAClient";
            path.microsoft = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Microsoft";
            path.profileUnity = "\\\\" + server + "\\Users\\" + uName + "\\ProfileUnity";
            path.sun = "\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Sun";
            path.profile = "\\\\" + server + "\\Profiles\\" + uName;
            path.profileOld = path.profile + ".old";
            path.profileV2 = path.profile + "V2";
            path.systemProfile = "\\\\" + sysName + "\\c$\\Documents and Settings\\" + uName;
            path.profiles = "\\\\" + server + "\\Profiles";
            path.users = "\\\\" + server + "\\Users";
            return (path);
            //In this method, when I wrote it, I could type "path." and the Intellisense 
            //would auto pop up with all the properties from my Class below, so that I 
            //could define them. After making your changes this does not happen.
        }
    }
    class paths
    {
        public string profile { get; set; }
        public string profileV2 { get; set; }
        public string profileOld { get; set; }
        public string citrixTemp { get; set; }
        public string profileUnity { get; set; }
        public string cookies { get; set; }
        public string citrix { get; set; }
        public string icaClient { get; set; }
        public string sun { get; set; }
        public string microsoft { get; set; }
        public string appData { get; set; }
        public string systemProfile { get; set; }
        public string users { get; set; }
        public string profiles { get; set; }


        //I even tried commenting out these two new methods, and intellisense still does not work for the properties.
        public bool reName(string path)
        {
            Directory.SetCurrentDirectory(path.profiles);
            if (Directory.Exists(path.profileOld))
            {
                Directory.Delete(path.profileOld);
            }
            Directory.Move(path.profile, path.profileOld);
            return true;
        }


        public void Delete(string path)
        {
            Directory.SetCurrentDirectory("..\\" + path);
            try
            {
                Directory.Delete(path);
            }
            catch (Exception)
            {
                Console.WriteLine("Error Deleting :" + path);
            }
        }


    }


}
Thanks for any help, your last message taught me more about class declarations than 2 chapters in my book lol.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
You still have the same issue with your Rename method ;)

//Any ideas what I have done wrong? Code here is incomplete, but the below
//code should give you an idea of how I built my class, and what my
//other function does in assigning values to each class property in the path class.
//I assumed I could call them and use methods on them... like ppath.profile.rename();
//or ppath.profile.delete();
You have a misunderstanding; methods don't apply to the to the variables but to the (instantiated) class. That's why intellisense does not show an delete method for ppath.profile; but it shows it for ppath.
You can achieve what you want (?) by creating an additional class.

            static void Main(string[] args)
        {
//
//
           paths ppath = new paths();//this line and the next, used to be one line. "paths path = buildPaths(uName, server, sysName);"
           ppath = buildPaths(uName, server, sysName);//but I changed when my intellisense stopped popping up, and made it like this. Also changed to ppath.
            verify(ppath);
            Console.WriteLine("Starting Job now!");

            ppath.profile.delete();
           
        }


        private static void verify(paths path)
        {
            bool check = Directory.Exists(path.profile.directoryname);
            if (check)
            {
                return;
            }
            else
            {
                Console.WriteLine("The data you entered was not correct, a complete path could not be verified!");
                Environment.Exit(0);
            }
        }


        static paths buildPaths(string uName, string server, string sysName)
        {
            paths path = new paths();
            path.appData = new paths.MyDirectory("\\\\" + server + "\\Users\\" + uName + "\\Application Data");
            path.citrix = new paths.MyDirectory("\\\\" + server + "\\Users\\" + uName + "\\Application Data\\Citrix");
//
//
            return (path);
        }
    }

    class paths
    {
        public MyDirectory profile;
        public MyDirectory profileV2;
        public MyDirectory profileOld;
        public MyDirectory citrixTemp;
        public MyDirectory profileUnity;
        public MyDirectory cookies;
        public MyDirectory citrix;
        public MyDirectory icaClient;
        public MyDirectory sun;
        public MyDirectory microsoft;
        public MyDirectory appData;
        public MyDirectory systemProfile;
        public MyDirectory users;
        public MyDirectory profiles;

        public class MyDirectory
        {
            public string directoryname;

            public MyDirectory(string path)
            {
                directoryname = path;
            }
            
            public void delete()
            {
                // delete code here
            }

            public void rename()
            {
                // rename code here
            }
        }
    }



Now you can use e.g. ppath.profile.delete() in your main method (as shown).

Some notes:
  • The class MyDirectory is declared inside your class path. You can not use it directly. If you want, you can also take it out of the path class and make it a class like the path class.
  • The class has a non-default constructor so you can set the directoryname when you instantiate it (see the changes in buildPaths).

With regards to the first two lines in the main method; you can happily use paths ppath = buildPaths(....); it's actually better to do it like that as your approach first instantiates an object (takes memory) and your call to buildPaths also instantiates (so takes memory again). Eventually the garbage collector might come along to free the non-used memory (the first instance), but I would not rely on that (I'm to much of a C programmer where I had to free memory myself).
 

Leopardfist

Member
Joined
Nov 25, 2014
Messages
14
Programming Experience
Beginner
Wim, I see what you did there, and that is EXACTLY what I was wondering about... how to have the methods make the changes to the properties :), I understand it now.

ok 2 things.... I am not showing any errors on my rename() method.... so you will have to elaborate.

and number 2.... you think I am having problem doing ppath.profile.delete(), but that was just my curiosity asking about that.... my class being broken somewhere is not even letting me do ppath.anything including properties.... which I could do previously, as you can see where I made my buildpaths method. I even tries it in there and it wont work anymore. I think when I changed from path to ppath, I messed something up in VS that linked it as an object or something, I don't know... I may have to rewrite it all in a new project.

Anyway, I am SUPER APPRECIATIVE of your help, you have a way of using examples in a way that it makes it possible for my old mind to grasp the purposes of the little things like the keywords that VS uses by default and such. The class built on a class definitely opens up possibilities for me :).

All of this is really academic for me, I am sure the program I write will just have a list with all the paths I need to work with, but I been trying to see if I could figure out a better way too challenge myself.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
        public bool reName(string path)
        {
            Directory.SetCurrentDirectory(path.profiles);
            if (Directory.Exists(path.profileOld))
            {
                Directory.Delete(path.profileOld);
            }
            Directory.Move(path.profile, path.profileOld);
            return true;
        }


The code does not compile ;) You should see red squirlies under e.g. path.profiles. The reason is that you pass a string (path) to the method. The string does not have properties like profile, profileOld etc.; your class paths however has those properties.
 
Top Bottom