类内部的struct字段的属性,无法访问类的字段 [英] struct field's property inside a class, cannot access class's fields

查看:62
本文介绍了类内部的struct字段的属性,无法访问类的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,能否请您帮我寻找解决方案.

情况:
有一个form (MyForm)和一个user control class (MyShape),
MyForm内部将有多个MyShape 对象.现在,每个对象都需要了解有关其他对象的一些信息.由于信息具有许多字段,并且对所有对象都通用,因此我认为将信息绑定在结构(或类???)中会更好.我需要将此struct 的字段设置为属性,以便可以读取最新数据.

问题:
MyShape的字段不能在struct的字段的属性中访问.

需要帮助:
有没有一种方法可以访问struct字段中的MyShape字段?
是使用class 而不是struct 更好吗?
还有另一种方法可以执行这样的任务吗?

代码:
为了更好地理解我的要求,示例代码如下:
(*此代码不正确,仅代表我的要求)

Hello everyone, can you please help me in finding the solution.

Situation:
There is a form (MyForm), and a user control class (MyShape),
there will be multiple objects of MyShape inside MyForm. Now each object need to know some info about some other object. As the info has many fields and its common to all the objects, i thought it would be better to bind the info in a struct (or a class ???). I need to make the fields of this struct as property, so that i can read the latest data.

problem:
MyShape''s fields are not accessible inside struct''s fields''s property.

Help required :
Is there a way to access MyShape''s fields inside struct''s fields''s ?
Is using class rather than struct be better ?
Is there another way to perform such task ?

Code:
A sample code for better understanding of my requirement is as below:
(* THIS CODE IS NOT CORRECT, ITS JUST A REPRESENTATION OF MY REQUIREMENT)

public class MyShape
{
	private int num;
	private int size;
	public struct Info
	{
		int Num{ get { return num;} set{ num = value;}} // Error
		int Size{ set { size = value;} set{ size = value;}} // Error		
...
	};

	public static Info info;

	public MyShape(int num, int size)
	{
		this.num = num;
		this.size = size;
	}
}

public class MyForm : form
{
	private void SomeFunc()
	{
		MyShape shapeA = new MyShape(1,2);
		MyShape shapeB = new MyShape(3,4);

		shapeA.info = shapeB.info;
	}
}

推荐答案

嘿阿卜沙克,

基本上,我们创建struct/class来创建对象.
类/结构保存对象的定义,以便在创建对象时将保存其每个成员的引用.

现在嵌套一个类或结构并不意味着可以访问外部的属性.您可能要做的一件事是

Hey Abhshake,

Basically we create struct /class to create objects.
Class / struct holds the definition of an object so that when an object is created it will hold reference of each of its members.

Now Nesting a class or struct does not mean that the properties from outside will be accessible. One thing which you might do is

public class MyShape
{
   public struct MyNestedStruct
   {
      public MyShape ParentObject {get;set;}
      public MyNestedStruct(MyShape sobj)
      {
          this.MyShape = sobj; 
      }
   } 

   public MyNestedStruct MyStruct = new MyNestedStruct(this);

}

现在,从嵌套结构中,您可以访问MyShape的对象.
实际上,您只能通过类的对象访问类中的属性.没有对象就无法访问属性.

因此,在您的情况下,最好是将属性公开为类的成员,而不是创建Struct,或者,如果您希望将其发送给结构,以获取对父对象的引用,则比创建Struct更好.
:rose:

}

Now from the nested struct you can access the object of MyShape.
Actually you can only access the properties from within a class by its object. You cannot access a property without an object.

So in your case, rather than creating a Struct it is better to expose properties as Member of the class, or if you wish you can send the object this to the struct to get reference to the parentobject.
:rose:


您反对将它们设置为普通旧属性吗?

What is your objection to making them plain old properties?

public class MyShape
{
    private int num;
    private int size;

    public MyShape(int num)
    {
        this.num = num;
    }

    public int Num
    {
        get
        {
             return this.num;
        }

        set
        {
            this.num = value;
        }
    }

    // and so on
}


如果您不赞成Abishek的解决方案,另一种可能性是在任何类外部声明您的结构,然后在您的类中具有Method或Property并返回struct的实例.
像这样的东西:

Another possibility if you do not favour Abishek''s solution is to declare your struct outside of any class and then have either a Method or Property in your class that returns an instance of the struct.
Something like this:

public struct UsefulStruct
{
  public int number;
  public string name;

  public UsefulStruct(int number, string name)
  {
    this.number = number;
    this.name = name;
  }
}

public class YourClass
{
  int number;
  string name;

  // using a method
  public UseFulStruct GetUsefulInfo()
  {
    return new UsefulStruct(this.number, this.name);
  }

  // using a property
  public UsefulStruct UsefulInfo
  {
    get
    {
      return new UsefulStruct(this.number, this.name);
    }
  }
}



祝你好运. :)



Good luck. :)


这篇关于类内部的struct字段的属性,无法访问类的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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