473,216 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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

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
Jul 21 '05 #1
2 11176
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" <an*******@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com... 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

Jul 21 '05 #2
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?= <an*******@discussions.microsoft.com>
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: <AF**********************************@microsoft.co m>
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.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dam_fool_2003 | last post by:
Hai, Several advanced programs access the struct's member like the one following: #define str(p) (p.data) struct node { unsigned int data; }; int main(void) {
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
3
by: Dean L. Howen | last post by:
Hi friends, In C++, we can declare a struct and write it into file just by giving the address the strct instance (using &). for example: ///////////////////////////////// typedef struct Test...
2
by: Jon Hyland | last post by:
This might be a dumb question, but what is the best way for one instance of a user control to access properties of an instance of another user control? For example, let's say I have an instance...
2
by: SACHIN | last post by:
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 {
1
by: CT | last post by:
Hi, Looking for an API that provides directory + file access properties. There is a lot of ansii functions that returns only the properties of a file/directory, i'm more interested in security,...
4
by: Christopher Pisz | last post by:
This is a solution I thought about to some of the problems I had talked about in the "zero memory" above in the ng. While this isn't a MS specific post, let me walk you though the problem that...
1
by: tytelizgal | last post by:
Hello, Here is my problem: I declare the vector struct in my .h file and then try to access its fields in the .c file. I did declare a variable of type vector. However, I get a compiler error...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.