473,289 Members | 2,108 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,289 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 11198
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...
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"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.