473,386 Members | 1,698 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,386 software developers and data experts.

Structs + Object

Little question:
I have a class A
{
public A()
{
//Whatever
}
public SomeStruct prop
{
get {return m_prop;}
set {m_prop = value;}
}
}
And structure SomeStruct
{
public string s1,s2;
public SomeStruct(string a1, a2)
{
s1 = a1;
s2 = a2;
}
}

I want to be able to assign A.prop.s1 = "Whatever"; and A.prop.s2 =
"Whatever 2"; //unable to modify rerurn value, but I want to !!!
rather then A.prop = new SomeStruct("Whatever","Whatever 2");

How to do this, ideas?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #1
5 1117
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Little question:
I have a class A
{
public A()
{
//Whatever
}
public SomeStruct prop
{
get {return m_prop;}
set {m_prop = value;}
}
}
And structure SomeStruct
{
public string s1,s2;
public SomeStruct(string a1, a2)
{
s1 = a1;
s2 = a2;
}
}

I want to be able to assign A.prop.s1 = "Whatever"; and A.prop.s2 =
"Whatever 2"; //unable to modify rerurn value, but I want to !!!
rather then A.prop = new SomeStruct("Whatever","Whatever 2");

How to do this, ideas?


Well, you just can't, with the above. You could:

1) Have methods in A to set each part part of the property
2) Make SomeStruct a class instead
3) (Not recommended) Make m_prop public, and get rid of the property

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
tnx
If I'll do it class so what'll happend with initialization? This way I'd
have to initialize it each time used or do it something like
public SomeClass
{
public string s1,s2;
}

....

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Little question:
I have a class A
{
public A()
{
//Whatever
}
public SomeStruct prop
{
get {return m_prop;}
set {m_prop = value;}
}
}
And structure SomeStruct
{
public string s1,s2;
public SomeStruct(string a1, a2)
{
s1 = a1;
s2 = a2;
}
}

I want to be able to assign A.prop.s1 = "Whatever"; and A.prop.s2 =
"Whatever 2"; //unable to modify rerurn value, but I want to !!!
rather then A.prop = new SomeStruct("Whatever","Whatever 2");

How to do this, ideas?


Well, you just can't, with the above. You could:

1) Have methods in A to set each part part of the property
2) Make SomeStruct a class instead
3) (Not recommended) Make m_prop public, and get rid of the property

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
If I'll do it class so what'll happend with initialization? This way I'd
have to initialize it each time used or do it something like
public SomeClass
{
public string s1,s2;
}


No, you specify a constructor with the class, just as you did with the
struct.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
tnx

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
If I'll do it class so what'll happend with initialization? This way I'd
have to initialize it each time used or do it something like
public SomeClass
{
public string s1,s2;
}


No, you specify a constructor with the class, just as you did with the
struct.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Hi Tamir,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are receiving a compiler error when
assigning to a property returned value. If there is any misunderstanding,
please feel free to let me know.

You get this error because SomeStruct is a value type. When the property
returns it, it is put on the stack as a temporary variable. It is a copy of
m_prop and is read-only. Assigning value to it doesn't make sense. If you
need to assign to it, besides make SomeStruct a class, you can also try the
following when accessing it.

SomeStruct ss = a.prop;
ss.s1 = "Whatever";

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #6

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

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
8
by: Christian Christmann | last post by:
Hi, I was wondering how the =operator works for struct. When I for example define a struct as follows: struct point { int a; char *c;
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
6
by: Christian Christmann | last post by:
Hi, a question on local structs: Usually, when a local variable is used without initialization, it might hold any value. According to C99, does this rule also apply to elements of local...
4
by: veera sekhar kota | last post by:
I read structs are stored at stack area or inline-heap based objects. What is meant by inline-heap based objects? I didnt get that. Thanks, Veera.
17
by: Johan Tibell | last post by:
Could someone outline the pros and cons of typedefing pointers to structs like this? typedef struct exp_ { int val; struct exp_ *child; } *exp; (This is straight from memory so it might not...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.