chnage the Array Element

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

    #1

    chnage the Array Element

    Hi All..

    i am Writing a C# code ..but i had face a small problem ,
    i hope that you can solve it Please.

    I had creat the following class

    class Q
    {
    int A;
    Int B
    }

    then in the main i creat an array of the above class

    Q[] Array_Q = new Q[55]();

    then i tried to fill the elemnets with a values

    int i,j;
    for (i=0 ; i<=10 ; i++)
    {
    for (j=i;j<=10;j++)
    Array_Q[i].A = i; //<== here is the error
    Array_Q[i].B = j;
    }


    if I run the program it give me an Exchption handelling
    Error ..!!
    so what is the Problem do you think..


    thank you All
  • Jon Skeet

    #2
    Re: chnage the Array Element

    sh <hshgh@yahoo.co m> wrote:[color=blue]
    > then in the main i creat an array of the above class
    >
    > Q[] Array_Q = new Q[55]();[/color]

    That creates an array of references, all of which are null to start
    with. It doesn't create a single instance of Q. You need to change the
    element to refer to a valid instance before doing anything else.
    [color=blue]
    > then i tried to fill the elemnets with a values
    >
    > int i,j;
    > for (i=0 ; i<=10 ; i++)
    > {
    > for (j=i;j<=10;j++)[/color]

    Put in: Array_Q[i] = new Q(); and it will work
    [color=blue]
    > Array_Q[i].A = i; //<== here is the error
    > Array_Q[i].B = j;
    > }[/color]

    <snip>

    It's very important to know the difference between reference types and
    value types - if Q had been a value type, it would have worked. See
    http://www.pobox.com/~skeet/csharp/parameters.html and
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • sh

      #3
      Re: chnage the Array Element

      Ok .. thank you Mr.Jon Skeet
      I will try it ... and I will see

      Thank you agin[color=blue]
      >-----Original Message-----
      >sh <hshgh@yahoo.co m> wrote:[color=green]
      >> then in the main i creat an array of the above class
      >>
      >> Q[] Array_Q = new Q[55]();[/color]
      >
      >That creates an array of references, all of which are[/color]
      null to start[color=blue]
      >with. It doesn't create a single instance of Q. You need[/color]
      to change the[color=blue]
      >element to refer to a valid instance before doing[/color]
      anything else.[color=blue]
      >[color=green]
      >> then i tried to fill the elemnets with a values
      >>
      >> int i,j;
      >> for (i=0 ; i<=10 ; i++)
      >> {
      >> for (j=i;j<=10;j++)[/color]
      >
      >Put in: Array_Q[i] = new Q(); and it will work
      >[color=green]
      >> Array_Q[i].A = i; //<== here is the error
      >> Array_Q[i].B = j;
      >> }[/color]
      >
      ><snip>
      >
      >It's very important to know the difference between[/color]
      reference types and[color=blue]
      >value types - if Q had been a value type, it would have[/color]
      worked. See[color=blue]
      >http://www.pobox.com/~skeet/csharp/parameters.html and
      >http://www.pobox.com/~skeet/csharp/memory.html
      >
      >--
      >Jon Skeet - <skeet@pobox.co m>
      >http://www.pobox.com/~skeet/
      >If replying to the group, please do not mail me too
      >.
      >[/color]

      Comment

      • David Lau

        #4
        Re: chnage the Array Element

        Thanks, it is very useful for the beginner like me
        "sh" <hshgh@yahoo.co m> wrote in message
        news:036a01c35f 12$b5b5dab0$a30 1280a@phx.gbl.. .[color=blue]
        > Ok .. thank you Mr.Jon Skeet
        > I will try it ... and I will see
        >
        > Thank you agin[color=green]
        > >-----Original Message-----
        > >sh <hshgh@yahoo.co m> wrote:[color=darkred]
        > >> then in the main i creat an array of the above class
        > >>
        > >> Q[] Array_Q = new Q[55]();[/color]
        > >
        > >That creates an array of references, all of which are[/color]
        > null to start[color=green]
        > >with. It doesn't create a single instance of Q. You need[/color]
        > to change the[color=green]
        > >element to refer to a valid instance before doing[/color]
        > anything else.[color=green]
        > >[color=darkred]
        > >> then i tried to fill the elemnets with a values
        > >>
        > >> int i,j;
        > >> for (i=0 ; i<=10 ; i++)
        > >> {
        > >> for (j=i;j<=10;j++)[/color]
        > >
        > >Put in: Array_Q[i] = new Q(); and it will work
        > >[color=darkred]
        > >> Array_Q[i].A = i; //<== here is the error
        > >> Array_Q[i].B = j;
        > >> }[/color]
        > >
        > ><snip>
        > >
        > >It's very important to know the difference between[/color]
        > reference types and[color=green]
        > >value types - if Q had been a value type, it would have[/color]
        > worked. See[color=green]
        > >http://www.pobox.com/~skeet/csharp/parameters.html and
        > >http://www.pobox.com/~skeet/csharp/memory.html
        > >
        > >--
        > >Jon Skeet - <skeet@pobox.co m>
        > >http://www.pobox.com/~skeet/
        > >If replying to the group, please do not mail me too
        > >.
        > >[/color][/color]


        Comment

        Working...