By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

Problem passing in reference of calling class into DLL

P: n/a
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.
Dec 13 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"scottt" <sc****@discussions.microsoft.com> wrote in message
news:00**********************************@microsof t.com...
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.

Dec 13 '05 #2

P: n/a
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.
"scottt" <sc****@discussions.microsoft.com> wrote in message
news:00**********************************@microsof t.com...
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.


Dec 13 '05 #3

P: n/a
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.

"Nicholas Paldino [.NET/C# MVP]" wrote:
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.
"scottt" <sc****@discussions.microsoft.com> wrote in message
news:00**********************************@microsof t.com...
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.


Dec 13 '05 #4

P: n/a
scottt <sc****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 13 '05 #5

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"scottt" <sc****@discussions.microsoft.com> wrote in message
news:0D**********************************@microsof t.com...
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.

"Nicholas Paldino [.NET/C# MVP]" wrote:
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.
"scottt" <sc****@discussions.microsoft.com> wrote in message
news:00**********************************@microsof t.com...
> 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.


Dec 13 '05 #6

P: n/a
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,

"Jon Skeet [C# MVP]" wrote:
scottt <sc****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 13 '05 #7

P: n/a
scottt <sc****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 13 '05 #8

P: n/a
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,

"Jon Skeet [C# MVP]" wrote:
scottt <sc****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 13 '05 #9

P: n/a
scottt <sc****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 13 '05 #10

P: n/a
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.
"Jon Skeet [C# MVP]" wrote:
scottt <sc****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 14 '05 #11

P: n/a
scottt <sc****@discussions.microsoft.com> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 14 '05 #12

P: n/a


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.

"Jon Skeet [C# MVP]" wrote:
scottt <sc****@discussions.microsoft.com> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.