-1

I am using a Windows Form MainForm which calls the following:

FileSystemLayer FSL = new FileSystemLayer();
DatabaseLayer DBL = new DatabaseLayer();

What I want to do:

(inside the DatabaseLayer class)

String SomeString = MainForm.FSL.AnotherString;

The problem

The FileSystemLayer FSL is inaccessible due to its protection level

I've tried

public FileSystemLayer FSL = new FileSystemLayer();
public DatabaseLayer DBL = new DatabaseLayer();

But it gave the result

Inconsistent accessibility: 'DatabaseLayer' is less accessible than field 'MainForm.DBL'

So I'm hoping someone can give me some advice on how to communicate between the two classes.

Robin
  • 1,927
  • 3
  • 18
  • 27
  • 1
    http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp – Zaki Jul 04 '13 at 16:00
  • 1
    If you made the DatabaseLayer Class public, I think this would solve the problem. But the bigger issue is that I think you should heed the advice of the link above, or at lease pass a reference to FSL down to DBL instead of trying to access FSL from DBL after the fact. Think public DatabaseLayer DBL = new DatabaseLayer(FileSystemLayer fSL); –  Jul 04 '13 at 16:09

3 Answers3

0

That message is telling you that while the DBL member field of the main form is declared as public the type itself (DatabaseLayer) is declared as something other than public, most likely private, or internal

So you can solve the problem by modifying the the DatabaseLayer class and making it public class DatabaseLayer.

That said, the main purpose of encapsulating functionality into the file system layer and the database layer respectively is to loosen coupling. But if you expose one to the other directly and allow one to call into the other than you're sort of throwing the benefit out the window. You should consider alternative approaches to facilitating communication between the two layers.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • When I change the DatabaseLayer class to public all the public functions in the class get the error message instead. – Robin Jul 04 '13 at 16:15
  • and when I make the functions private they can not be accessed from my MainForm – Robin Jul 04 '13 at 16:18
  • The functions in the class that you are exposing should be marked public. The class itself can be public or internal. If you make the class internal, than make the DBL field internal as well (instead of public). – Mike Dinescu Jul 04 '13 at 16:19
  • Are all of your mentioned classes in the same assembly? – VahidNaderi Jul 04 '13 at 16:22
0

It seems that the DatabaseLayer class is defined as internal so if you want to make a property of that type to be accessible publicly, you should define the type (DatabaseLayer) public too, or you may make your property less visible by defining it as internal.

But as also it seems that you are using some kind of layering it wouldn't be the right way to achieve what you are looking for.

VahidNaderi
  • 2,448
  • 1
  • 23
  • 35
0

After trying several suggested solutions, that I couldn't manage to apply, I instead changed the hierarchy of the class calling like described below. Thanks to the people taking time to help me towards a solution.

enter image description here

(Them Paint skillz)

Robin
  • 1,927
  • 3
  • 18
  • 27