Class library

D

Dave

hi,
I wrote class library that contain a struct stX and a new class library that
get the first class library as referance and implement a function foo that
one of its parametrs is of type of the struct stX;

I wrote an application(exe) that get the first class library as a referance
and have a member of type stX and have a referance of the second class
library and caled to its function foo.

The problem is when i pass the exe member of type stX to the class library
the compiler thinks it a diffrent type of the type the function foo needs
why???

Can i share types between dll and exe in diffrent way???


Thanks
 
P

Peter Bradley

Works for me:

Put this in a Class Library project called StXLib:

using System;
using System.Collections.Generic;
using System.Text;

namespace StXLib
{
public struct StX
{
public int i;
public double d;
}
}

Put this in a Class Library project called DoFooLib:

using System;
using System.Collections.Generic;
using System.Text;

namespace DoFooLib
{
public class DoFoo
{
public double foo(StXLib.StX stx) {
return stx.d * stx.i;
}
}
}

Put this in a Console project called RefTest:

using System;
using System.Collections.Generic;
using System.Text;

namespace RefTest
{
class RefTestExe
{
static void Main(string[] args)
{
StXLib.StX stx = new StXLib.StX();
DoFooLib.DoFoo df = new DoFooLib.DoFoo();
stx.d = 11.1;
stx.i = 3;
double d = df.foo(stx);
Console.WriteLine(d.ToString());
Console.WriteLine("Press a Enter to continue ...");
Console.ReadLine();
}
}
}

Build it and run it. It should be fine.

Peter
 
P

Peter Bradley

Ooops! Forgot to say about the references:

The StXLib project has no additional references over and above the standard
ones.

The DoFooLib references StXLib.

The RefTest project references DoFooLib and StXLib.

Apologies.



Peter

Peter Bradley said:
Works for me:

Put this in a Class Library project called StXLib:

using System;
using System.Collections.Generic;
using System.Text;

namespace StXLib
{
public struct StX
{
public int i;
public double d;
}
}

Put this in a Class Library project called DoFooLib:

using System;
using System.Collections.Generic;
using System.Text;

namespace DoFooLib
{
public class DoFoo
{
public double foo(StXLib.StX stx) {
return stx.d * stx.i;
}
}
}

Put this in a Console project called RefTest:

using System;
using System.Collections.Generic;
using System.Text;

namespace RefTest
{
class RefTestExe
{
static void Main(string[] args)
{
StXLib.StX stx = new StXLib.StX();
DoFooLib.DoFoo df = new DoFooLib.DoFoo();
stx.d = 11.1;
stx.i = 3;
double d = df.foo(stx);
Console.WriteLine(d.ToString());
Console.WriteLine("Press a Enter to continue ...");
Console.ReadLine();
}
}
}

Build it and run it. It should be fine.

Peter


Dave said:
hi,
I wrote class library that contain a struct stX and a new class library
that get the first class library as referance and implement a function
foo that one of its parametrs is of type of the struct stX;

I wrote an application(exe) that get the first class library as a
referance and have a member of type stX and have a referance of the
second class library and caled to its function foo.

The problem is when i pass the exe member of type stX to the class
library the compiler thinks it a diffrent type of the type the function
foo needs why???

Can i share types between dll and exe in diffrent way???


Thanks
 
P

PS

Dave said:
hi,
I wrote class library that contain a struct stX and a new class library
that get the first class library as referance and implement a function foo
that one of its parametrs is of type of the struct stX;

I wrote an application(exe) that get the first class library as a
referance and have a member of type stX and have a referance of the second
class library and caled to its function foo.

The problem is when i pass the exe member of type stX to the class library
the compiler thinks it a diffrent type of the type the function foo needs
why???

Can i share types between dll and exe in diffrent way???

Perhaps you have declared stX in the exe and in the dll so they are in
different namespaces and are therefore considered to be different types.

PS
 
P

Peter Bradley

*Declaring* it in different namespaces shouldn't be a problem (see my reply
earlier in the thread). *Defining* it in more than one place (regardless of
namespace) would be a problem, but I can't see how the OP could have done
that. I've come across it in remoting code where soaputil is used to
generate a proxy: unless you use the -gc option, this will provide a
definition of every class used by the file, within the generated dll and so
will cause the problem described. We aren't talking about anything as
esoteric as that, though, I don't think.

Something else that can be a problem would be if the assembly containing the
struct had been given a strong name, but the version had not been fixed in
the AssemblyInfo.cs file. This can mean that two references that look
identical are actually pointing to different versions if there's been a
recompile between setting the references. This would give the symptoms
described.

But unless and until the OP replies, we're just guessing.


Peter
 

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