Can I define Array with a defined structure type?

I want to define an array of structure tryped data. Can I do it and is it same as conventional way of Array define

Thank you very much for your help.

James



Answer this question

Can I define Array with a defined structure type?

  • TejasLambu

    Hello, Chris:

    Here's what I tried and got the error in C#. Could anybody please give the answer Thank you very much.

    using System;

    struct SimpleStruct

    {

    private int xval;

    public int X

    {

    get

    {

    return xval;

    }

    set

    {

    if (value < 100)

    xval = value;

    }

    }

    public void DisplayX()

    {

    Console.WriteLine("The stored value is: {0}", xval);

    }

    }

    class TestClass

    {

    public static void Main()

    {

    SimpleStruct ss = new SimpleStruct();

    SimpleStruct [] Nd = new SimpleStruct(); // Test here.

    Nd.x = 1; // Test here.

    ss.X = 5;

    ss.DisplayX();

    }

    }

    Error shows:

    Error 1 Cannot implicitly convert type 'SimpleStruct' to 'SimpleStruct[]' C:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\Structs\Struct1\struct1.cs 31 30 Struct1
    Error 2 'System.Array' does not contain a definition for 'x' C:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\Structs\Struct1\struct1.cs 32 12 Struct1

    Please help, thank you.

    James


  • technoxicated

    Thank you very much. It works now. J.
  • Rag_Singh

    I don't see why not - have you tried



  • rclenzi

    SimpleStruct [] Nd = new SimpleStruct[20];

    You need to use square brackets, and you need to specify how many you want. Use ArrayList for dynamic arrays, or List.

    Nd.x = 1; // Test here.

    ss.X = 5;

    This can't work. If you have an array, you need to index the array to access the individual objects, as in Nd[0].X = 1; Also, C# is case sensitive, .x won't work at all.



  • BCDC

    Thank you for your info. I didn't see a sample in C# document so I have not tried it out. Would you have any example of this in C#

    I greatly appreciate your help.

    James


  • Can I define Array with a defined structure type?