12 Replies - 778 Views - Last Post: 15 March 2011 - 06:43 AM Rate Topic: -----

#1 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Coding between class and functions

Posted 15 March 2011 - 02:41 AM

Hi Guys,

Can you take a look at this for me?

I've created a class called 'folder', but I've created an array for files.. I can't get the array to pass in as a function paramter. I've tried the following below but i get the red squiggly line. :helpsmilie:

 class Folder
    {
        string name;
        Array[] files;
        int accessPermission;
    }

    public Folder(string folderName, string[] files, int userAccessLevel)
    {
    }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 15 March 2011 - 03:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Coding between class and functions

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • Posts: 972
  • Joined: 30-September 10

Re: Coding between class and functions

Posted 15 March 2011 - 02:47 AM

Hi,

Your constructor (Folder()) isn't in the Folder class.

Plus, you've declared an array of type Array (meaning it can only hold objects of type Array). Should it not be of type String so that it can hold strings...

All arrays ultimately derive from the Array class (in fact, a separate type is created in the background, of which derives from the Array class, when you declare an array). So, when you declare a string array below, a string[] type is created of which derives from the Array class.

You want your array to hold strings, not Array objects...


class Folder
{
     string name;
     string[] files; //holds strings
     int accessPermission;

     public Folder(string folderName, string[] files, int userAccessLevel)
     {
     }
}



Plus, I would use an enumeration for the 'accessPermission' variable, rather than a plain int.

This post has been edited by CodingSup3rnatur@l-360: 15 March 2011 - 02:56 AM

Was This Post Helpful? 2
  • +
  • -

#3 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Coding between class and functions

Posted 15 March 2011 - 02:52 AM

Damn, I knew it was something silly. Nice one! :bananaman:
Was This Post Helpful? 0
  • +
  • -

#4 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Coding between class and functions

Posted 15 March 2011 - 03:32 AM

Guys,

Not sure if i've used the correct description above. I'll give you a quick breakdown:

I've created a program.cs file where I've written the main body of my code. From here I've added a class called folders.cs (separate page within VB2010). I've created a class titled 'Folder' and the code contains:

class Folder
    {
        string name;
        Array[] files;
        int accessPermissions;

        public Folder(string folderName, Array[] fileName, int userAccessPermissions)
        {
            folderName = name;
            fileName = files;
            userAccessPermissions = accessPermissions;

        }


In the main file I've tried to create a folder:

Folder folderOne = new Folder("folder1", "fileName1", 3);

But I'm getting a red line stating best overload method has invalid arguements. This is probably something simple that I've done wrong, can anyone help?

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 15 March 2011 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Coding between class and functions

Posted 15 March 2011 - 03:49 AM

your folder constructor is looking for an array of files as the second parameter, and you are providing a single string.

either pass in an array variable, or create another constructor that accepts a single string value.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Coding between class and functions

Posted 15 March 2011 - 04:44 AM

This should work..

Folder folderOne = new Folder("folder1", new string[] { "fileName1" }, 3);



Notice I am now passing an array as the second parameter.

Topics merged. Please refrain from posting duplicate topics. Once is enough.
Was This Post Helpful? 0
  • +
  • -

#7 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Coding between class and functions

Posted 15 March 2011 - 05:30 AM

View Posteclipsed4utoo, on 15 March 2011 - 04:44 AM, said:

This should work..

Folder folderOne = new Folder("folder1", new string[] { "fileName1" }, 3);



Notice I am now passing an array as the second parameter.

Topics merged. Please refrain from posting duplicate topics. Once is enough.



Sorry guys, I'm still quite new to DIC. All help is appreciated! I've noted to not keep posting new topics :sorcerer:
Was This Post Helpful? 0
  • +
  • -

#8 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Coding between class and functions

Posted 15 March 2011 - 05:48 AM

I've tried this:

1
Folder folderOne = new Folder("folder1", new string[] { "fileName1" }, 3);



But I'm getting this error:

'ConsoleApplication1.Folder' does not contain a constructor that takes two arguements

This is what I've written for the object:

 class Folder
    {
        string nameOfFolder;
        Array folder;
        int accessPermissions;

        public Folder(string name, Array[] folderName, int userAccessPermissions)
        {
            name = nameOfFolder;
            folder = folderName;
            accessPermissions = userAccessPermissions;

        }

Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Coding between class and functions

Posted 15 March 2011 - 05:58 AM

is there a specific reason you are using the Array class rather than a strongly typed array? like a string array?
Was This Post Helpful? 0
  • +
  • -

#10 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Coding between class and functions

Posted 15 March 2011 - 06:07 AM

Sorted it now, changed the Array[] to string[] and it worked. No more squiggly lines.. however, thats one problem sorted.

I've written this now to write the file:

bool writeReturn = folderOne.write(userOne.accessLevel, newFile4);


Again the root code is from the folder class:

public bool Write(string name, int accessPermission)
        {
            if (accessPermission >= accessPermissions)
            {
                // User can write file
            }

            return false;

        }


Sorry guys, I know I'm keeping on. I will get there in the end, still getting to grips with my programming.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Coding between class and functions

Posted 15 March 2011 - 06:18 AM

What is userOne.accessLevel? What is newFile4? You need to give us more code. We aren't mind readers.

What error are you getting?
Was This Post Helpful? 0
  • +
  • -

#12 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Coding between class and functions

Posted 15 March 2011 - 06:30 AM

This is the main body:

static void Main(string[] args)
        {
            User userOne = new User("Joe Bloggs", "dummies", 1);
            User userTwo = new User("Terry Griffiths", "snooker", 2);
            User userThree = new User("John Wayne", "cowboy", 3);

            File newFile1 = new File("File1", ".doc", 100);
            File newFile2 = new File("File2", ".jpeg", 75);
            File newFile3 = new File("File3", ".xls", 50);
            File newFile4 = new File("File4", ".txt", 25);

            File[] arr1 = new File[2];
            arr1[0] = newFile1;
            arr1[1] = newFile2;

            File[] arr2 = new File[2];
            arr2[0] = newFile3;
            arr2[1] = newFile4;

            Folder folderOne = new Folder("folder1", new string[] {"fileName1"}, 3);
            Folder folderTwo = new Folder("folder2", new string[] { "fileName2" }, 1);

            

            bool writeReturn = folderOne.write(userOne.accessLevel, newFile4);
            Console.WriteLine("User One written to folder one " + writeReturn);

            bool writeReturn2 = folderTwo.write(userThree.accessLevel, newFile4);
            Console.WriteLine("User Three written to folder two " + writeReturn2);




The 'newFile3' and 'newFile4' are example files that I've created, both of which have different access levels for the user. I've put an IF statement in so that if the user has the correct level of access to 'view' this file then the compiler will display that file.
Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Coding between class and functions

Posted 15 March 2011 - 06:43 AM

You have to be getting build errors as you are passing a User.accessLevel object(assuming is a string?) and a File object to a method that accepts a string and an integer as parameters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1