C# - Struct in a class.. can not access properties of the struct

G

Guest

I have this class as part of a Consol application.
using System;

namespace Bugreport
{
/// <summary>
/// This class tries to use the Class/Struct combination.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}


public void test(){
CLS myCLS = new CLS();
///this statement is giving a compile time error:
///D:\ITSSjain\Visual Studio Projects\Bugreport\Class1.cs(26): Cannot modify the return value of 'Bugreport.CLS.myStruct' because it is not a variable.

myCLS.myStruct.Prop1 = "This is a test string";
}
}
/// <summary>
/// Test Structure
/// </summary>
public struct STRUCT{
public string Prop1;
public string Prop2;
}

/// <summary>
/// test class which has a public property exposing the above struct.
/// </summary>
public class CLS{
private STRUCT _myStruct;
public CLS(){
myStruct = new STRUCT();
}
public STRUCT myStruct{
get{
return _myStruct;
}
set{
_myStruct = value;
}
}
}
}

Why am I getting the Compile time error?
thanks
sachin jain
 
E

Eric Gunnerson [MS]

Sachin,

This happens because you are using a struct, and structs have "copy by
value" semantics.

In your statement:
myCLS.myStruct.Prop1 = "This is a test string";

when you call myCLS.myStruct, the myStruct property is returning a copy of
the property value. Referring to a property is then referring to a property
on the *copy*, and if the compiler allowed it, it would be a meaningless
statement, so we don't allow you to say it.

In general, you should stick with classes unless you're really sure you need
to use a struct. If you did want to do this with a struct, you'd have to
write:

MyStruct temp = myCLS.myStruct;
temp.Prop1 = "This is a test string";
myCLS.myStruct = temp;

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
SACHIN said:
I have this class as part of a Consol application.
using System;

namespace Bugreport
{
/// <summary>
/// This class tries to use the Class/Struct combination.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}


public void test(){
CLS myCLS = new CLS();
///this statement is giving a compile time error:
///D:\ITSSjain\Visual Studio Projects\Bugreport\Class1.cs(26): Cannot
modify the return value of 'Bugreport.CLS.myStruct' because it is not a
variable.
 
C

Chris Lyon [MSFT]

Hi Sachin

You can't access the members of a property directly. Try saving myCLS.myStruct into a temporary variable, modifying it, then setting myCLS.myStruct to it.

Either that, or you could change the properties to return the STRUCT's fields, instead of the whole STRUCT.


Hope that helps
-Chris

--------------------
Thread-Topic: C# - Struct in a class.. can not access properties of the struct
thread-index: AcPkOdzeepjnEonlTke+XEEwEPD4gg==
X-Tomcat-NG: microsoft.public.dotnet.general
From: =?Utf-8?B?U0FDSElO?= <[email protected]>
Subject: C# - Struct in a class.. can not access properties of the struct
Date: Mon, 26 Jan 2004 10:26:06 -0800
Lines: 60
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa07.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.general:122648
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.general

I have this class as part of a Consol application.

using System;



namespace Bugreport

{

/// <summary>

/// This class tries to use the Class/Struct combination.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

}





public void test(){

CLS myCLS = new CLS();

///this statement is giving a compile time error:

///D:\ITSSjain\Visual Studio Projects\Bugreport\Class1.cs(26): Cannot modify the return value of 'Bugreport.CLS.myStruct' because it is not a variable.



myCLS.myStruct.Prop1 = "This is a test string";

}

}

/// <summary>

/// Test Structure

/// </summary>

public struct STRUCT{

public string Prop1;

public string Prop2;

}



/// <summary>

/// test class which has a public property exposing the above struct.

/// </summary>

public class CLS{

private STRUCT _myStruct;

public CLS(){

myStruct = new STRUCT();

}

public STRUCT myStruct{

get{

return _myStruct;

}

set{

_myStruct = value;

}

}

}

}



Why am I getting the Compile time error?

thanks

sachin jain

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top