Problem passing in reference of calling class into DLL

G

Guest

hi,

I am having a little problem passing in reference of my calling class (in my
..exe)into a DLL. Both programs are C# and what I am trying to do is pass a
reference to my one class into a DLL function. When I try and compile the DLL
I get "The type or namespace name "MyForm" could not be found.

I think I have to reference the class but since the DLL needs to be built
before the EXE it looks like I have a chicken and egg type problem. Is it
even possible to do this?

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

You could define your type in a DLL that is separate from the DLL as
well as from the EXE, then reference it from both the EXE and the dll. That
would solve your problem.

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

You could define your type in a DLL that is separate from the DLL as
well as from the EXE, then reference it from both the EXE and the dll. That
would solve your problem.

Hope this helps.
 
G

Guest

Thanks that is kind of my last resort option since I am also battling with a
"X is defined in mutiple places" warning message too. The code worked when it
was all under one name space but since I have pulled out this section of code
to be a DLL I have been running into the defined in multiple places all over
the code.

Are there any other options?

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

No, not really, and honestly, if you want to use the same type in
multiple places, this is the way to go about doing it. Replicating code to
do it is not a good implementation strategy.
 
J

Jon Skeet [C# MVP]

scottt said:
Thanks that is kind of my last resort option since I am also battling with a
"X is defined in mutiple places" warning message too. The code worked when it
was all under one name space but since I have pulled out this section of code
to be a DLL I have been running into the defined in multiple places all over
the code.

Are there any other options?

Aside from Nick's suggestions (which are entirely correct) it's worth
being clear about the difference between namespaces and assemblies - it
wasn't moving things out of a single namespace that caused you
problems, it was moving things into different assemblies. While
*sometimes* there is a one-to-one relationship between assemblies and
namespaces, they're very different concepts. One assembly can define
types in multiple namespaces, and many assemblies can contribute types
to the same namespace.
 
G

Guest

Hi,

I think I should give a little more information about the problem I am having.
First I understand what you and Nicholas are saying about being defined in
multiple places. I have pull out my abstract classes so they are in one DLL
and will reference them in the other DLLs where necessary. I try and explain
the problem better this time.

What I have is the following:

I have a DLL that implements the abstract class "XA" in one DLL. One of the
functions receives a reference to the class calling into the DLL "ref
ClassName className". Basically passing in "this"


Now in the class that calls into the DLL I create the instance of the DLL
class and assign it to a variable of type abstract class.

So how can I compile the abstract class that uses the calling class as one
of its parameters? If I compile the abstract class DLL it does not "know"
about the class of the parameter being passed in and if I complile the class
first it does not have the abstract class DLL to reference.


Thanks,
 
J

Jon Skeet [C# MVP]

scottt said:
I think I should give a little more information about the problem I am having.
First I understand what you and Nicholas are saying about being defined in
multiple places. I have pull out my abstract classes so they are in one DLL
and will reference them in the other DLLs where necessary. I try and explain
the problem better this time.

What I have is the following:

I have a DLL that implements the abstract class "XA" in one DLL. One of the
functions receives a reference to the class calling into the DLL "ref
ClassName className". Basically passing in "this"

Do you really need to pass it *by* reference? Note that you can't pass
"this" by reference anyway.
Now in the class that calls into the DLL I create the instance of the DLL
class and assign it to a variable of type abstract class.

So how can I compile the abstract class that uses the calling class as one
of its parameters? If I compile the abstract class DLL it does not "know"
about the class of the parameter being passed in and if I complile the class
first it does not have the abstract class DLL to reference.

I would suggest making the parameter an interface type instead. Using
interfaces is often a solution for this kind of problem.
 
G

Guest

1) Yes I found out you cannot pass "this" by reference. Not a big deal either
passing by reference or not since the class I am passing in is the class that
controls everything.


2) I debated on using an interface vs. abstract class. The reason I went
with abstract is that you can have multiple abstracts vs. only one interface.

my abstract classes are defined by services and defines the function calls
For example I may have a LongDistance abstract class and a LocalDistance
abstract class. In the main program I look at the user's preferences to
determine which DLL to create (for this example I might have MCI and Sprint
as DLLs) but the vairable I assign it to is of the type abstract class so can
"generically" call the functions.

if pref == MCI
abstract class x = new MCI DLL
else
abstract class x = new Sprint DLL


Since the service provider may provide both local and long distance I don't
think I could use Interfaces. Correct?


I have a simple sample program if you think that would help.


Thanks,
 
J

Jon Skeet [C# MVP]

scottt said:
1) Yes I found out you cannot pass "this" by reference. Not a big deal either
passing by reference or not since the class I am passing in is the class that
controls everything.

I would argue that if you were trying to pass "this" by reference, you
may not fully understand how parameters are passed in C#:
http://www.pobox.com/~skeet/csharp/parameters.html
2) I debated on using an interface vs. abstract class. The reason I went
with abstract is that you can have multiple abstracts vs. only one interface.

No, it's the other way round. A class can implement multiple
interfaces, but only derive from one class.
my abstract classes are defined by services and defines the function calls
For example I may have a LongDistance abstract class and a LocalDistance
abstract class. In the main program I look at the user's preferences to
determine which DLL to create (for this example I might have MCI and Sprint
as DLLs) but the vairable I assign it to is of the type abstract class so can
"generically" call the functions.

if pref == MCI
abstract class x = new MCI DLL
else
abstract class x = new Sprint DLL


Since the service provider may provide both local and long distance I don't
think I could use Interfaces. Correct?

Nope, I'm afraid you've got it the wrong way round.
I have a simple sample program if you think that would help.

Yes, that would help.
 
G

Guest

In my test solution I have 2 projects

First project is my DLL

using System;

namespace CDLL
{
public class MyDLL
{

Form1 f1;
public MyDLL(ref Form1 f)
{
f1 = f;
}
}
}


Simple enogh but notice that I am trying to pass in the class calling it. In
this case it is Form1. (BTW I can use the ref or not use the ref I still have
the same problem).

I won't include the full code for the second part since its a default form
so here is the may parts that I have changed:


namespace CTest {

public Form1()
{
InitializeComponent();

CTest.Form1 f1 = this;
myDLL = new MyDLL(f1);
}
}



So the DLL cannot be created because it does not know about Form1 and the
executable does not know about MyDLL because I can't compile it to reference
the DLL.


What am I doing wrong?


Thanks.
 
J

Jon Skeet [C# MVP]

What am I doing wrong?

If you create an interface in your class library that has everything
that MyDLL needs, you can then make that the type of the parameter. You
can then make Form1 implement that interface, and pass the form to the
MyDLL constructor.
 
G

Guest

Thanks Jon. Everything finally clicked in my head this morning and I
understand.
Just finished changing my code around to use interfaces and all works well.

Thanks Again.
 

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