fbpx

Namespace issue when adding a C# class

More
5 months 4 days ago #1 by Boonnam S
Namespace issue when adding a C# class was created by Boonnam S
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

Please Log in or Create an account to join the conversation.

More
5 months 4 days ago #2 by Robert van der Hulst
Replied by Robert van der Hulst on topic Namespace issue when adding a C# class
Boonnam,

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

If you change the using to
using Atalasoft
it should work.

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

Robert

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
5 months 4 days ago #3 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Namespace issue when adding a C# class
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

Please Log in or Create an account to join the conversation.

More
5 months 4 days ago #4 by Boonnam S
Replied by Boonnam S on topic Namespace issue when adding a C# class
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.

Attachment not found



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:
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:
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

Please Log in or Create an account to join the conversation.

More
5 months 4 days ago #5 by Boonnam S
Replied by Boonnam S on topic Namespace issue when adding a C# class
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

Please Log in or Create an account to join the conversation.

More
5 months 4 days ago #6 by Chris Pyrgas
Replied by Chris Pyrgas on topic Namespace issue when adding a C# class
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

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

More
5 months 4 days ago #7 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Namespace issue when adding a C# class
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

Please Log in or Create an account to join the conversation.

More
5 months 2 days ago #8 by Boonnam S
Replied by Boonnam S on topic Namespace issue when adding a C# class
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

Please Log in or Create an account to join the conversation.