473,406 Members | 2,710 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,406 software developers and data experts.

can 2 projects share 1 file?

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.
Nov 20 '05 #1
8 1964
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" <wm******@yahoo.com> wrote in message
news:uW*************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #2
* "William Meitzen" <wm******@yahoo.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
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.

"William Meitzen" <wm******@yahoo.com> wrote in message
news:uW*************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #4
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

Nov 20 '05 #5
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. <><

"Fergus Cooney" <fi****@post.com> wrote in message
news:uY**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #6
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


Nov 20 '05 #7
* "William Meitzen" <wm******@yahoo.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
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!
Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: AARON PECORARO | last post by:
I need to split apart my web application into multiple projects to allow it to be distributed in parts, but all of the projects need to work together (ie. they need to share session information)....
1
by: Tim Haughton | last post by:
Hello, I seem to be encountering a problem that many others have encountered before me. Unfortunately, even the mighty Google seems reluctant to yield any solutions. We are developing some...
1
by: Simon Neve | last post by:
Hello, This question is related to sharing .Net projects across solutions and is reposted from the SourceSafe group. We have several different solutions and want to share common assemblies...
2
by: Brad Wood | last post by:
Using VS2003, I can't add an existing class to a project w/o the IDE creating a copy of it. I want multiple projects to be able to share a central copy of a class. I can't compile the shared...
5
by: Tinius | last post by:
I have created a class which I wish to reuse amongst several projects. But each time I Add the class to a new project, it creates a copy of that class in the folder of the new project. This means...
10
by: Conan | last post by:
Hi, I am having a problem with .Net / Visual Studio that I can't find the cause of. I have searched newsgroups and the web and find people with similar but different (I think) problems. Here's...
3
by: Ron L | last post by:
I have an application that I am developing that is a front end for a SQL database. We will also be developing a subset of the UI that will work as a (mainly) standalone client that will make a...
13
by: Paul Cheetham | last post by:
Hi, I have two projects where I want to use the same source file in each project. If I add the file to each project, then a local copy is made. I want to be able to have a single source file...
1
by: Andy | last post by:
When I open a web project from a sandbox, Visual Studio reports: “The project you are trying to open is a Web project. You need to open it by specifying its URL path” The project is checked...
10
by: Scott Townsend | last post by:
So I'm trying to Write a Backend to something and in testing I decided to create a generic front end app that can simulate the passed in Data. So I have 2 projects in my solution, though I'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.