?
Solved

constructor not working

Posted on 2014-10-29
3
Medium Priority
?
76 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
[X]
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
3 Comments
 
LVL 20

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 2000 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 30

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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