我正在进入C#,我遇到了这个问题:
namespace MyDataLayer
{
namespace Section1
{
public class MyClass
{
public class MyItem
{
public static string Property1{ get; set; }
}
public static MyItem GetItem()
{
MyItem theItem = new MyItem();
theItem.Property1 = "MyValue";
return theItem;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在UserControl上有这个代码:
using MyDataLayer.Section1;
public class MyClass
{
protected void MyMethod
{
MyClass.MyItem oItem = new MyClass.MyItem();
oItem = MyClass.GetItem();
someLiteral.Text = oItem.Property1;
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,除非我去访问Property1.智能感知只给我" ,和"作为选项.当我将鼠标移到上面时,Visual Studio给出了这样的解释:EqualsGetHashCodeGetTypeToStringoItem.Property1
MemberMyDataLayer.Section1.MyClass.MyItem.Property1.getcannot be …
是否可以访问子类中的父成员...
class MainClass {
class A { Whatever }
class B {
List<A> SubSetList;
public void AddNewItem(A NewItem) {
Check MasterListHere ????
}
}
List<A> MasterList;
}
Run Code Online (Sandbox Code Playgroud)
所以...我的主要课程将有一个主列表.它也会有一堆B的实例.在B的每个实例中,我想向特定的B添加新的A,但只有它们存在于主列表中.我玩弄MasterList是静态的,它可以工作......直到我有多个MainClass实例......我将拥有它.
我可以将对MasterList的引用传递给B的每个实例,但我最终将拥有多个这些"MasterLists",如果我不需要,我不想要传递大量引用.
我有一个"米"级."米"的一个属性是另一个叫做"生产"的类.我需要通过参考从生产类访问米级(额定功率)的属性.在仪表实例化时不知道powerRating.
我怎样才能做到这一点 ?
提前致谢
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production();
}
}
Run Code Online (Sandbox Code Playgroud) 我有三个这样的课程.
class A
{
public class innerB
{
//Do something
}
public class innerC
{
//trying to access objB here directly or indirectly over here.
//I dont have to create an object of innerB, but to access the object created by A
//i.e.
innerB objInnerB = objB;
//not like this
innerB objInnerB= new innerB();
}
private innerB objB{get;set;} **//Private**
public A()
{
objB= new innerB();
}
}
Run Code Online (Sandbox Code Playgroud)
我想访问由类A创建的类C中的类B的对象.是否有可能以某种方式对类C中的类A的对象进行更改.我可以通过创建事件或无论如何获得A类的对象.
编辑:我在提出上述问题时的错误.在A中创建的B的对象是私有的而不是公共的
通过创造事件是否可能做到这一点
无论如何,我能够提出一个可以由A类处理的事件,那么我的问题就可以解决了.
我有ClassB,它嵌套在ClassA中.在ClassA中我有一个名为_MyId的变量...如何从ClassB访问_MyId?
提前致谢!
我有两个类我需要声明两个类共有的变量..
在嵌套类的情况下,我需要访问内部类中的外部类变量
请在c#中给我一个更好的方法.
示例代码
Class A
{
int a;
Class B
{
// Need to access " a" here
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢