473,230 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

Problem passing in reference of calling class into DLL

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
12 2989
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
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
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
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
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
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
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
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
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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
9
by: SB | last post by:
Ok, very simple problem. I'm trying to update a value by calling a function using pass by reference, but it does not update the value. In short, the value I'm trying to update is balance, which is...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
4
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
4
by: Immortal_Nephi | last post by:
You have written several global functions inside header code. Then, you create either static library or dynamic linked library. All global functions can be reuseable to the main() function when...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.