Solved

constructor not working

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

912 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

18 Experts available now in Live!

Get 1:1 Help Now