473,803 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\Vis ual Studio Projects\Bugrep ort\Class1.cs(2 6): 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 11260
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*******@disc ussions.microso ft.com> wrote in message
news:AF******** *************** ***********@mic rosoft.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\Vis ual Studio Projects\Bugrep ort\Class1.cs(2 6): 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: AcPkOdzeepjnEon lTke+XEEwEPD4gg ==
X-Tomcat-NG: microsoft.publi c.dotnet.genera l
From: =?Utf-8?B?U0FDSElO?= <an*******@disc ussions.microso ft.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************ *************** *******@microso ft.com>
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.publi c.dotnet.genera l
Path: cpmsftngxa07.ph x.gbl
Xref: cpmsftngxa07.ph x.gbl microsoft.publi c.dotnet.genera l:122648
NNTP-Posting-Host: tk2msftcmty1.ph x.gbl 10.40.1.180
X-Tomcat-NG: microsoft.publi c.dotnet.genera l

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\Vis ual Studio Projects\Bugrep ort\Class1.cs(2 6): 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
15537
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
3793
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 class. How can I invert these C++ code into pure c code by using struct in C language? Can somebody give me any ideas? thanks. For example, how to conver the following code into pure c code?
2
2182
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 instance to a reference type (but not System.String). The commented out code are lines that when enabled (and any conflicting lines disabled) stop the exception from being thrown. Thanks kindly, Andrew.
3
2216
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 { int intV; char charV;
2
2358
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 of UserControlA (ucA1) and an instance of UserControlB (ucB1) on a particular ASP.NET page. UserControlA has a string property called StringA that gets generated from a DB call in it's Page_Load event. I want UserControlB to be able to access...
2
634
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
1468
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, network sharing access. thanks,
4
3327
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 MS created that I am trying to overcome They have functions: HANDLE CreateIoCompletionPort( HANDLE FileHandle,
1
1352
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 saying that 'vector has no member named' where x is the fields I defined in my struct. Here is my struct def in the .h file: typedef struct { void* elems; int elemSize; int logLength; int allocLength;
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.