Namespace issue when adding a C# class

This forum is meant for questions and discussions about the X# language and tools
Post Reply
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Namespace issue when adding a C# class

Post by boonnam »

I am trying to create a dll using X#. I started a Class Library project. Since I need to use a 3rd party (EZTwain) product, the first thing I did was to import their class by adding eztwain.cs to my project as instructed by their guide.
Eztwain.cs' namespace is "Atalasoft.EZTwain". In my class I added "USING Atalasoft.EZTwain".

It failed to compile. The error is "The type or namespace name 'Atalasoft' could not be found (are you missing a using directive or an assembly reference?)".

Here is my class that will be consuming EZTwain:

USING System
USING System.Collections.Generic
USING System.Linq
USING System.Text
USING Atalasoft.EZTwain

BEGIN NAMESPACE QLScan
CLASS Class1
CONSTRUCTOR()
RETURN

public method MethodA() as void
EZTwain.EndMultipageFile()
return

END CLASS
END NAMESPACE


I also tried changing the namespace in eztwain.cs to match my project namespace. In this case the compiler doesn't like "EZTwain.EndMultipageFile()".

In C# I didn't have any issue with using eztwain.cs. I already created a C# Windows form project to test the functionality of EZTwain. I also started a Class Library project in C# and added eztwain.cs. No compilation issue.

Unfortunately, I cannot add reference to their dll in both C# and X# projects. The error I received when attempting to add reference to their dll is:

Could not load file or assembly 'Eztwain4.dll or one of its dependencies. The module was expected to contain an assembly mainifest.

Does eztwain.cs need to be converted to X# in order to use it in X# project?

Thanks,
Boonnam
User avatar
robert
Posts: 4523
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Namespace issue when adding a C# class

Post by robert »

Boonnam,

I think the class is called EZTwain in the Atalasoft namespace.

If you change the using to

Code: Select all

using Atalasoft
it should work.

If that doesn't work, then show us the example C# code.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Namespace issue when adding a C# class

Post by wriedmann »

Hi Boonam,

you cannot compile a .cs file in a X# application.

There are two possibilities: create a library from the C# file and include that in your application (the fastest way), or try to translate the C# to X# using ILSpy and the X# plugin.

The later option may not work or require some manual work, so the first option would be the better one.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Namespace issue when adding a C# class

Post by boonnam »

Robert,

I tried "Using Atalasoft ". I also changed the namespace in eztwain.cs to "Atalasoft". Still have same compile error.

Attached is the screen shot of my simple C# dll project solution explorer. When I received the compile error in X#, I was wondering if I would also get the same error in C#. So this C# dll project was created to help me.
Dll error_1.JPG
In case the attachment isn't clear, there are only two classes in this project. They are class1.cs and eztwain.cs. The references are the standard one that Microsoft inserted with the exception of "System.Drawing", which is needed by eztwain.cs.

Here is my entire code of my class1.cs:

Code: Select all

using Atalasoft.EZTwain;

namespace CSharpQLScan
{
    public class Class1
    {
        public void MethodA()
        {
            EZTwain.EndMultipageFile();
        }
    }
No compile issue. The line "EZTwain.EndMultipageFile();" was there to see if I can access EZTwain properties/methods.

Here are the first few lines from eztwain.cs:

Code: Select all

using System.Runtime.InteropServices;
using System.Text;

namespace Atalasoft.EZTwain
{

    public abstract class EZTwain
    {


        /// <summary>
        /// Gets the EZImage as .Net image object.
        /// </summary>
        /// <returns>.Net image object.</returns>
        public static System.Drawing.Image DIB_ToImage(System.IntPtr hdib)
        {
My intent is to create a dll using X# and use my C# windows form project as a guide since I got it working. Once I create the dll, I plan to create a windows services to consume it using X# as well. Once again, I would use my current C# windows services project as a guide.

Thanks,
Boonnam
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Namespace issue when adding a C# class

Post by boonnam »

Wolfgang,

Are you sure that you can't mix X# and C#? Sometime last year when I was playing with X#, I was able to insert a .cs file to my X# project and reference the C# properties and methods in the .prg file. Base on this experience I decided to import eztwain.cs to my current X# project. If this has changed, then I have to rethink about using X# for this project since I need to use a 3rd party product.

Thanks,
Boonnam
User avatar
Chris
Posts: 4913
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Namespace issue when adding a C# class

Post by Chris »

Hi Boonam,

I suspect the problem is related to having a class name and a part of the namespace being exactly the same ("EZTwain"). Please try renaming the class name to something different, for example "EZTwainClass", does this work this way?

About mixing x# and c#, yes this is of course possible, in separate dlls of the same solution. What is not possible is to include both x# and c# in the same library/dll/exe, that's what Wolfgang was referring to. It's just the term "project" as used by VS that brings this confusion.

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Namespace issue when adding a C# class

Post by wriedmann »

Hi Boonam,

as Chris wrote of course you can mix C# and X# in the same solution, but they need to be different executables, as for every executable (exe or dll) only one compiler can be responsible.

So your C# code should build a dll, that then can be used by your X# application (can be exe or dll) without any problem. In the past, I had several examples for this, but now that X# can rebuild all C# features I needed, I have transformed the C# code to X# code.

With .NET you can mix and match languages, but in different assemblies.
So you can have a class in a C# assembly, then inherit from this class in a X# assembly, and then again use (or inherit from) in a VB.NET application.
There are only two limitations: every assembly must be build by one compiler (X#,C#,VB.NET, ....) because in .NET there is no linker anymore like in older application architectures, and if you would like to use an X# class in an assembly written in another language, the class interface cannot use any X# specific extensions, like Date, codeblock or VO style array.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Namespace issue when adding a C# class

Post by boonnam »

It was indeed two separate projects within one solution. I am now able to compile my X# class library project. Thank you all for your help.

Boonnam
Post Reply