referencing a class member from another file

D

DjordjeD

I need to call(reference) class member
"PrintDestination.PrintDestinationPrinter" from another file. How do I
do it, I tried
"ReportRenderer.PrintDestination.PrintDestinationPrinter" but it
doesn't work. Please help!

here is my class:

*****************************************

using System;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using ABELSoft.Reporting.User.Facade;

namespace ABELSoft.Reporting.User.Facade
{
/// <summary>
/// Summary description for ReportRenderer.
/// </summary>
public class ReportRenderer
{
private CrystalDecisions.CrystalReports.Engine.ReportDocument
m_ReportDocument;
private string m_sReportName;
private PrintDestination m_PrintDestination;
private string m_sDestinationName;
private DataSet m_ReportData;

public void RenderReport()
{
switch( m_PrintDestination )
{
case PrintDestination.PrintDestinationScreen:
{
ReportPreviewFac Preview = new ReportPreviewFac();
Preview.ReportSource = m_ReportDocument;
Preview.ShowDialog();
break;
}
case PrintDestination.PrintDestinationPrinter:
{
m_ReportDocument.PrintToPrinter( 1, false, 0, 0 );
break;
}
case PrintDestination.PrintDestinationFile:
{
m_ReportDocument.Export();
break;
}
}
}

public enum PrintDestination
{
PrintDestinationPrinter,
PrintDestinationScreen,
PrintDestinationFile
}
#endregion public routines
}
}
 
I

Ian Griffiths [C# MVP]

There are two ways you can do this.

(1) Compile both source files into the same binary output file.

or

(2) Reference the binary file that contains the code you need to use when
building.

Note that both of these require you to do something to the input for the
compiler.

If you are using Visual Studio .NET, here's how it will look:

(1) Make sure the two source files are in the same project. That is all.

(2) Make sure the calling project has a reference to the code being called.
Use the 'Add References' feature. In Solution Explorer, right-click on the
References item for the project that's going to call the function and select
Add References. Add a reference to the component that contains the code you
wish to call. If the component is a different project in the same solution,
add it as a 'Project' reference. Otherwise, Browse... for the DLL.


If you are using the compiler from the command line, here's how it will
look:

(1) csc SourceFile1.cs SourceFile2.cs

or

(2) csc /r:ExternalComponent.dll SourceFile1.cs

That "/r" switch is what a 'reference' in VS.NET means. It says "The code
I'm compiling now makes use of classes defined in this external DLL here".

Regardless of which of these two techniques you use, the source code will
look the same. You can either use the fully qualified name, e.g.
ABELSoft.Reporting.User.Facade.ReportRenderer.PrintDestination.PrintDestinationPrinter,
or you can add a 'using' statement to the top of the file:

using ABELSoft.Reporting.User.Facade;

and then just refer to it as
ReportRenderer.PrintDestination.PrintDestinationPrint.

Note that the 'using' statement does not render the steps mentioned earlier
optional. You still need to do either (1) or (2). The 'using' statement is
*never* interpreted by the C# compiler as an instruction to go and look for
some external file. It simply tells it that you plan not to bother using
the full namespace every time.

By the way, in C#, it's not normal to do this:

public enum PrintDestination
{
PrintDestinationPrinter,
PrintDestinationScreen,
PrintDestinationFile
}

This looks like something a C++ developer would do. In C# we normally do
this:

public enum PrintDestination
{
Printer,
Screen,
File
}

That would be a bad idea in C++ of course because in C++, enumeration entry
names are scoped by namespace. But in C#, you never ever see the name of
an enumeration entry used in isolation. It is *required* to be referred to
via its containing enum name. So it would always be
PrintDestination.Printer, etc. Given this fact, putting the enum name in
the enum member is always redundant.
 

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