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

Structs in C#

I have one structure object of Temp strucure.

Expand|Select|Wrap|Line Numbers
  1. struct Temp
  2. {
  3. string caption
  4. }
I want to add it to ArrayList.

Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();
  2. Temp original= new Temp()
  3. list.add(original);
Then I want to aceess list[0].caption

Expand|Select|Wrap|Line Numbers
  1. object tempobject = list  [0] as object;
  2.   Temp temp = (Temp )tempobject ;
  3.        temp .caption= "jo";
Since struct is a reference type, it does'nt modify oringinal object (original).
I want to modify original object. What should I do?
Dec 21 '09 #1

✓ answered by Frinavale

Well the simple answer to this is you cannot accomplish what you want to do using structs. A struct is a value type, while a class is a reference type.

Since you need/want to use reference type semantics then you cannot use a struct in this case. Convert your struct into an class.

-Frinny

12 1638
tlhintoq
3,525 Expert 2GB
Start by getting away from old style structs and arraylists.
You can use a class in place of a struct. In the future your class can gain higher-end capabilities such as methods that will run. If it never grows that smart, that's fine, just use it for the data.

Expand|Select|Wrap|Line Numbers
  1. namespace demo
  2. {
  3.     class Temp
  4.     {
  5.         public string caption
  6.         {
  7.             get; set;
  8.         }
  9.     }
  10.  
  11.     class Program
  12.     {
  13.         List<Temp> myTemps = new List<Temp>();
  14.  
  15.         void Main()
  16.         {
  17.             myTemps.Add(new Temp());
  18.             myTemps[0].caption = "jo";
  19.         }
  20.     }
  21. }
Second... Don't make more work than necessary. From your example:
Expand|Select|Wrap|Line Numbers
  1. object tempobject = list [0] as object;
  2. Temp temp = (Temp )tempobject ;
  3. temp .caption= "jo";
  4.  
You don't have to make anything to read what you have already made.
list[0] is already a 'temp'
You could have just
Expand|Select|Wrap|Line Numbers
  1. list[0].Capture = "jo";
Dec 21 '09 #2
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Dec 21 '09 #3
Thanks for your reply.. but I want to use struct..thats in base class.
I can not modify it. Could you please tell me if there is an option to use this?
Dec 22 '09 #4
tlhintoq
3,525 Expert 2GB
Re-read the second half of my post...

Second... Don't make more work than necessary. From your example:
Expand|Select|Wrap|Line Numbers
  1. object tempobject = list [0] as object;
  2. Temp temp = (Temp )tempobject ;
  3. temp .caption= "jo";
  4.  
You don't have to make anything to read what you have already made.
list[0] is already a 'temp'
You could have just
Expand|Select|Wrap|Line Numbers
  1. list[0].Capture = "jo";
Dec 22 '09 #5
I am adding some pseudo code...I want to use structure like reference type.
I can not change the struct to class. because it is in base class.

Expand|Select|Wrap|Line Numbers
  1. ArrayList module1Info = new ArrayList();
  2. module1Info.Add(Class1.obj); 
  3. module1Info.Add(struct2.obj); //  a structure type
  4.  
  5. ArrayList module2Info = new ArrayList();
  6. module2Info.Add(Class1.obj); 
  7. module2Info.Add(struct2.obj); // a structure type
  8.  
  9. ArrayList moduleList = new ArrayList();
  10. moduleList.Add(module1Info);
  11. moduleList.Add(module2Info);
  12.  
  13. foreach (ArrayList arrayList in moduleList)
  14. {  
  15.      //Through this action I want to change the original   struct2.obj caption
  16.      //How it is possible..
  17.      arrayList [1]. caption = "jo";
  18. }

struct2 has this structure.

Expand|Select|Wrap|Line Numbers
  1.  struct2 
  2. {
  3.  
  4. string caption;
  5.  
  6. }

Could you please tell me

1. How it is possible?
2. Is there any datastructure other than ArrayList you are suggesting for this
Dec 22 '09 #6
Can anyone provide a reply to this ( above).. It is urgent?
Dec 22 '09 #7
Frinavale
9,735 Expert Mod 8TB
Well the simple answer to this is you cannot accomplish what you want to do using structs. A struct is a value type, while a class is a reference type.

Since you need/want to use reference type semantics then you cannot use a struct in this case. Convert your struct into an class.

-Frinny
Dec 22 '09 #8
tlhintoq
3,525 Expert 2GB
ArrayList[1] doesn't have a .caption property.
arrayList element 1 is another arrayList

You are getting lost in your own nested data structures. If you can't visualize in your head then write it down so you can see what you are working with.

Expand|Select|Wrap|Line Numbers
  1. moduleList [an ArrayList]
  2.      [0]  = module1list [another ArrayList]
  3.                    [0] = Class1.obj
  4.                    [1] = struct2.obj
  5.      [1] = module2list [an ArrayList]
  6.                    [0] = Class2.obj
  7.                    [1] = struct2.obj
  8.  
Since you are only adding the ".obj" property of struct2 to your array list, you aren't going to be able to change the ".caption" property from within the ArrayList: It doesn't exist within the arraylist

You are still going to have to change this into classes or objects though as structs won't allow you to do what you are trying to do.

I would suggest you start slower. Its obvious that you don't have a good grasp on classes or objects or structs.
  • Build a project that uses just one type of data and get good with it. Practice and learn and READ ABOUT what a struct is and can do.
  • Then work on a project that has just a class.
  • Then start putting together a List<> of classes so you can get used to working with lists and nested objects.
Crawl before you walk. Walk before you run.
Dec 22 '09 #9
Hello tlhintoq

In the post i have mentioned, I am adding some pseudo code...

Class1.obj means - I am creating an object of class 1 and I am adding to arraylist.

struct2.obj means - I am creating an object of struct2 and I am adding to arraylist.

You have wriiten arrayList element 1 is another arrayList. That is not true..
I am using foreach loop...


OK AnyWay I liked your response...tlhintoq... GOOD ...I know how to walk and run...


Thanks Frinavale for your information.. I am a c++ programmer.. So I just want to make sure that I am right.. In the first post I clearly mentioned

Since struct is a reference type, it does'nt modify oringinal object (original).
Dec 22 '09 #10
tlhintoq
3,525 Expert 2GB
You have wriiten arrayList element 1 is another arrayList. That is not true..
It is according to your code:

foreach (ArrayList arrayList in moduleList)
'arrayList' is an element from moduleList. 'arrayList' is an ArrayList.

ArrayList moduleList = new ArrayList();
'ModuleList' is an ArrayList

ArrayList module2Info = new ArrayList();
module2Info is an ArrayList

ArrayList moduleList = new ArrayList();
'moduleList is an ArrayList



moduleList.Add(module1Info);
moduleList {an ArrayList} *add* module1Info {an ArrayList}

So... WHen you do this
foreach (ArrayList arrayList in moduleList)
'arrayList' is the first element of moduleList, which is module1Info {an array list}

You have confused yourself with your nested ArrayLists.
Dec 22 '09 #11
ArrayList[1] doesn't have a .caption property.
arrayList element 1 is another arrayList
foreach (ArrayList arrayList in moduleList)
{
//Through this action I want to change the original struct2.obj caption
//How it is possible..
arrayList [1]. caption = "jo";
}
What i understood is for-each loop gives the arraylist and arrayList [1] (struct) has caption property..
Dec 23 '09 #12
tlhintoq
3,525 Expert 2GB
Arrays are zero indexed.
arrayList[0] is the first element of your array
arrayList[1] is the second element of your array

Expand|Select|Wrap|Line Numbers
  1. moduleList.Add(module1Info);  // This is setting moduleList[0] to module1Info
  2. moduleList.Add(module2Info);  // This is setting moduleList[1] to module2Info

Expand|Select|Wrap|Line Numbers
  1. foreach (ArrayList arrayList in moduleList)
the first time through this loop 'arrayList' will contain moduleList[0]
the second time through this loop 'arrayList' will contain moduleList[1]

Expand|Select|Wrap|Line Numbers
  1. arrayList [1].caption = "jo"; // just wrong
the first time through the foreach loop arrayList will be moduleList[0] which is module1Info.
So at that point arrayList[1] might as well say module1Info[1] so we could re-write the line as:
module1Info[1].Caption = "jo";
Since module1Info[1] is really struct2.obj we could further re-write this line as:
struct2.obj.caption

Now do you see why it doesn't work? Because you need struct2.caption not struct2.obj.caption

Let's look at the ArrayList that is module1Info[1]

Expand|Select|Wrap|Line Numbers
  1. ArrayList module1Info = new ArrayList();
  2. module1Info.Add(Class1.obj); // module1Info[0]
  3. module1Info.Add(struct2.obj); // module1Info[1]
module1Info[1] is struct2.obj because that's what you assigned to it.
I have no idea what .obj property is of your struct2 because you don't show that in your code: You only show the .Caption property. But whatever the .obj property is, that is what you are assigning to module1Info[1]


If you were using classes then you could actually do:
Class2.obj.Caption

But you aren't using classes. You are using structs. Structure don't allow you to nest data like this.
Dec 23 '09 #13

Sign in to post your reply or Sign up for a free account.

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...
6
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
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...
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...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
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...
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.