Solved

constructor not working

Posted on 2014-10-29
3
68 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 18

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 42

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now