By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

Need help with calling one class from another

P: n/a
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) from the "including" class
(InventoryItem).

Here's the code, trimmed down. I've included ********* at the start
of the first line that's blowing up on me. I'm sure others that try
to access the invInfo.companyName field will blow up too.

Any help please?

------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InventoryInfo
{
public class InventoryInfoClass
{
private string _companyName;
public InventoryInfoClass()
{
companyName = "";

}

public string companyName
{
get
{
return _companyName;
}
set
{
_companyName = value;
}
}

}

public class InventoryItem
{
private InventoryInfoClass invInfo;
private string _itemID;

public InventoryItem()
{
InventoryInfoClass invInfo = new InventoryInfoClass();
_itemID = "";

}

public string itemID
{
get
{
return _itemID;
}
set
{
_itemID = value;
}

}
public string companyName
{
get
{
********* return invInfo.companyName;
}

set
{
invInfo.companyName = value;
}
}
public void printInvItem()
{

Console.WriteLine("Company Name: {0}", this.companyName);
Console.WriteLine("Item ID: {0}", this.itemID);

Console.WriteLine("Press any key to continue");
Console.ReadLine();

}

}
class InvApp
{
static void Main(string[] args)
{
InventoryItem invItem = new InventoryItem();

invItem.printInvItem();

invItem.companyName = "C";

invItem.printInvItem();
}

}

}


Jul 21 '08 #1
Share this Question
Share on Google+
20 Replies


P: n/a
I am feeling that every InventoryItem have an InventoryInfoClass, (or
in other words, every InventoryItem object have a company name) its
means InventoryInfoClass can be a base class for all items.

So you should change your model and inherit InventoryItem from
InventoryInfoClass, then all the private members of InventoryInfoClass
will be accessible in InventoryItem.
Ali
On Jul 21, 9:22*am, "d.s." <nodamnspa...@yahoo.comwrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). *I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) *from the "including" class
(InventoryItem).

Here's the code, trimmed down. *I've included ********* at the start
of the first line that's blowing up on me. *I'm sure others that try
to access the invInfo.companyName field will blow up too.

Any help please?

------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InventoryInfo
{
* * public class InventoryInfoClass
* * {
* * * * private string _companyName;

* * * * public InventoryInfoClass()
* * * * {
* * * * * * companyName = "";

* * * * }

* * * * public string companyName
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return _companyName;
* * * * * * }
* * * * * * set
* * * * * * {
* * * * * * * * _companyName = value;
* * * * * * }
* * * * }

* * }

* * public class InventoryItem
* * {
* * * * private InventoryInfoClass invInfo;
* * * * private string _itemID;

* * * * public InventoryItem()
* * * * {
* * * * * * InventoryInfoClass invInfo = new InventoryInfoClass();
* * * * * * _itemID = "";

* * * * }

* * * * public string itemID
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return _itemID;
* * * * * * }
* * * * * * set
* * * * * * {
* * * * * * * * _itemID = value;
* * * * * * }

* * * * }

* * * * public string companyName
* * * * {
* * * * * * get
* * * * * * {
********* * * * return invInfo.companyName;
* * * * * * }

* * * * * * set
* * * * * * {
* * * * * * * * invInfo.companyName = value;
* * * * * * }
* * * * }

* * * * public void printInvItem()
* * * * {

* * * * * * Console.WriteLine("Company Name: {0}", this.companyName);
* * * * * * Console.WriteLine("Item ID: {0}", this.itemID);

* * * * * * Console.WriteLine("Press any key to continue");
* * * * * * Console.ReadLine();

* * * * }

* * }

* * class InvApp
* * {
* * * * static void Main(string[] args)
* * * * {
* * * * * * InventoryItem invItem = new InventoryItem();

* * * * * * invItem.printInvItem();

* * * * * * invItem.companyName = "C";

* * * * * * invItem.printInvItem();
* * * * }

* * }

}
Jul 21 '08 #2

P: n/a
On Sun, 20 Jul 2008 21:22:38 -0700, d.s. <no**********@yahoo.comwrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) from the "including" class
(InventoryItem).
First, the code you posted should work. The line you say doesn't work
isn't accessing the private field, but rather a public property. Can we
assume that the code you posted isn't in fact the code that you're having
trouble with?

I admit, from the code you posted, it's not obvious to me why you have the
nested class anyway. The outer, containing class is just proxying the
value to the nested class anyway. Why not just put that data in the
containing class?

That said, the issue you're seeing is a basic rule about nested classes:
the nested class can see "private" members of the containing class, but
not the other way around. That is, since the nested class is effectively
a member the containing class, it has access to everything _any_ member of
the containing class would, including all private members of the
containing class. But the containing class is most certainly not a member
of the nested class, and only the members of the nested class can access
private members.

Personally, I don't see the problem here. I don't know why you want the
nested class in the first place, but assuming you do, why not just use the
public property as you've defined it, rather than trying to get at the
private field? That's a better encapsulation anyway.

If you can elaborate on what code is actually not working, and why it is
you think your containing class should have access to private members of
the nested class, perhaps we can offer different advice more suitable to
your goals (assuming the above doesn't do it).

Pete
Jul 21 '08 #3

P: n/a
d.s. wrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) from the "including" class
(InventoryItem).

Here's the code, trimmed down. I've included ********* at the start
of the first line that's blowing up on me. I'm sure others that try
to access the invInfo.companyName field will blow up too.

Any help please?
8< snip
>
public class InventoryItem
{
private InventoryInfoClass invInfo;
private string _itemID;

public InventoryItem()
{
InventoryInfoClass invInfo = new InventoryInfoClass();
Here you have declared a local variable with the same name as the member
variable. The local variable gets assigned the new info object, but the
member variable remains null.
_itemID = "";

}
8< snip

--
Göran Andersson
_____
http://www.guffa.com
Jul 21 '08 #4

P: n/a
On Jul 20, 11:49*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 20 Jul 2008 21:22:38 -0700, d.s. <nodamnspa...@yahoo.comwrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). *I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) *from the "including" class
(InventoryItem).

First, the code you posted should work. *The line you say doesn't work *
isn't accessing the private field, but rather a public property. *Can we *
assume that the code you posted isn't in fact the code that you're having*
trouble with?

I agree it should work, but it is bombin on me. It is the code, but
it's trimmed way down, getting rid of other variables and their
accessors.
>
I admit, from the code you posted, it's not obvious to me why you have the *
nested class anyway. *The outer, containing class is just proxying the *
value to the nested class anyway. *Why not just put that data in the *
containing class?
Ths was a homework assignment. It has already been submitted, so
you're not doing my work for me. But I need to understand what went
wrong because other assignments will build on this. The teacher seems
to be having a bit of a problem responding to requests for help
(online class).

So, the assignment was to do it as done here. The next assignment was
to inherit the InventoryInfo class, which worked with no problems.
That said, the issue you're seeing is a basic rule about nested classes: *
the nested class can see "private" members of the containing class, but *
not the other way around. *That is, since the nested class is effectively *
a member the containing class, it has access to everything _any_ member of *
the containing class would, including all private members of the *
containing class. *But the containing class is most certainly not a member *
of the nested class, and only the members of the nested class can access *
private members.
But it should be able to use the accessors to gain access to the
private variables. Perhaps my terminology is wrong. I'm trying to
use the accessor, and it's blowing up:

********* return invInfo.companyName;
>
Personally, I don't see the problem here. *I don't know why you want the *
nested class in the first place, but assuming you do, why not just use the *
public property as you've defined it, rather than trying to get at the *
private field? *That's a better encapsulation anyway.
I am trying to use the accessor to gain access to the private field.

********* return invInfo.companyName;

but it doesn't like it.
>
If you can elaborate on what code is actually not working, and why it is *
you think your containing class should have access to private members of *
the nested class, perhaps we can offer different advice more suitable to *
your goals (assuming the above doesn't do it).
The line I put the asterisks on is the first line that bombs in my
compiler.

If the contained class is to access the elements of the container
class, this means I will need to change the contained class everytime
I want to include it into a different container class that has
different elements.
Jul 21 '08 #5

P: n/a
d.s. wrote:
>First, the code you posted should work. The line you say doesn't work
isn't accessing the private field, but rather a public property. Can we
assume that the code you posted isn't in fact the code that you're having
trouble with?


I agree it should work, but it is bombin on me.
No, it should not work. Se my other post in the thread.
>If you can elaborate on what code is actually not working, and why it is
you think your containing class should have access to private members of
the nested class, perhaps we can offer different advice more suitable to
your goals (assuming the above doesn't do it).

The line I put the asterisks on is the first line that bombs in my
compiler.
Are you completely sure about that? It compiles just fine for me, but I
get a runtime exception on exactly that line because the invInfo member
variable is null.

--
Göran Andersson
_____
http://www.guffa.com
Jul 21 '08 #6

P: n/a
On Jul 21, 3:01 am, Göran Andersson <gu...@guffa.comwrote:
d.s. wrote:
First, the code you posted should work. The line you say doesn't work
isn't accessing the private field, but rather a public property. Can we
assume that the code you posted isn't in fact the code that you're having
trouble with?
I agree it should work, but it is bombin on me.

No, it should not work. Se my other post in the thread.
If you can elaborate on what code is actually not working, and why it is
you think your containing class should have access to private members of
the nested class, perhaps we can offer different advice more suitable to
your goals (assuming the above doesn't do it).
The line I put the asterisks on is the first line that bombs in my
compiler.

Are you completely sure about that? It compiles just fine for me, but I
get a runtime exception on exactly that line because the invInfo member
variable is null.

--
Göran Andersson
_____http://www.guffa.com
I'll check that out. The error message I got back was not very
helpful. Yes, it did compile for me. It was a runtime error.
Thanks. I'll check it.
Jul 21 '08 #7

P: n/a
On Mon, 21 Jul 2008 02:27:14 -0700, d.s. <no**********@yahoo.comwrote:
I agree it should work, but it is bombin on me. It is the code, but
it's trimmed way down, getting rid of other variables and their
accessors. [...]
Ah, I see where I misread. Your use of the word "including" misdirected
me and I incorrectly read the "Info" class as being nested.

Also, next time you post a question, please avoid vague terms like "blow
up" and "bombin [sic] on me". It doesn't help that one post claims it was
a compiler error, but more broadly you simply need to post a precise
description of the error.

I apologize for any confusion...as is sometimes the case, trying to answer
questions very late at night caused me to overlook pretty much all of the
important parts of the code, relying too much on your description of the
problem.

Anyway, Göran has the answer for you. You should read his post.

Pete
Jul 21 '08 #8

P: n/a
On Jul 21, 3:01*am, Göran Andersson <gu...@guffa.comwrote:
d.s. wrote:
First, the code you posted should work. *The line you say doesn't work *
isn't accessing the private field, but rather a public property. *Can we *
assume that the code you posted isn't in fact the code that you're having *
trouble with?
I agree it should work, but it is bombin on me.

No, it should not work. Se my other post in the thread.
If you can elaborate on what code is actually not working, and why it is *
you think your containing class should have access to private members of *
the nested class, perhaps we can offer different advice more suitable to *
your goals (assuming the above doesn't do it).
The line I put the asterisks on is the first line that bombs in my
compiler.

Are you completely sure about that? It compiles just fine for me, but I
get a runtime exception on exactly that line because the invInfo member
variable is null.

--
Göran Andersson
_____http://www.guffa.com
I set the invInfo member variable to non-null. And not it blows up on
the set accessor when I try to change it. Also, when I hover over
invInfo on that line, it shows NULL.
Jul 21 '08 #9

P: n/a
On Jul 21, 8:38*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 21 Jul 2008 02:27:14 -0700, d.s. <nodamnspa...@yahoo.comwrote:
I agree it should work, but it is bombin on me. *It is the code, but
it's trimmed way down, getting rid of other variables and their
accessors. *[...]

Ah, I see where I misread. *Your use of the word "including" misdirected *
me and I incorrectly read the "Info" class as being nested.

Also, next time you post a question, please avoid vague terms like "blow *
up" and "bombin [sic] on me". *It doesn't help that one post claims it was *
a compiler error, but more broadly you simply need to post a precise *
description of the error.
Sorry. I was at home, without the code in front of me, and was just
posting from memory.
>
I apologize for any confusion...as is sometimes the case, trying to answer *
questions very late at night caused me to overlook pretty much all of the*
important parts of the code, relying too much on your description of the *
problem.
You also have a novice over here, to c# anyway.
Jul 21 '08 #10

P: n/a
On Mon, 21 Jul 2008 08:55:32 -0700, d.s. <no**********@yahoo.comwrote:
[...]
You also have a novice over here, to c# anyway.
Being a novice doesn't prevent you from being precise about your errors.
I see that you have once again described your error in an imprecise way.
I'll once again recommend that you describe it precisely.

And of course, since you've changed the code, you should post actual
_code_ that illustrates your change. We can make assumptions about what
you changed, but there are lots of ways to do it wrong and only one way to
do it right. If it's not working, undoubtedly you didn't do it right,
which leaves a great number of possible alternatives from which we have to
guess about.

Pete
Jul 21 '08 #11

P: n/a
On Jul 21, 8:38*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 21 Jul 2008 02:27:14 -0700, d.s. <nodamnspa...@yahoo.comwrote:
I agree it should work, but it is bombin on me. *It is the code, but
it's trimmed way down, getting rid of other variables and their
accessors. *[...]

Ah, I see where I misread. *Your use of the word "including" misdirected *
me and I incorrectly read the "Info" class as being nested.

Also, next time you post a question, please avoid vague terms like "blow *
up" and "bombin [sic] on me". *It doesn't help that one post claims it was *
a compiler error, but more broadly you simply need to post a precise *
description of the error.
Ok, I just compiled and ran the code here at work.

It gives me a run-time error on that line with a message of "Object
reference not set to an instance of an object." If I hover over
invInfo, it shows NULL.

So, within that accessor, it can't see the instance of invInfo that I
created in the constructor.

I tried changing the scope (or whatever you call it) of invInfo from
private to protected. Same problem. I set the companyName to "test"
in the constructor. Same problem.

I'm using VS 2008 on this. It's a console app.

I'm going to reread Goran's post, to make sure I got it all.
Jul 21 '08 #12

P: n/a
On Jul 21, 12:42*am, Göran Andersson <gu...@guffa.comwrote:
d.s. wrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). *I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) *from the "including" class
(InventoryItem).
Here's the code, trimmed down. *I've included ********* at the start
of the first line that's blowing up on me. *I'm sure others that try
to access the invInfo.companyName field will blow up too.
Any help please?

8< snip
* * public class InventoryItem
* * {
* * * * private InventoryInfoClass invInfo;
* * * * private string _itemID;
* * * * public InventoryItem()
* * * * {
* * * * * * InventoryInfoClass invInfo = new InventoryInfoClass();

Here you have declared a local variable with the same name as the member
variable. The local variable gets assigned the new info object, but the
member variable remains null.
Ok, I'm a little vague on this. Not that you're vague. I'm just not
sure how to set this up. I thought by doing the following, I was
setting up a local variable:
public class InventoryItem
{
>>>>public InventoryInfoClass invInfo;
private string _itemID;

Jul 21 '08 #13

P: n/a
On Jul 21, 9:15*am, "d.s." <nodamnspa...@yahoo.comwrote:
On Jul 21, 12:42*am, Göran Andersson <gu...@guffa.comwrote:


d.s. wrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). *I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) *from the "including" class
(InventoryItem).
Here's the code, trimmed down. *I've included ********* at the start
of the first line that's blowing up on me. *I'm sure others that try
to access the invInfo.companyName field will blow up too.
Any help please?
8< snip
* * public class InventoryItem
* * {
* * * * private InventoryInfoClass invInfo;
* * * * private string _itemID;
* * * * public InventoryItem()
* * * * {
* * * * * * InventoryInfoClass invInfo = new InventoryInfoClass();
Here you have declared a local variable with the same name as the member
variable. The local variable gets assigned the new info object, but the
member variable remains null.

Ok, I'm a little vague on this. *Not that you're vague. *I'm just not
sure how to set this up. *I thought by doing the following, I was
setting up a local variable:

*public class InventoryItem
* * {>>>>public InventoryInfoClass invInfo;

* * * * private string _itemID;- Hide quoted text -

- Show quoted text -
I'm stepping through this code, and, in the constructor, I can see the
instance of invInfo populated. When I leave the constructor, invInfo
is null. I thought the constructor creates a persisting instance.
Jul 21 '08 #14

P: n/a
On Mon, 21 Jul 2008 09:11:55 -0700, d.s. <no**********@yahoo.comwrote:
Ok, I just compiled and ran the code here at work.

It gives me a run-time error on that line with a message of "Object
reference not set to an instance of an object." If I hover over
invInfo, it shows NULL.
Then it's still null (not "NULL"...that's a Windows constant).

But you still didn't post the code you changed. You claimed to have fixed
the assignment to a local variable in the constructor, but based on your
most recent description I'd say you almost certainly didn't.
So, within that accessor, it can't see the instance of invInfo that I
created in the constructor.
Well, as Göran pointed out, you never assigned the instance you created in
the constructor to that field. You assigned it to a local variable only,
so the instance just gets thrown away when the constructor returns to the
caller.
I tried changing the scope (or whatever you call it)
"Accessibility". "Scope" is something else.
of invInfo from private to protected. Same problem.
Of course. Accessibility is a compile-time issue. You're getting a
run-time error. Changing accessibility isn't going to affect that.
I set the companyName to "test"
in the constructor. Same problem.
Of course. You're still changing the property on an instance that isn't
being saved to your field.

I believe that if you simply change "InventoryInfoClass invInfo = new
InventoryInfoClass();" in your constructor to read "invInfo = new
InventoryInfoClass();", that will fix your problem. There are better ways
to write the code IMHO, but that's the smallest change that should address
the run-time error you're reporting.

Pete
Jul 21 '08 #15

P: n/a
On Mon, 21 Jul 2008 09:27:30 -0700, d.s. <no**********@yahoo.comwrote:
I'm stepping through this code, and, in the constructor, I can see the
instance of invInfo populated. When I leave the constructor, invInfo
is null. I thought the constructor creates a persisting instance.
The constructor is just another method. It doesn't have any magic to
cause an instance to persist unless you copy the reference to that
instance somewhere that will make it persist.
Jul 21 '08 #16

P: n/a
On Jul 21, 9:33*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 21 Jul 2008 09:27:30 -0700, d.s. <nodamnspa...@yahoo.comwrote:
I'm stepping through this code, and, in the constructor, I can see the
instance of invInfo populated. *When I leave the constructor, invInfo
is null. *I thought the constructor creates a persisting instance.

The constructor is just another method. *It doesn't have any magic to *
cause an instance to persist unless you copy the reference to that *
instance somewhere that will make it persist.
That worked. Thanks Peter, and Goran, and all. I thought I would
have needed to cast or define that variable before I assigned an
object to it.

I'm not new to programming, but I am new to conceptualizing all the
ins and outs of scoping and instantiation in object oriented
programming. It doesn't always stick very well in the wetware.

Thanks again.
Jul 21 '08 #17

P: n/a
On Mon, 21 Jul 2008 09:48:12 -0700, d.s. <no**********@yahoo.comwrote:
That worked. Thanks Peter, and Goran, and all. I thought I would
have needed to cast or define that variable before I assigned an
object to it.
Casting, no. Define a variable, yes. But you already did, as the
instance field from which you try to retrieve it later. The local
variable was superfluous.

Glad it worked out. :)
Jul 21 '08 #18

P: n/a
On Jul 21, 9:54*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 21 Jul 2008 09:48:12 -0700, d.s. <nodamnspa...@yahoo.comwrote:
That worked. *Thanks Peter, and Goran, and all. *I thought I would
have needed to cast or define that variable before I assigned an
object to it.

Casting, no. *Define a variable, yes. *But you already did, as the *
instance field from which you try to retrieve it later. *The local *
variable was superfluous.

Glad it worked out. *:)
Ok, I think I got it. I inadvertently created a separate instance of
invInfo within the constructor, which only had scope within the
constructor, rather than referencing the class variable.

Is that it?

Thanks.

Jul 21 '08 #19

P: n/a
On Mon, 21 Jul 2008 10:59:56 -0700, d.s. <no**********@yahoo.comwrote:
Ok, I think I got it. I inadvertently created a separate instance of
invInfo within the constructor, which only had scope within the
constructor, rather than referencing the class variable.

Is that it?
Almost. You didn't create "a separate instance", you created "a separate
_variable_".

You only ever create one instance; the instance itself isn't the problem.
It's where you copy the reference of the instance to after you create it
that was the issue.

Other than that, yes...your statement is an accurate description of the
mistake.

Pete
Jul 21 '08 #20

P: n/a
d.s. wrote:
Ok, I'm a little vague on this. Not that you're vague. I'm just not
sure how to set this up. I thought by doing the following, I was
setting up a local variable:
public class InventoryItem
{
>>>>public InventoryInfoClass invInfo;
private string _itemID;
No, that's a member variable. It's scope is the class.

A local variable is a variable declared inside a method, and it has the
scope of that method.

You dceclared a local variable in the constructor with the same name as
the member variable. You created an instance of the class (using the new
keyword), and assigned it's reference to the local variable.

When the constructor code ends, the local variable goes away, and with
it the reference to the instance that you created.

As you want to access the member variable, you should not create the
local variable at all.

Change this:

InventoryInfoClass invInfo = new InventoryInfoClass();

to:

invInfo = new InventoryInfoClass();

--
Göran Andersson
_____
http://www.guffa.com
Jul 21 '08 #21

This discussion thread is closed

Replies have been disabled for this discussion.