Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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;
	}
}
Posted
Updated 27-Sep-10 15:15pm
v3

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

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:
 
Share this answer
 
Comments
Abhishrek 2-Oct-10 9:39am    
Thanks a lot Abhishek for the solution and for the explanation.
(sorry for taking it long to reply or accept your answer)
What is your objection to making them plain old properties?

C#
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
}
 
Share this answer
 
Comments
Abhishrek 27-Sep-10 21:09pm    
As i wrote in the question, there will be many class fields which i need to simply copy from one object to another object. I can ofcourse do it one by one if the fields use property. However it would be much simpler to simply copy one struct to another !!! Thus i was looking for struct approach.
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:

C#
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. :)
 
Share this answer
 
Comments
Abhishrek 2-Oct-10 9:35am    
Thanks man ... it seems good :)
i still wonder, why there is no such feature in C# to access multiple fields together, as a struct

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900