3 Replies - 447 Views - Last Post: 13 May 2016 - 03:05 PM Rate Topic: -----

#1 DrDress  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Import class from different project

Posted 13 May 2016 - 04:07 AM

I just imported some classes from project1 to project2 using the Solution Explorer->Add->Existing Item....

I'm no expert, I only read that one could do this and tried.

The class now apears in the Solution Explorer to the right, and I can open and edit the text. But I cant make objects of the classes in my main Form1. It's like the class isn't included somehow.

Also I'm not sure if I just made a copy of the file and added to my project2 or am I accessing the same file located in project1. That has implications if I edit stuff in the class via either of the project.

Is This A Good Question/Topic? 0
  • +

Replies To: Import class from different project

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,899
  • Joined: 12-June 08

Re: Import class from different project

Posted 13 May 2016 - 06:26 AM

Typically you would have your other classes compile as a .DLL.. then 'ADD REFERENCE' to that dll.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Import class from different project

Posted 13 May 2016 - 06:57 AM

So, a solution is just a logical collection of projects. Projects are just logical collections of files and resources that can be "built" to produce output.

Adding items to a solution does not implicitly "connect" them or cause them to reference each other. It just means that you can see both of them in the same solution.

If you want to actually call into one project's code from another, you would need to use the "Add Reference" dialog. You can get that by right-clicking the "References" node below the project in the Solution Explorer and selecting "Add Reference".

This dialog will look different depending on which version of VS you are using. But the gist is the same. From here, you can reference .NET DLLs, COM Components, and other projects in your Solution.

Adding a reference creates a dependency. It makes it so that one project needs another to run. So now, when you compile your solution and check in the Bin folder, you'll see the assembly from the referenced project along with the assembly built from the project itself.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,375
  • Joined: 02-June 10

Re: Import class from different project

Posted 13 May 2016 - 03:05 PM

My guess is the imported classes aren't in the same namespace.

For example: If the files were from "ProjectOne" that is probably their namespace. You dropped them into "ProjectTwo". Well, ProjectTwo doesn't know anything about ProjectOne files.
Change the namespace, or add a reference.

All of this points to a bigger issue: You're just playing "guess and by golly" rather than following some guided lesson plan.
Pick up a good "Learn C# in 30 days" book and WORK it, cover to cover. Its not supposed to be a reference book like a dictionary that you only look at when you get stuck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1