Solved

constructor not working

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 52
Diagnostics with Net and Net.Sockets 2 28
Simple Injector with Web Service 4 41
Problem!!! 8 33
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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