Newbie: Missing something obvious about instance references

T

Tony Johansson

Hi

I've been searching for a solution to my problem all day now but
haven't found anything which makes me think the solution is something
obvious. Any help is appreciated.

Suppose you have three classes in separate files and two namespaces.

using Pain.Common;

namespace Test
{
public class Resource : Pain.Common.AnyCollection
{
public Resource()
{
AnyCollection testCol = new AnyCollection();
}
}
}

using Pain.Common;

namespace Test
{
public class FrmMain: System.Windows.Forms.Form
{
// Stuff
testCol.xyxyxyx // <-- This is my problem
}
}


namespace Pain
{
namespace Common
{
public class AnyCollection: CollectionBase
{
public AnyCollection()
{
}
public object this[int index]
{
get
{
return (object)List[index];
}
set
{
List[index] = value;
}
}
public int Add(object obj)
{
return List.Add(obj);
}
}
}
}

As indicated by the code I'm able to create a collection in the
Resource class but I'm not able to use it in the FrmMain class (or any
other class). It is like the system is not aware of it.

Shouldn't I be able to reference class instances from another class or
class instance if they are using the same namespace and the classes
are public?

What am I doing wrong, missing here?

/TJ
 
N

nevin

Why not make your testCol a public member of the Resource class so you would
change the Resource class to be:

public class Resource : Pain.Common.AnyCollection
{
Public AnyCollection testCol
//do your get and set functions here (or not bother seeing as it's
public)

public Resource()
{
testCol = new AnyCollection();
}
}
 
T

Tony Johansson

Hi

Yes, that solves the problem of referencing from that specific class.
But it does not solve it if I want to reference it from other classes.
I can declare Public AnyCollection testCol there as well but it does
not point to an instance... can I make it point to the same instance?
Waht would be the best way of doing this?

/TJ
 
J

Jeff Louie

Tony...I am not sure I fully understand the problem. If you have an
instance
outside of the class in the same assembly, you can pass a reference by
value
to the class constructor (containment by reference). If you have a
utility class
in a different assembly, compile it to a dll library and you can call it
from
another assembly.

Regards,
Jeff
Yes, that solves the problem of referencing from that specific class.
But it does not solve it if I want to reference it from other classes.<
 
T

Tony Johansson

Hi,

Since I have used VB .Net before and I'm now starting to learn C#
there are some differences that I can't figure out.

What I want to know is...

In VB .Net you could use a method from a class instance from another
class if you declared them as public. In C# this isn't enough, what is
the C# way of doing this?

/TJ
 
P

Paul E Collins

Tony Johansson said:
In VB .Net you could use a method from a class
instance from another class if you declared them
as public. In C# this isn't enough, what is the C#
way of doing this?

Read up on the 'static' modifier.

A public static method can be called from another class simply by qualifying
it with the class name. A public non-static method requires an object that
is an instance of the class.

P.
 
J

Jeff Louie

Tony...

from Chapter 2: One difficult design decision is
to decide if a class should inherit from a parent class or hold a
reference to a
new object. Inheritance represents an IS_A relationship from a
generalization
to a specialization. Containment represents a HAS_A relationship between
the
whole and a part. So a car IS_A motorized vehicle, but HAS_A radio. The
two
relationships can be expressed in code thusly:

class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
Radio r= new Radio();
}

An instance of Car contains an instance of a Radio so that the lifetimes
of the
radio and car are intertwined. This is "containment by ownership" making
Car
an example of a composite class. 

Containment can also be accomplished using references to external
objects.
"Containment by reference" can be expressed in code thusly:

class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
private Radio r;
public Car(Radio r) {
...
this.r= r;
}
}


You can then create an instance of Car like this:


class Class1
{
[STAThread]
static void Main(string[] args)
{
Radio r= new Radio();
Car c= new Car(r);
}
}

Regards,
Jeff
In VB .Net you could use a method from a class instance from another
class if you declared them as public. In C# this isn't enough, what is
the C# way of doing this?<
 
T

Tony Johansson

Thank you Jeff, exactly what I was looking for... this makes sense.
This is fundamental but didn't find any good description of it.

/TJ
 

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