2

IDE: VS 2010, C# .net,

I have two win projects in 1 solution,

ProjA, and ProjB

now ProjA contains classA.cs

namespace ProjA
{
    class ClassA
    {
        public static int aValue = 5;
    }
}

same way ProjB contains ClassB.cs

namespace ProjB
{
    public class ClassB
    {
        public static int bValue = 10;  
    }
}

and here is FormA.cs

using System;
using System.Windows.Forms;
using ProjB;

namespace ProjA
{
    public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        private void FormA_Load(object sender, EventArgs e)
        {
            int va = ProjB.ClassB.bValue;; //Here getting error.???
        }
    }
}

Error : Cannot resolve symbol ProjB

Hint: This Problem Is related to namespace, I am trying to access ClassB which is in ProjB from FormA which is in ProjA, Here ProjA and ProjB are the 2 winforms project in Same solution

---xxxx----------- THis problem has been solved.

but now I want to access ClassA.cs in FormB.cs (just reverse of above problem),

when I tried same way ProjB(RighClick) -> Add reference -> ProjName(Tab) ProjA(Click)

The new problem I am facing is its saying unable to add it will create circular dependency, Please suggest How to solve this issue.

I want to access ClassA.cs in ProjB->FormB.cs here FormB is in ProjB

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
yogeshkmrsoni
  • 315
  • 7
  • 21

5 Answers5

0

ProjB is the namespace, so it doesn't know which class to use. If they're in the same solution(package of projects), just use the classname ClassB.(Or ProjB.ClassB.bValue)

If your projects aren't in the same Solution, go to Project A, File->Add->Existing Project, and add ProjB.

0

You need to create new instance of class or make it static. So this:

 namespace ProjB
{
public static class ClassB
{
  public static int bValue = 10;  
}

and then

int va = ClassB.bValue; 

OR

int va = new ClassB().bValue; 
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
0

Provided you have referenced correctly ProjB you just need to instatinate classB in ProjA:

using System;
    using System.Windows.Forms;
    using ProjB;

    namespace ProjA
    {
         ProjB.ClassB classb=new ProjB.ClassB();
        public FormA()
        {
            InitializeComponent();
            ....
           classb.bValue=....//set desired valued
        }

        private void FormA_Load(object sender, EventArgs e)
        {

            int va =classb.bValue;
        }
apomene
  • 14,282
  • 9
  • 46
  • 72
0

The problem was with reference,

I added reference

Right click ProjA -> References -> Add Reference, then I added ProjB.

And Problem resolved. :-)

yogeshkmrsoni
  • 315
  • 7
  • 21
0

In your visual studio

1-) Right click to your solution

2-) Then open Properties of the solution..

In the properties page, on the left you'll see menus..

3-) Click to Project Dependencies

In Details of project Dependencies (right side of the page )

4-) Select your Project A from the DropDownList which has a label with "Project"

5-) Under the DropDownList Check (Mark as checked) the ProjectB

6-) Click to save / ok

This operation what you did, warns the compiler as so :

"When you start to build ProjectA, Before build/compile ProjectA First build/Compile ProjectB and then continue to build ProjectA"

or in another saying " Dear Compiler ! When you work with my ProjectA, You will need My ProjectB to do your work properly. So please also check my ProjectB.

Best Regards, Your Lovely Developer "

NOTE : Never circular reference (ProjectA depends to ProjectB and ProjectB depends to ProjectA) to eachother..this gets infinitive recursion error of the compiling your code..!

Visual Representation of The VS2010-ProjectDependencies Box

sihirbazzz
  • 708
  • 4
  • 20