473,756 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with calling one class from another

I've got an app with two classes, and one class (InventoryInfoC lass)
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.company Name field will blow up too.

Any help please?

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

using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.IO;

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

}

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

}

public class InventoryItem
{
private InventoryInfoCl ass invInfo;
private string _itemID;

public InventoryItem()
{
InventoryInfoCl ass invInfo = new InventoryInfoCl ass();
_itemID = "";

}

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

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

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

Console.WriteLi ne("Company Name: {0}", this.companyNam e);
Console.WriteLi ne("Item ID: {0}", this.itemID);

Console.WriteLi ne("Press any key to continue");
Console.ReadLin e();

}

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

invItem.printIn vItem();

invItem.company Name = "C";

invItem.printIn vItem();
}

}

}


Jul 21 '08 #1
20 1478
I am feeling that every InventoryItem have an InventoryInfoCl ass, (or
in other words, every InventoryItem object have a company name) its
means InventoryInfoCl ass can be a base class for all items.

So you should change your model and inherit InventoryItem from
InventoryInfoCl ass, then all the private members of InventoryInfoCl ass
will be accessible in InventoryItem.
Ali
On Jul 21, 9:22*am, "d.s." <nodamnspa...@y ahoo.comwrote:
I've got an app with two classes, and one class (InventoryInfoC lass)
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.company Name field will blow up too.

Any help please?

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

using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InventoryInfo
{
* * public class InventoryInfoCl ass
* * {
* * * * private string _companyName;

* * * * public InventoryInfoCl ass()
* * * * {
* * * * * * companyName = "";

* * * * }

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

* * }

* * public class InventoryItem
* * {
* * * * private InventoryInfoCl ass invInfo;
* * * * private string _itemID;

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

* * * * }

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

* * * * }

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

* * * * * * set
* * * * * * {
* * * * * * * * invInfo.company Name = value;
* * * * * * }
* * * * }

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

* * * * * * Console.WriteLi ne("Company Name: {0}", this.companyNam e);
* * * * * * Console.WriteLi ne("Item ID: {0}", this.itemID);

* * * * * * Console.WriteLi ne("Press any key to continue");
* * * * * * Console.ReadLin e();

* * * * }

* * }

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

* * * * * * invItem.printIn vItem();

* * * * * * invItem.company Name = "C";

* * * * * * invItem.printIn vItem();
* * * * }

* * }

}
Jul 21 '08 #2
On Sun, 20 Jul 2008 21:22:38 -0700, d.s. <no**********@y ahoo.comwrote:
I've got an app with two classes, and one class (InventoryInfoC lass)
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
d.s. wrote:
I've got an app with two classes, and one class (InventoryInfoC lass)
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.company Name field will blow up too.

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

public InventoryItem()
{
InventoryInfoCl ass invInfo = new InventoryInfoCl ass();
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
On Jul 20, 11:49*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Sun, 20 Jul 2008 21:22:38 -0700, d.s. <nodamnspa...@y ahoo.comwrote:
I've got an app with two classes, and one class (InventoryInfoC lass)
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.company Name;
>
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.company Name;

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
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
On Jul 21, 3:01 am, Göran Andersson <gu...@guffa.co mwrote:
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
On Mon, 21 Jul 2008 02:27:14 -0700, d.s. <no**********@y ahoo.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
On Jul 21, 3:01*am, Göran Andersson <gu...@guffa.co mwrote:
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
On Jul 21, 8:38*am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Mon, 21 Jul 2008 02:27:14 -0700, d.s. <nodamnspa...@y ahoo.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
1666
by: jfj | last post by:
I don't understand. We can take a function and attach it to an object, and then call it as an instance method as long as it has at least one argument: ############# class A: pass def foo(x): print x
2
2158
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
9
2370
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that and get
0
3940
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
18
2340
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before. Here's the scenario: We have a base class with all virtual functions. We'll call this the Animal class. We then make two classes Fish and Bird that both inherit from Animal. In the program, we have a single array of Animal pointers that will...
2
1885
by: rn5a | last post by:
This function in a VB class file takes UserID as a parameter & returns a SqlDataReader to the calling function which exists in a ASPX page: Namespace NConnect Public Class Cart Private sqlConn As New SqlConnection(".....") Public Function GetAddress(ByVal UserID As Integer) As SqlDataReader Dim sqlCmd As SqlCommand Dim sqlReader As SqlDataReader
15
2171
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this is because the compiler needs to see the implemenation of A when allocating memory for a_obj. ...
5
2031
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I want to run the executable in separate threads because they can be long-running and it would be optimal for us to run a bunch simultaneously. I've got that part working - it's pretty easy in C#. What I'm having a hard time with is managing the...
0
1806
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or overridden by the type. The reason is because the CLR can just call these methods nonvirtually and System.ValueType overrides all of these virtual methods and expects the value in the this argument to refer to an unboxed value type instance. Remember, a...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.