Structs in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kakkany
    New Member
    • Dec 2009
    • 6

    Structs in C#

    I have one structure object of Temp strucure.

    Code:
    struct Temp
    {
    string caption
    }
    I want to add it to ArrayList.

    Code:
    ArrayList list = new ArrayList();
    Temp original= new Temp()
    list.add(original);
    Then I want to aceess list[0].caption

    Code:
    object tempobject = list  [0] as object;
      Temp temp = (Temp )tempobject ;
           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?
    Last edited by tlhintoq; Dec 21 '09, 08:38 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Code:
    namespace demo
    {
        class Temp
        {
            public string caption
            {
                get; set;
            }
        }
    
        class Program
        {
            List<Temp> myTemps = new List<Temp>();
    
            void Main()
            {
                myTemps.Add(new Temp());
                myTemps[0].caption = "jo";
            }
        }
    }
    Second... Don't make more work than necessary. From your example:
    Code:
    object tempobject = list [0] as object;
    Temp temp = (Temp )tempobject ;
    temp .caption= "jo";
    You don't have to make anything to read what you have already made.
    list[0] is already a 'temp'
    You could have just
    Code:
    list[0].Capture = "jo";

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      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.

      Comment

      • kakkany
        New Member
        • Dec 2009
        • 6

        #4
        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?

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Re-read the second half of my post...

          Second... Don't make more work than necessary. From your example:
          Code:
          object tempobject = list [0] as object;
          Temp temp = (Temp )tempobject ;
          temp .caption= "jo";
          You don't have to make anything to read what you have already made.
          list[0] is already a 'temp'
          You could have just
          Code:
          list[0].Capture = "jo";

          Comment

          • kakkany
            New Member
            • Dec 2009
            • 6

            #6
            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.

            Code:
            ArrayList module1Info = new ArrayList();
            module1Info.Add(Class1.obj); 
            module1Info.Add(struct2.obj); //  a structure type
            
            ArrayList module2Info = new ArrayList();
            module2Info.Add(Class1.obj); 
            module2Info.Add(struct2.obj); // a structure type
            
            ArrayList moduleList = new ArrayList();
            moduleList.Add(module1Info);
            moduleList.Add(module2Info);
            
            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";
            }

            struct2 has this structure.

            Code:
             struct2 
            {
            
            string caption;
            
            }

            Could you please tell me

            1. How it is possible?
            2. Is there any datastructure other than ArrayList you are suggesting for this
            Last edited by tlhintoq; Dec 22 '09, 03:27 PM. Reason: Make it easier to follow this mess

            Comment

            • kakkany
              New Member
              • Dec 2009
              • 6

              #7
              Can anyone provide a reply to this ( above).. It is urgent?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                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

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  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.

                  Code:
                  moduleList [an ArrayList]
                       [0]  = module1list [another ArrayList]
                                     [0] = Class1.obj
                                     [1] = struct2.obj
                       [1] = module2list [an ArrayList]
                                     [0] = Class2.obj
                                     [1] = struct2.obj
                  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.

                  Comment

                  • kakkany
                    New Member
                    • Dec 2009
                    • 6

                    #10
                    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...tlhi ntoq... 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).

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      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.

                      Comment

                      • kakkany
                        New Member
                        • Dec 2009
                        • 6

                        #12
                        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..

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          Arrays are zero indexed.
                          arrayList[0] is the first element of your array
                          arrayList[1] is the second element of your array

                          Code:
                          moduleList.Add(module1Info);  // This is setting moduleList[0] to module1Info
                          moduleList.Add(module2Info);  // This is setting moduleList[1] to module2Info

                          Code:
                          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]

                          Code:
                          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.cap tion

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

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

                          Code:
                          ArrayList module1Info = new ArrayList();
                          module1Info.Add(Class1.obj); // module1Info[0]
                          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.Capt ion

                          But you aren't using classes. You are using structs. Structure don't allow you to nest data like this.

                          Comment

                          Working...