Referencing another source file

P

PLS

I guess I'm a bit confused about how to organize a C# project.

I have two .cs files compiling into one assembly. Both place classes
into the same namespace.

File A contains a class with a bunch of static methods that I want to
use in file B.

What do I put into B to make the class and methods visible?

Thanks,
++PLS
 
N

Nicholas Paldino [.NET/C# MVP]

PLS,

If the classes are in the same namespace, in the same assembly, you
should be able to make the call by using the class name, followed by the
static method/property/field you wish to access. Basically, like any other
static class. For example:

In File 1:

public static class StaticClass
{
public static void DoSomething()
{
}
}

In File 2:

public class OtherClass
{
// Make the call.
public void DoSomethingElse()
{
// Call the static method on StaticClass.
StaticClass.DoSomething();
}
}
 
C

Chris Mullins [MVP - C#]

PLS said:
I guess I'm a bit confused about how to organize a C# project.

I have two .cs files compiling into one assembly. Both place classes
into the same namespace.

File A contains a class with a bunch of static methods that I want to
use in file B.

What do I put into B to make the class and methods visible?

Odds are, you're just missing the keyword "Public".

Put it on your class definitions:

Public class MyClass
{
}

And on the methods you want in the class to be visible.
 
N

Nicholas Paldino [.NET/C# MVP]

I doubt that is it. If they are in the same assembly, then a missing
access modifier on the class will default to internal, which would make it
accessible to the other class in the same assembly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top