nested types

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

    nested types

    Hi Guys.
    I need to declare an array of PortType in MainType.
    Does any one has idea on how to do that?
    Any help apreciated.

    public struct PortType
    {
    public string Description;
    public int Port;
    }

    public struct MainType
    {
    public string Name;
    //this part has a problem, I want to declare it inside of this
    type, but when I want to use it, there is no ref to Ports from Main[i].
    public static PortType[] Ports = new PortType[999];
    }

    public static MainType[] Main = new MainType[100];


    So is there any way to use like this:
    Main[i].Ports[x].Description="t ext";



  • Peter Duniho

    #2
    Re: nested types

    On Thu, 30 Oct 2008 12:46:54 -0700, Wndr <someone@yahoo. comwrote:
    [...]
    public struct MainType
    {
    public string Name;
    //this part has a problem, I want to declare it inside of
    this
    type, but when I want to use it, there is no ref to Ports from Main[i].
    public static PortType[] Ports = new PortType[999];
    }
    >
    public static MainType[] Main = new MainType[100];
    >
    >
    So is there any way to use like this:
    Main[i].Ports[x].Description="t ext";
    Not if you make the field "static". But if it's an instance member, then
    it should work just as you seem to want it to.

    Why did you mark it "static" if you want it to be an instance member?

    Pete

    Comment

    • Wndr

      #3
      Re: nested types

      Thank you for replay.
      I am new in C#, that's why I called it static, sorry about that, what I
      should change it to?
      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.ujust3y v8jd0ej@petes-computer.local. ..
      On Thu, 30 Oct 2008 12:46:54 -0700, Wndr <someone@yahoo. comwrote:
      >
      >[...]
      >public struct MainType
      > {
      > public string Name;
      > //this part has a problem, I want to declare it inside of
      >this
      >type, but when I want to use it, there is no ref to Ports from Main[i].
      > public static PortType[] Ports = new PortType[999];
      > }
      >>
      >public static MainType[] Main = new MainType[100];
      >>
      >>
      >So is there any way to use like this:
      >Main[i].Ports[x].Description="t ext";
      >
      Not if you make the field "static". But if it's an instance member, then
      it should work just as you seem to want it to.
      >
      Why did you mark it "static" if you want it to be an instance member?
      >
      Pete

      Comment

      • Peter Duniho

        #4
        Re: nested types

        On Thu, 30 Oct 2008 13:45:36 -0700, Wndr <someone@yahoo. comwrote:
        Thank you for replay.
        I am new in C#, that's why I called it static, sorry about that, what I
        should change it to?
        Just delete it. You didn't make your "Name" field "static", and you
        shouldn't make anything else "static" unless you actually need it to be
        "static".

        For future reference, I'd recommend that as you learn C#, you not write
        _any_ code for which you don't understand each and every character in that
        code. If there's something in a line of code you don't understand, read
        and study the documentation until you do understand it.

        This is especially true for those lines of code that don't seem to be
        doing what you want them to. :) Doing so would be a very effective way
        to address specific problems in the code as well as getting a better
        understanding of the language generally.

        Pete

        Comment

        • Wndr

          #5
          Re: nested types

          Yes you know in VB it was prity simple,
          I just had to declare
          Public Type PortType
          Description As String
          Port As Integer
          End Type

          Public Type MainType
          Name As String
          Ports() As PortType
          End Type


          Public Main() As MainType
          and then I could use:
          Main(Index).Por ts(i ).Description ="Test"
          So I was trying to do like this in C#, everything worked fine until I got
          to this point, it's not int, or string, or array, I was looking for this
          info in books and on the web, but still coun't find it.
          Thanks anyway.

          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.ujuu7zd v8jd0ej@petes-computer.local. ..
          On Thu, 30 Oct 2008 13:45:36 -0700, Wndr <someone@yahoo. comwrote:
          >
          >Thank you for replay.
          >I am new in C#, that's why I called it static, sorry about that, what I
          >should change it to?
          >
          Just delete it. You didn't make your "Name" field "static", and you
          shouldn't make anything else "static" unless you actually need it to be
          "static".
          >
          For future reference, I'd recommend that as you learn C#, you not write
          _any_ code for which you don't understand each and every character in that
          code. If there's something in a line of code you don't understand, read
          and study the documentation until you do understand it.
          >
          This is especially true for those lines of code that don't seem to be
          doing what you want them to. :) Doing so would be a very effective way
          to address specific problems in the code as well as getting a better
          understanding of the language generally.
          >
          Pete

          Comment

          • Peter Duniho

            #6
            Re: nested types

            On Thu, 30 Oct 2008 14:23:19 -0700, Wndr <someone@yahoo. comwrote:
            Yes you know in VB it was prity simple,
            I just had to declare
            Public Type PortType
            Description As String
            Port As Integer
            End Type
            >
            Public Type MainType
            Name As String
            Ports() As PortType
            End Type
            Ironically, the correct C# code is more similar to the VB.NET code you
            posted than is the C# code you apparently tried.

            Comment

            Working...