can 2 projects share 1 file?

  • Thread starter Thread starter William Meitzen
  • Start date Start date
W

William Meitzen

Does someone know how I can get two projects to share a common class?
Something like this:

solution WholeShmeer
project1
code1
CommonClass
project2
code2
CommonClass

.... where CommonClass is a class I created and resides in the same file?
I'm writing a rather large class, and it seems a waste to keep duplicate
code.
 
Why don't you just write it in one and reference it from the other class?
You can specify the build order so I think this will get you anything that
you'd want.
 
* "William Meitzen said:
Does someone know how I can get two projects to share a common class?
Something like this:

solution WholeShmeer
project1
code1
CommonClass
project2
code2
CommonClass

... where CommonClass is a class I created and resides in the same file?
I'm writing a rather large class, and it seems a waste to keep duplicate
code.

Just add the file, but in the file selection dialog you click on the
little dropdown of the "Add" button. There you can choose to
_reference_ the file instead of creating a copy of it.
 
I don't think I explained myself very well. I'm using VB.Net 2002, and I
have a project that currently looks something like this:

Solution WholeShmeer
References
Client (project 1 of 2)
Client.vb (this file has code and a class called clsConnection)
References
Server (project 2 of 2)
Server.vb (this file has code and a class called clsConnection)

I'd like to have clsConnection in one file, and have both Client and Server
use that same code. Rather like C's version of a #include "filename.c"
statement.

I've tried creating an additional project (right-click the Solution, click
Add / New project / Visual basic projects / Class library) and setting
dependencies (right-click the Solution, click Project dependencies /
Dependencies tab / Choose "Server" / check CommonClass, choose "Client" /
check CommonClass), but I still get errors when I compile ("Type
'clsUserInfo' is not defined).

I've also tried adding the file which contains CommonClass to both the
Client/References list and Server/References list, but they seem to point to
..dll files instead of .vb files.
 
Hi William,

A quick question. What was wrong with William R's suggestion?

But if clsConnection has no logical reason to be with one rather than the
other (and the name suggests this) then the Third Project idea is the way to
go.

So you do what you did before and again you get the clsUserInfo error. So
the quetion is - where does clsUserInfo live? And you're going to tell me that
it's in Client (I guess).

Now that's a problem because you can't have projClient using projConn and
vice versa. One of them must be independant. So you're going to have a rethink
about how you separate these things.

Do that first and then tell us what your object dependencies are. What you
need to achieve is a dependancy graph (projectwise) that flows in one
direction only.

Regards,
Fergus
 
I must express my appreciation at everyone's politeness.

Nothing is wrong with William R.'s suggestion. It's only that I've been
using VS for about 3 months, and while I'd love to define a class in in one
file and instantiate it in the same as well as in another project's file, I
don't know how to yet (step-by-step). I am familiar with a host of other
languages, so I think it will only be a matter of getting my explanation up
to speed, and understanding how VB's scoping rules work.

What I'd like to do is let two projects instatiate from the same class.

I'm writing two projects. One will allow one machine, the Client, to
initiate a connection with another machine, the Server. The Client will ask
the Server for some updates (similar to FTP), then close the connection. In
writing these projects, I am writing a class called clsCommunication, which
houses some commands for establishing, maintaining, and closing down a
connection via TCP. It is currently about 200 lines long, and identical in
Client.vb and Server.vb. I'm learning all about TCPClient, TCPListener,
threads, and events, and while it's taxing my brain, I'm having great fun
doing so. (Plug: I got a lot of help from Sybex's _Visual Basic .NET
Developer's Handbook_.) My Server application listens for and accepts
connections, and my Client application initiates connections. (Side note:
If you've written code using TCPClient and TCPListener, then you'll know
that they must interact together. If not, please take it on faith that I
made the transition from having a lot of very awkward code for both Server
and Client, to seeing the advantages of having Server and Client create
instantiations of clsCommunication.) Every time I improved (pronounced,
"found bugs in") clsCommunication, I had to copy and paste 200 lines of
nearly identical code. Soooooo, now I want my Server project and Client
project to see the same file which houses clsCommunication, or to get
clsCommunication's scope within Server and Project's view.

I did not explain where clsUserInfo before. clsUserInfo is a class with
only 2 lines of code, and I was using it to test the suggestions Ryan and
Wagner offered. I wanted to first get my problem resolved with clsUserInfo,
and then I will get it working with clsCommunication.

I am operating under the impression that I Client must depend on the file
which contains clsCommunication, and Server must also depend on the file
which contains clsCommunication. If I need to achieve a dependency graph
that flows however in one direction only, then my impression must be wrong.

Here is my entire Solution as it stands right now:

Solution Mercury
--project Client
----References
----AssemblyInfo.vb
----Client.vb
----net12.ico
--project Common
----References
----AssemblyInfo.vb
----Class1.vb
--project Server
----References
----AssemblyInfo.vb
----Server.vb
--project Setup
----Detected Dependencies
----Primary Output from Client (Active)

As of right now, I have no object dependencies.

Sincerely,
William Meitzen. <><
 
Hi William,

The thing about the direction of dependencies was that you can have Client
and Server dependant on clsCommunication but not the other way as

The image would be
Client -----------------\____clsCommunication
Server----------------/

Which is fine.
Perhaps you this in mind
Client ---------------- clsCommunication --------------Server

Which looks two-way but is still fine as long as the lines are one-way.

But never mind all that. What I was concerned about was whether
clsCommunication needs a clsUserInfo in Client (or Server). If it's two lines
for debuggin/testing, it can go easily enough.

So, if clsCommunication needs nothing of those other two it should be able
to go straight into its own project, (although from the list you showed, maybe
there's a home for it in Common). Then just do Add Reference for the Client
and Server projects and give each a reference to the project with
clsCommunication in it.

It sounds like you are very close. Give a shout if there's anything else,
but it probably won't be me answering as I'm off to bed.

G'night.

Regards,
Fergus
 
* "William Meitzen said:
I don't think I explained myself very well. I'm using VB.Net 2002, and I
have a project that currently looks something like this:

Solution WholeShmeer
References
Client (project 1 of 2)
Client.vb (this file has code and a class called clsConnection)
References
Server (project 2 of 2)
Server.vb (this file has code and a class called clsConnection)

I'd like to have clsConnection in one file, and have both Client and Server
use that same code. Rather like C's version of a #include "filename.c"
statement.

I've tried creating an additional project (right-click the Solution, click
Add / New project / Visual basic projects / Class library) and setting
dependencies (right-click the Solution, click Project dependencies /
Dependencies tab / Choose "Server" / check CommonClass, choose "Client" /
check CommonClass), but I still get errors when I compile ("Type
'clsUserInfo' is not defined).

I've also tried adding the file which contains CommonClass to both the
Client/References list and Server/References list, but they seem to point to
.dll files instead of .vb files.

You can add the files shared between the 2 projects to a class library
project. The other project reference the class library in order to use
the objects defined in it. You can set a reference by selecting the
project's "References" folder in the solution explorer and select "Add
Reference..." from the context menu. After adding the reference, you
may want to import the namespace of the class library in order to use
the classes of the class library directly.
 
I think I figured it out.

1. Add a new project, call it, say, Common.
2. Name the .vb file, say CommonCode.vb
3. Enter the class code you want other projects to see into CommonCode.vb.
Perform the following steps to each of your other projects:
4. Right-click "References," choose "Add Reference."
5. Click the "Projects" tab.
6. Highlight the project name, "Common," then click "Select."
7. You'll see "Common" appear in the Selected Components area below. Click
OK.
8. Do either of the following:
8a. Add the line "Imports Common" near the top.
Option Explicit on
Imports Common
Public Class Form1
[Windows Form Designer generated code]
dim insClassInCommon as new clsNameOfClass
end class
8b. Instantiate your class by prefixing it with the class' name in
Common.vb:
Option Explicit on
Public Class Form1
[Windows Form Designer generated code]
dim insClassInCommon as new common.clsNameOfClass
end class

Even though I figured it out, it would have taken me much longer without
your help. Thanks!
 
Back
Top