none
Using a struct without initialization RRS feed

  • Question

  • Hello,

    I have declared 2 variables in a UWP app, which are of a type of an own structure. But the compiler gives the error CS0165 if I want to assign a value to a property. A variable of type Windows.Foundation.Size, which is not initialized in my code, can be assigned to another variable without error message. I can also set or read one of its properties, also without error message.

    Furthermore, no compiler error is generated if the property of my structure is a field instead. This also seems not logical.

    What is the difference between Windows.Foundation.Size and MyStructX?

    public struct MyStructA
    {
    	public int	A;
    }
    
    public struct MyStructX
    {
    	public int	X { get; set; }
    }
    
    public MainPage()
    {
    	Size	sz;
    	Size	sz2 = sz;		    // OK
    	double	w = sz.Width;		// OK
    
    	sz.Width = 33;			  // OK
    
    
    	MyStructA	mstA;
    //	MyStructA	mstA2 = mstA;	        // CS0165
    //	int			a = mstA.A;	  // CS0170
    
    	mstA.A = 33;				  // OK
    
    
    	MyStructX	mstX;
    //	MyStructX	mstX2 = mstX;	    // CS0165
    //	int		x = mstX.X;	    // CS0165
    
    	mstX.X = 33;			    // CS0165
    }

    Best Regards,
    Heiko


    Monday, September 30, 2019 12:27 PM

Answers

All replies

  • The error message appears in both case. You might have missed something. 

    See DEMO Example


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Heiko65456465 Tuesday, October 1, 2019 10:42 AM
    Monday, September 30, 2019 12:59 PM
  • Hi,

    I guess it has something to do with the type Windows.Foundation.Size.

    I noticed that it is initialized by default with default values (zero for Width and Height and false for IsEmpty).

    Looking at the documentation it seems this struct is part of the .NET platform extensions and is a .NET projection of the Windows Runtime (WinRT).

    https://docs.microsoft.com/en-us/dotnet/api/windows.foundation.size?view=dotnet-plat-ext-3.0

    Kind regards,

    Johnny Hooyberghs

    Monday, September 30, 2019 1:17 PM
  • Thank you very much for your answers. Then it's something special with the .NET/WinRT structures. In our own structures we either have to set each member, if they are all accessible and writable fields, not properties. Otherwise the struct variable must be assigned another struct instance, e.g. 'new MyStruct()'.

    Best Regards,
    Heiko

    Tuesday, October 1, 2019 10:41 AM