How to share a .cs

A

ad

I have write a clss, say DM.cs, and I save it in a independent directory
like c:\MyUtil

I have a new project in c:\proj1.

When I use VS to add the existed c:\MyUtil\DM.cs to the new project, the VS
will copy DM.cs form c:\MyUtil
to c:\proj1.

How can I left the DM.cs in the independent directory ?
 
O

Olorin

make a class library containing only DM.cs.
Then, from your application, and any other app that needs to use it,
add a reference to it.

HTH,
F.O.R.
 
T

Tim Wilson

When you add the code file to the project make sure that you specify it as a
"link file". So in the "Add Existing Item" dialog, after you select the
DM.cs file, select the drop down arrow on the "Open" button and then select
"Link File". The file will now appear in the Solution Explorer, under the
appropriate project, with a code file icon that looks like it has a little
shortcut image over top of it.
 
S

SP

ad said:
I have write a clss, say DM.cs, and I save it in a independent directory
like c:\MyUtil

I have a new project in c:\proj1.

When I use VS to add the existed c:\MyUtil\DM.cs to the new project, the
VS
will copy DM.cs form c:\MyUtil
to c:\proj1.

How can I left the DM.cs in the independent directory ?

Add / Existing Item - navigate to DM.cs. Do not click on the Open button but
use the dropdown and choose Link.

SP
 
A

ad

But when I click the drop down arrow on the "Open" button, there is no "link
file" appear.
If I right click the DM.cs, there a link item I can select, but the file is
alwayse copy to the c:\Proj1

Can I modified the RelPath = "DM.cs" in the .csproj file?
How to modify it?
 
T

Tim Wilson

After you select the appropriate cs file in the list, when you click on the
drop down arrow on the "Open" button you should see a small context menu
appear with the options "Open", "Open With...", and "Link File". If you
don't see the "Link File" option, here are a few questions.

Are you creating a web application, by chance? I have read that this option
is not available through web projects due to it being viewed as a security
issue.

If you are not creating a web app, are you using VS.Net 2002 or 2003? It's
possible, although I can't confirm this because I no longer have 2002
installed, that this feature is not in the 2002 product.
 
S

SB

It's not on the normal File->Open dropdown menu. You must go to File->Add
Existing Item (or hit CTRL-Shift-A) in VS.Net 2003. You can also get there
by highlighting the project or solution in the Solution Explorer and
right-clicking...it will be under the "Add" submenu. Once you get the "Add
Existing Item" window open, you should clearly see a dropdown arrow that's
on the Open button which has a menuitem titled "Link File" (VS.Net 2003)

If you're using C# Express, the context menu appears to be the only way to
get to this.

HTH,
-sb
 
A

ad

Thank a lot!
My project is a web application, so I can't find the "link file" item.
But if I open a windows applicaiton, the "link file" appear.
The " link file" can't be used in web application is so inconvenient.
 
T

Tim Wilson

The " link file" can't be used in web application is so inconvenient.
Agreed. I don't know exactly why this feature was left out of web projects,
other than what I stated before, but it's definitely a nice feature to have.
Your other option in this case is to just compile the shared functionality
into an assembly, class library, and reference this assembly in all projects
as needed.
 
B

Bruce Wood

May I ask a question... just for my own education? I'm a WinForms
developer, so I haven't done any ASP work yet.

Why would you want to do this: share a .cs file between multiple dlls /
exes? Isn't the "correct" solution to build the functionality into a
dll and reference the dll from two projects?

Obviously, there's a scenario that calls for "Link File", or the
developers wouldn't have put it in Visual Studio. I just don't know
what it could be. Can someone enlighten me?
 
T

Tim Wilson

One answer that I can give on this, from a compact framework perspective, is
that you can share code between desktop projects and device projects easily
with this option and conditional compilation constants. Technically you can
build a class library against the compact framework and it will be
retargeted against the desktop framework on demand, but if you're attempting
to build a single source file that understands different targets than this
option can be useful. But I guess it's all up to the dev to decide. Possibly
MS put this in there just to allow end devs to decide for themselves how
they want to share functionality - assembly-wise, or source-wise.
 
J

Jon Skeet [C# MVP]

Bruce Wood said:
May I ask a question... just for my own education? I'm a WinForms
developer, so I haven't done any ASP work yet.

Why would you want to do this: share a .cs file between multiple dlls /
exes? Isn't the "correct" solution to build the functionality into a
dll and reference the dll from two projects?

Obviously, there's a scenario that calls for "Link File", or the
developers wouldn't have put it in Visual Studio. I just don't know
what it could be. Can someone enlighten me?

Sure - it's someone wanting to use a single .cs file and being too lazy
to put it in a library. I personally don't think it's something
Microsoft should pander to, but there we go...
 
A

ad

Thank again.
I think I must take your option.

I have a single file, say MD.cs. How can I complie it to assembly or class
library?
 
T

Tim Wilson

Create new C# "Class Library" project, remove the "Class1.cs" file, add your
cs file, set the output location/name through the project properties, set
any assembly specific information (company, description, version) through
the "AssemblyInfo.cs" file, and then compile.
 
B

Bruce Wood

Well, Tim gave an example of a valid use: a .cs file that contains
conditional compilation instructions, that must be compiled differently
for two different environments. Putting it in a DLL once will, of
course, not have the desired effect. A little scary from my point of
view, but then I don't program for CF yet.
 
A

ad

Thank alot!

Tim Wilson said:
Create new C# "Class Library" project, remove the "Class1.cs" file, add your
cs file, set the output location/name through the project properties, set
any assembly specific information (company, description, version) through
the "AssemblyInfo.cs" file, and then compile.
 
J

Jon Skeet [C# MVP]

Bruce Wood said:
Well, Tim gave an example of a valid use: a .cs file that contains
conditional compilation instructions, that must be compiled differently
for two different environments. Putting it in a DLL once will, of
course, not have the desired effect. A little scary from my point of
view, but then I don't program for CF yet.

Putting it in a DLL *will* have the desired effect - you just need to
build the DLL twice, once for each environment. Personally I think
that's a nicer solution than sharing a source file between two
different projects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top