从另一个类调用一个类需要帮助 [英] Need help with calling one class from another

查看:91
本文介绍了从另一个类调用一个类需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有两个类的应用程序,一个类(InventoryInfoClass)

是另一个类(InventoryItem)中的一个对象。我试图从包括中包含类别(InventoryInfo)的

包含类(InventoryInfo)中访问(获取/设置)私有变量时遇到了问题
问题class

(InventoryItem)。


这是代码,减少了。我在开始时已经包括了*********

的第一行'正在炸毁我。我确定其他尝试

来访问invInfo.companyName字段的人也会爆炸。


请帮忙吗?


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


使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System。文字;

使用System.IO;


命名空间InventoryInfo

{

公共类InventoryInfoClass < br $>
{

private string _companyName;

public InventoryInfoClass()

{

companyName =" ;;


}


公共字符串companyName

{

get

{

return _companyName;

}

set

{

_companyName = value;

}

}


}


公共类InventoryItem

{

私人InventoryInfoCla ss invInfo;

私有字符串_itemID;


public InventoryItem()

{

InventoryInfoClass invInfo = new InventoryInfoClass();

_itemID ="" ;;


}


公共字符串itemID

{

get

{

返回_itemID;

}

套装

{

_itemID = value;

}


}

公共字符串companyName

{

get

{

****** ***返回invInfo.companyName;

}


set

{

invInfo.companyName =值;

}

}

public void printInvItem()

{

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

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


Console.WriteLine(按任意键继续);

Console.ReadLine();


}


}

类InvApp

{

static void Main (string [] args)

{

InventoryItem invItem = new InventoryItem();


invItem.printInvItem();


invItem.companyName =" C";


invItem.printInvItem();

}


}


}

解决方案

< blockquote>我觉得每个InventoryItem都有一个InventoryInfoClass,(或者

换句话说,每个InventoryItem对象都有一个公司名称)它

表示InventoryInfoClass可以是一个基类对于所有项目。


所以你应该改变你的模型并从

InventoryInfoClass继承InventoryItem,然后是InventoryInfoClass的所有私人成员

可在InventoryItem中访问。

Ali

7月21日,9:22 * am,ds < nodamnspa ... @ yahoo.comwrote:


我有一个有两个班级的应用程序,一个班级(InventoryInfoClass)

是另一个类(InventoryItem)中的对象。 *我试图访问(获取/设置)来自包括的

包含的类(InventoryInfo)*中的私有变量,我遇到了

的问题。 class

(InventoryItem)。


这是代码,减少了。 *我在开始时已经包含了*********

的第一行'正在炸毁我。 *我确定其他尝试

访问invInfo.companyName字段的人也会爆炸。


请帮忙吗?


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


使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System .Text;

使用System.IO;


命名空间InventoryInfo

{

* * public class InventoryInfoClass

* * {

* * * * private string _companyName;


* * * * public InventoryInfoClass()

* * * * {

* * * * * * companyName ="" ;;


* * * *}


* * * *公共字符串companyName

* * * * {

* * * * * * get

* * * * * * {

* * * * * * * * return _companyName;

* * * * * *}

* * * * * *设置

* * * * * * {

* * * * * * * * _companyName = value;

* * * * * *}

* * * *}


* *}


* *公共类InventoryItem

* * {

* * * *私有InventoryInfoClass invInfo;

* * * *私人字符串_itemID;


* * * * public InventoryItem()

* * * * {

* * * * * * InventoryInfoClass invInfo = new InventoryInfoClass();

* * * * * * _itemID ="" ;;


* * * *}


* * * *公共字符串itemID

* * * * {

* * * * * * get

* * * * * * {

* * * * * * * *返回_itemID;

* * * * * *}

* * * * * *设置

* * * * * * {

* * * * * * * * _itemID = value;

* * * * * *}


* * * *}


* * * * public string companyName

* * * * {

* * * * * *获得

* * * * * * {

** ******* * * *返回invInfo.companyName;

* * * * * *}


* * * * * *设定

* * * * * * {

* * * * * * * * invInfo.companyName = value;

* * * * * *}

* * * *}


* * * * public void printInvItem()

* * * * {


* * * * * * Console.WriteLine(" Company)名称:{0}",this.companyName);

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


* * * * * * Console.WriteLine(按任意键继续);

* * * * * * Console.ReadLine();


* * * *}

* *}


* *类InvApp

* * {

* * * * static void Main(string [] args)

* * * * {

* * * * * * InventoryItem invItem = new InventoryItem();


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


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


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

* * * *}


* *}


}


On Sun,2008年7月20日21:22:38 -0700,ds < no ********** @ yahoo.comwrote:


我有一个带有两个类和一个类的应用程序(InventoryInfoClass) )

是另一个类(InventoryItem)中的一个对象。我试图从包括中包含类别(InventoryInfo)的

包含类(InventoryInfo)中访问(获取/设置)私有变量时遇到了问题
问题class

(InventoryItem)。



首先,您发布的代码应该有效。你说的行不起作用

没有进入私人领域,而是公共财产。我们可以

假设您发布的代码实际上并不是您所拥有的代码吗?
有问题吗?


我承认,从你发布的代码来看,对我来说,为什么你还有

嵌套类并不明显。外部的包含类只是代替嵌套类的

值。为什么不把这些数据放在包含类的

中呢?

也就是说,你看到的问题是关于嵌套类的基本规则: br />
嵌套类可以看到私有包含类的成员,但

不是相反的。也就是说,由于嵌套类实际上是包含类的b $ ba成员,它可以访问所有包含类的所有_或$
成员,包括<的所有私有成员br />
包含类。但是包含类肯定不是嵌套类的成员

,只有嵌套类的成员可以访问

私有成员。


就个人而言,我在这里看不到问题。我不知道你为什么要首先使用

嵌套类,但假设你这样做,为什么不直接使用

公共财产呢?定义它,而不是试图进入

私有领域?无论如何,这是更好的封装。


如果你可以详细说明哪些代码实际上不起作用,为什么它是

你认为你的包含类应该可以访问嵌套类的私人成员

,或许我们可以提供更适合你的目标的不同建议(假设上面没有这样做)。


Pete


ds写道:


我有一个有两个类的应用程序,一个类(InventoryInfoClass)

是另一个类中的一个对象(InventoryItem)。我试图从包括中包含类别(InventoryInfo)的

包含类(InventoryInfo)中访问(获取/设置)私有变量时遇到了问题
问题class

(InventoryItem)。


这是代码,减少了。我在开始时已经包括了*********

的第一行'正在炸毁我。我确定其他尝试

来访问invInfo.companyName字段的人也会爆炸。


请帮忙吗?



8< snip


>

公共类InventoryItem

{

private InventoryInfoClass invInfo ;

私有字符串_itemID;


public InventoryItem()

{

InventoryInfoClass invInfo = new InventoryInfoClass();



这里你声明了一个与成员

变量同名的局部变量。局部变量被赋予新的信息对象,但

成员变量保持为空。


_itemID ="";


}



8< snip


-

G?跑Andersson

_____
http://www.guffa.com


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();
}

}

}


解决方案

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();
* * * * }

* * }

}


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


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


这篇关于从另一个类调用一个类需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆