Does C# support array of classes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrea Trevisan

    #1

    Does C# support array of classes?


    I'm following a tutorial tour for Microsoft C# and I ask if
    this language supports array of classes like C++.That's a
    personal curiosity.
    I add the body of the two .cs files of my project which works fine but
    commented-out lines:

    using System;

    namespace My3
    {
    /// <summary>
    /// Summary description for CAMix.
    /// </summary>
    public class CAMix
    {
    public CAMix()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    public int i_Y;
    }
    }

    using System;

    namespace My3
    {
    class CMix
    {
    public int i_Y;
    }

    struct SMix
    {
    public int i_Y;
    }
    /// <summary>
    /// Summary description for Class1.
    /// </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
    //
    CMix c_Mix1 = new CMix();
    CMix[] c_Mix2 = new CMix[4];
    CAMix[] c_Mix3 = new CAMix[4];

    SMix s_Mix1;
    SMix s_Mix2 = new SMix();
    SMix[] s_Mix3 = new SMix[7];

    c_Mix1.i_Y = 8;
    Console.WriteLi ne("c_Mix1.i_Y = " + c_Mix1.i_Y.ToSt ring());
    /*for (int i_Index = 0; i_Index < c_Mix2.Length; i_Index++)
    c_Mix2[0].i_Y = i_Index * 100;*/
    /*for (int i_Index = 0; i_Index < c_Mix3.Length; i_Index++)
    c_Mix3[0].i_Y = i_Index * 100;*/

    s_Mix1.i_Y = 3;
    Console.WriteLi ne("s_Mix1.i_Y = " + s_Mix1.i_Y.ToSt ring());
    s_Mix2.i_Y = 15;
    Console.WriteLi ne("s_Mix2.i_Y = " + s_Mix2.i_Y.ToSt ring());
    for (int i_Index = 0; i_Index < s_Mix3.Length; i_Index++)
    {
    s_Mix3[i_Index].i_Y = i_Index * 10;
    Console.WriteLi ne("s_Mix3[{0}].i_Y = " +
    s_Mix3[i_Index].i_Y.ToString() , i_Index);
    }
    }
    }
    }

    This text editor isn't wonderful,but I rely on your wonderful will.
    regards
    Mr. Trevisan Andrea

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Fergus Cooney

    #2
    Re: Does C# support array of classes?

    Dear Andrea,

    I don't know about C++ but C# has Types with which you can do loads of
    classy stuff (pun intended).

    See my post in the thread "opening form by name" in which you can see
    how knowing the name of a form (which is also its class name) is enough to
    create and display an instance of the form using Types (and Reflection).

    With an array of Types you can do similar things and more for any
    class/type.

    Hope this piques your curiousity to study Reflection - it's very
    powerful.

    Best wishes,
    Fergus


    Comment

    • Zhanyong Wan [MSFT]

      #3
      Re: Does C# support array of classes?

      Hi Fergus,

      I believe what Andrea wanted is "array of objects", or "array of class
      instances." It is different from "array of Type objects", which I admit is
      very useful. Thanks,

      - Zhanyong Wan

      Visual Studio and .NET Setup

      This posting is provided "AS IS" with no warranties, and confers no rights.
      Use of included samples (if any) is subject to the terms specified at
      http://www.microsoft.com/info/cpyright.htm
      --------------------
      | From: "Fergus Cooney" <wollus@tesco.n et>
      | References: <us#FL$MaDHA.14 92@TK2MSFTNGP12 .phx.gbl>
      | Subject: Re: Does C# support array of classes?
      | Date: Sun, 24 Aug 2003 02:37:38 +0100
      | Lines: 19
      | X-Priority: 3
      | X-MSMail-Priority: Normal
      | X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
      | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
      | Message-ID: <ekOYaBeaDHA.40 28@tk2msftngp13 .phx.gbl>
      | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      | NNTP-Posting-Host: m55-mp1.cvx3-a.bre.dial.ntli .net 62.255.96.55
      | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
      | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1789 13
      | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      |
      | Dear Andrea,
      |
      | I don't know about C++ but C# has Types with which you can do loads of
      | classy stuff (pun intended).
      |
      | See my post in the thread "opening form by name" in which you can see
      | how knowing the name of a form (which is also its class name) is enough to
      | create and display an instance of the form using Types (and Reflection).
      |
      | With an array of Types you can do similar things and more for any
      | class/type.
      |
      | Hope this piques your curiousity to study Reflection - it's very
      | powerful.
      |
      | Best wishes,
      | Fergus
      |
      |
      |

      Comment

      • Fergus Cooney

        #4
        Re: Does C# support array of classes?

        Hi Zhanyong,

        Reckon you're right there. :-)

        And, on reflection (no pun this time), I'm aiming a bit further
        into her future with .NET!

        Regards,
        Fergus


        Comment

        Working...