Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. make a class property an array of an instantiated class?

make a class property an array of an instantiated class?

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmatteson
    wrote on last edited by
    #1

    v9ExportPacket p = new v9ExportPacket(); p.TemplateRecord = new TemplateRecord[5]; TemplateRecord t_tmp = new TemplateRecord(); t_tmp.FlowSetID=0; t_tmp.Length=10; t_tmp.id=23; t_tmp.FieldCount=4; t_tmp.Fields= new FieldPairs[t_tmp.FieldCount]; t_tmp.Fields[0] = new FieldPairs(1,2); t_tmp.Fields[1] = new FieldPairs(3,4); t_tmp.Fields[2] = new FieldPairs(5,6); t_tmp.Fields[3] = new FieldPairs(7,8); p.TemplateRecord[0] = t_tmp; i was wondering if there is a way that i could just use p.TemplateRecord[0].id=1; instead of having to create a TemplateRecord object t_tmp to set the properties of that class and then set the TemplateRecord[0] property object equal to t_tmp object. i have looked into using indexers but i haven't seen an example where you could access a property of an indexed property.

    C A 2 Replies Last reply
    0
    • M mmatteson

      v9ExportPacket p = new v9ExportPacket(); p.TemplateRecord = new TemplateRecord[5]; TemplateRecord t_tmp = new TemplateRecord(); t_tmp.FlowSetID=0; t_tmp.Length=10; t_tmp.id=23; t_tmp.FieldCount=4; t_tmp.Fields= new FieldPairs[t_tmp.FieldCount]; t_tmp.Fields[0] = new FieldPairs(1,2); t_tmp.Fields[1] = new FieldPairs(3,4); t_tmp.Fields[2] = new FieldPairs(5,6); t_tmp.Fields[3] = new FieldPairs(7,8); p.TemplateRecord[0] = t_tmp; i was wondering if there is a way that i could just use p.TemplateRecord[0].id=1; instead of having to create a TemplateRecord object t_tmp to set the properties of that class and then set the TemplateRecord[0] property object equal to t_tmp object. i have looked into using indexers but i haven't seen an example where you could access a property of an indexed property.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If you create an indexer and the object is a class, it's passed by reference, so you can automatically alter indexed properties.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      M 1 Reply Last reply
      0
      • M mmatteson

        v9ExportPacket p = new v9ExportPacket(); p.TemplateRecord = new TemplateRecord[5]; TemplateRecord t_tmp = new TemplateRecord(); t_tmp.FlowSetID=0; t_tmp.Length=10; t_tmp.id=23; t_tmp.FieldCount=4; t_tmp.Fields= new FieldPairs[t_tmp.FieldCount]; t_tmp.Fields[0] = new FieldPairs(1,2); t_tmp.Fields[1] = new FieldPairs(3,4); t_tmp.Fields[2] = new FieldPairs(5,6); t_tmp.Fields[3] = new FieldPairs(7,8); p.TemplateRecord[0] = t_tmp; i was wondering if there is a way that i could just use p.TemplateRecord[0].id=1; instead of having to create a TemplateRecord object t_tmp to set the properties of that class and then set the TemplateRecord[0] property object equal to t_tmp object. i have looked into using indexers but i haven't seen an example where you could access a property of an indexed property.

        A Offline
        A Offline
        Andrew Rissing
        wrote on last edited by
        #3

        Not really clear on what you're trying to do, but I'll take a stab at it. The important thing to remember when trying to access an index is what is it returning me? For instance, take a DataSet.

        DataSet ds;
        
        DataTable dt = ds.Tables[0]; // Gives you a DataTable (the first one in the dataset).
        DataRow dr = ds.Tables[0].Rows[0]; // Gives you a DataRow (the first row, of the first table, in the data set).
        object o = ds.Tables[0].Rows[0][0]; // Gives you an object (the value of the first column, in the first row, of the first table in the dataset).
        

        So, you can continue to index off a property, as long as that property contains an index operation. For your case, you might need to create a 'Collection' class that simply holds a collection of items for you. Assuming I'm even close on what you're asking a question about. :cool: Hope that helps.

        M 1 Reply Last reply
        0
        • A Andrew Rissing

          Not really clear on what you're trying to do, but I'll take a stab at it. The important thing to remember when trying to access an index is what is it returning me? For instance, take a DataSet.

          DataSet ds;
          
          DataTable dt = ds.Tables[0]; // Gives you a DataTable (the first one in the dataset).
          DataRow dr = ds.Tables[0].Rows[0]; // Gives you a DataRow (the first row, of the first table, in the data set).
          object o = ds.Tables[0].Rows[0][0]; // Gives you an object (the value of the first column, in the first row, of the first table in the dataset).
          

          So, you can continue to index off a property, as long as that property contains an index operation. For your case, you might need to create a 'Collection' class that simply holds a collection of items for you. Assuming I'm even close on what you're asking a question about. :cool: Hope that helps.

          M Offline
          M Offline
          mmatteson
          wrote on last edited by
          #4

          hey thanks for the reply. yea sorry i wasn't too clear as it was a post right as i was leaving work today. i think i understand what you mean and i will give it a try! thanks

          1 Reply Last reply
          0
          • C Christian Graus

            If you create an indexer and the object is a class, it's passed by reference, so you can automatically alter indexed properties.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            M Offline
            M Offline
            mmatteson
            wrote on last edited by
            #5

            Okay so let me see if i understand you correctly. I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th property and make its type equal to ClassB. but i would also like to index this property too. BTW Class B also has some properties to it. So in the end i would be able to access my items like this: classA xyz = new classA(); xyz.prop1=1; xyz.prop2=2; xyz.prop3=3; xyz.prop4=4; xyz.prop5[0].prop1=1; xyz.prop5[0].prop2=2; xyz.prop5[1].prop1=1; xyz.prop5[1].prop2=2; I remember doing stuff like this a while ago in VB6 but i'm pretty new to C# and i'm just learning out to do this sort of stuff. I hope that explain helps a little bit more. I think i understand what your saying though.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups