473,756 Members | 3,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1984
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******** *****@TK2MSFTNG P10.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******** *****@TK2MSFTNG P10.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 clsCommunicatio n, 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 clsCommunicatio n.) Every time I improved (pronounced,
"found bugs in") clsCommunicatio n, 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 clsCommunicatio n, or to get
clsCommunicatio n'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 clsCommunicatio n.

I am operating under the impression that I Client must depend on the file
which contains clsCommunicatio n, and Server must also depend on the file
which contains clsCommunicatio n. 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.co m> wrote in message
news:uY******** ******@TK2MSFTN GP12.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 clsCommunicatio n but not the other way as

The image would be
Client -----------------\____clsCommuni cation
Server----------------/

Which is fine.
Perhaps you this in mind
Client ---------------- clsCommunicatio n --------------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
clsCommunicatio n needs a clsUserInfo in Client (or Server). If it's two lines
for debuggin/testing, it can go easily enough.

So, if clsCommunicatio n 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
clsCommunicatio n 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 insClassInCommo n 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 insClassInCommo n as new common.clsNameO fClass
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
8411
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). Does anyone have any suggestions or solutions to this problem? Thanks, Richard Bowman
1
1310
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 webservices. We have multiple developers who have Visual Studio 2003 and SourceSafe 6.0d. None of our developers have IIS installed on their machines. We have a central test server that we want to work off which runs IIS and .NET 1.1. All of our...
1
1950
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 across them. Is it best to share the project and its files to each solution (each solution has it's own shared project), or have each solution point to the same file location of the project?
2
2717
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 class into a library because one of the executables that uses the class needs to be a self contained executable that will be downloaded and immediately executed on a PDA. I can't GAC the shared library so that both applications can use it because...
5
13515
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 that when I make changes to that class, I have to apply the same changes to all the projects. Is there a setting that I have missed such that I can share one class amongst several projects? -- Tinius
10
1790
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 the issue and some of what I've found out - I really hope it's familiar to someone and there's an answer! I have a solution with about a dozen projects, including 2 asp.net web applications and 1 asp.net web service project. Having migrated to...
3
3302
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 connection to the database, copy the relevant data to a local file, and disconnect from the database. The users will then be able to make modifications using the UI, and when a connection is available they can connect and upload their changes. ...
13
3338
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 used by both projects so that I only have to change one file. I am using compiler directives within the file, and so I can't just build it into a DLL and do it that way, I really do need to make both
1
1141
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 out from a repository into a sandbox. I don’t want the developer to have to setup IIS on their local machine to enable the editing of the web project – I want to configure the web project so that it only references files in the developer’s...
10
1188
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 finding that I've created classes, structs, enums, constants that are going to be neede by both. I don't want to have one project reference the other as they are not 'related' I want some 3rd project or something that I put all the stuff in. Then...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.