Problem with namespace and class

X

Xarky

Hi,

I have the following in the project, with the following desgin.


// class 1, which is a windows form
namespace Name
{
public class aaaa
{
public int number;
.....
}
}

// class 2, which is a code file
namespace Name
{
public class bbbb
{
.....
}
}

What I am trying to do is to access the object number from class bbbb,
but it is giving me an error. Also the object was tried with
protection level as public, protected and private.
Error given: The type or namespace name 'Name' could not be found
(are you missing a using directive or an assembly reference?)


Should that work? If no, how can I share an object between the two
classes.


Thanks in Advance
 
G

Guest

Hi,

Your object number in your class bbbb is using what type of access modifier?

Thanks.
 
X

Xarky

Chua Wen Ching said:
Hi,

Your object number in your class bbbb is using what type of access modifier?

Thanks.

What I am doing in class bbbb, is setting the value of number=0;
 
G

Guest

Hi Xarky,

I assume:

1) I think it is under the same project

2) There are 2 class files

3) To access number in class bbbb, you need to make sure that class aaaa had
a constructor

namespace Name
{
public class aaaa
{
public aaaa() {}

public int number;
}
}

4) When you want to call the number in bbbb, this is how you do it (please
make sure it is within a method or constructor, not globally)

// correct
namespace Name
{
public class bbbb
{
public static void Main()
{
aaaa _a = new aaaa();
_a.number = 0;

// print _a.number
}
}
}

// you can't do this!
namespace Name
{
public class bbbb
{
aaaa _a = new aaaa();
_a.number = 0;
}
}

Hope it helps :)
 
X

Xarky

This should be a better view of what I have.

namespace Project
{
public class Test
{
public int shared;
static void Main()
{
...
}
}
}

namespace Project
{
public class Work
{
public Work()
{
// What i was trying to do is accessing the object shared
// from here. But as it could be seen its not possible.
// Problem has now been solved by passing a copy of the
// instance shared, and having a private object within the
// class Work.
}
}
}


********* new solution as i did it now *********

namespace Project
{
public class Test
{
private int shared;
static void Main()
{
...
Work x = new Work (shared);
...
}
}
}

namespace Project
{
public class Work
{
private int personalShared;
public Work(int x)
{
this.personalShared = x;
}
}
}

Sorry for any inconvenience created.
 

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