473,769 Members | 1,637 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
20 1481
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 InventoryInfoCl ass 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:

InventoryInfoCl ass invInfo = new InventoryInfoCl ass();

to:

invInfo = new InventoryInfoCl ass();

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

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
2160
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
2342
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
2174
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
10211
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...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
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
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
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

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.