Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

constructor not working

Posted on 2014-10-29
3
72 Views
Last Modified: 2014-11-18
Im quite new to c# and working on my first project.

What Im trying to do is create a mass of objects in my case a load of directories, eventually I want to process the files in the directory, but at the moment concentrating on getting the framework working.

So what I have so far is:-
    class directory
    {
        public directory(String directory, ref worker worker)
        {
            if (worker.canIrun())
            {
                worker.runNew("x:\newDirectory");
            }
        }
    }

    class worker
    {
        public worker(String directory)
        {
            this.runNew(directory);
        }
        public void runNew(String directory)
        {
            directory newDirectory = new directory(directory, this);
        }
        public Boolean canIrun()
        {
            return true;
        }
    }

Open in new window


So to call this in my main program, I want to be able to run something like:-
worker newWorker = new worker("c:\\firstDirectory\\);

Open in new window


However I keep getting advised that my constructor doesnt accept my parameters.

What am I doing wrong????
0
Comment
Question by:tonelm54
3 Comments
 
LVL 19

Expert Comment

by:Peter Hutchison
ID: 40412187
The problem is this line:

 directory newDirectory = new directory(directory, this);

You have a parameter called directory, a variable type called directory and a method called directory. A bit of a mess really.
Try changing the names.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40412237
Your directory ctor takes the worker argument by ref, so you need to pass it in by ref:

directory newDirectory = new directory(directory, ref this);

Open in new window


As well as what Peter's advised I would suggest you look into creating your own coding standards. This will dictate things such as how you name your classes and members. For example, a common standard is to name your classes and members using PascalCase and then name your variables using camelCase. This is the format that Microsoft use, so you'll have some consistency between your own classes and the rest of the NET framework. It also makes reading your code easier - you'll quickly be able to identify what is a type or class member and what is a variable. Something simple like this can make a lot of difference:

class Directory
{
	public Directory(String directory, ref Worker worker)
	{
		if (worker.CanIRun())
		{
			worker.RunNew(@"x:\newDirectory");
		}
	}
}

class Worker
{
	public Worker(String directory)
	{
		this.RunNew(directory);
	}
	
	public void RunNew(String directory)
	{
		Directory newDirectory = new Directory(directory, ref this);
	}
	public Boolean CanIRun()
	{
		return true;
	}
}

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 40412288
Just to add about coding standards. In a team it's very important. Please see MS notes:
http://msdn.microsoft.com/en-us/library/vstudio/ms229043(v=vs.100).aspx
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question