-2

In Visual Studio 2013 I am trying to add a namespace for a cs file that is a common file with the project (Added using Add Existing Item).

For example, I added new.cs to the same project as old.cs

Is there a way to call functions from new.cs class from old.cs without referencing a DLL for new.cs?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Blah Blah
  • 49
  • 1
  • 8
  • 2
    Sure, just call them... if they're in the same project, you don't need a separate DLL. You might need a `using` directive, but that's a different matter. We can't really tell what's wrong as you haven't shown us any code or errors... – Jon Skeet Jun 16 '17 at 18:40
  • 2
    Yes. Just instantiate classes, etc. Add a `using` for the namespace if needed. If you don't show us the code you're trying to use, we can't give you any specifics. If you *do* choose to share the code, please add it 1) as *text*, not a screenshot, 2) in your question, not in a comment or a link to tumblr or something. Indent the code by *four spaces* so it will format correctly. You can select the code and press Ctrl+K to indent it. – 15ee8f99-57ff-4f92-890c-b56153 Jun 16 '17 at 18:40
  • @EdPlunkett I thought the same thing but hasn't been working for me. Unfortunately due to confidentiality issues I can't show my code on here. With that in mind any other ideas? – Blah Blah Jun 16 '17 at 18:43
  • 3
    "Hasn't been working for me" is utterly meaningless. You did something wrong. I can't possibly guess the specifics, since you refuse even to give us a hit about *what* you tried to do, or *how* it didn't work. Learn C#. That's the answer, if the only question you can give us is "how do I write code". – 15ee8f99-57ff-4f92-890c-b56153 Jun 16 '17 at 18:45
  • 2
    If you can't show the actual code then create a [mcve] – UnholySheep Jun 16 '17 at 18:54
  • @BlahBlah It doesn't matter what file the code is in. Doesn't make the slightest difference. Show us what you're trying to do. Change the names of the namespace and classes and methods if you have to. Omit all unnecessary code. I know, that would require effort, but it is *not possible to answer your question* unless you tell us what you're asking. People very, very experienced in C#, and in helping new programmers, are telling you that they can't help you without that information. Consider listening. Are you really claiming that the *error message* is classified information? – 15ee8f99-57ff-4f92-890c-b56153 Jun 16 '17 at 18:56

1 Answers1

0

Some options

Sharing a common file between projects

The Visual Studio environment isn't really optimized for this sort of code sharing.

If you attempt to use "Add Existing Item" with a .cs file that is outside the current project's code tree, it'll happily add it, but it will copy the file to your root. You might be able to get around it by manually editing your .csproj file, but I wouldn't recommend it.

You could maybe set up your folder structure so that your two projects have folders in common, e.g. by putting the .csproj files in the same folder, or by using a symbolic link, Unix-style. Seems to me this could get very confusing, and people may accidentally delete or alter files that they didn't intend to (we used to do something like this at my work and there were a lot of "oh crap!" moments).

Either way, your source code revision history will look like a total mess, and it'll be hard to determine what changes affect which project. Your QA team may hate you for this since it'll be harder to assess risk level for any changeset.

Cut and paste

Of course you can always cut and paste code from one project to another. This is a bulletproof method and it's very clear what to expect. But you'd end up maintaining two versions of the code.

Keep separate project, but combine into one .exe

You could keep your common code in a separate DLL project for your coders to work with. When you build the project, you can use a third party tool like Fody to inject the DLL into the master executable so you end up with only one file. See this question for more information.

John Wu
  • 50,556
  • 8
  • 44
  • 80