Coding Problem, Help Needed

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

    Coding Problem, Help Needed

    Hello to ALL,

    I'll explain my problem by pseudo code

    class A
    {
    int x;
    char y;
    string z;
    }

    class B
    {
    GetVariable(int i)
    {
    A a = new A();
    //Now I want to return the ith varaible
    //of class A
    }
    }

    class C
    {
    GetVar()
    {
    B b = new B();
    b.GetVariable(0 ) //it shud return x
    b.GetVariable(1 ) //it shud return y
    b.GetVariable(2 ) //it shud return z
    }
    }

    Now how do I implement the GetVariable(int i), in C#
    The reason I have class B is because there are lot's of classes
    like class A, and i don't want to edit them all.

    Any Suggestions...

    Thanks In advance...
    Cheers...

    Bye

  • Pipo

    #2
    Re: Coding Problem, Help Needed

    You can use an interface.

    "batista" <saad_theleo@ya hoo.com> wrote in message
    news:1127138928 .544915.57850@g 14g2000cwa.goog legroups.com...[color=blue]
    > Hello to ALL,
    >
    > I'll explain my problem by pseudo code
    >
    > class A
    > {
    > int x;
    > char y;
    > string z;
    > }
    >
    > class B
    > {
    > GetVariable(int i)
    > {
    > A a = new A();
    > //Now I want to return the ith varaible
    > //of class A
    > }
    > }
    >
    > class C
    > {
    > GetVar()
    > {
    > B b = new B();
    > b.GetVariable(0 ) //it shud return x
    > b.GetVariable(1 ) //it shud return y
    > b.GetVariable(2 ) //it shud return z
    > }
    > }
    >
    > Now how do I implement the GetVariable(int i), in C#
    > The reason I have class B is because there are lot's of classes
    > like class A, and i don't want to edit them all.
    >
    > Any Suggestions...
    >
    > Thanks In advance...
    > Cheers...
    >
    > Bye
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Coding Problem, Help Needed

      batista,

      You will have to make sure that the original class declares GetVariable
      as virtual, so that it can be overridden by derived classes. If it is not,
      then there is little you could do (you could shadow it with the new keyword,
      but that is evil).

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "batista" <saad_theleo@ya hoo.com> wrote in message
      news:1127138928 .544915.57850@g 14g2000cwa.goog legroups.com...[color=blue]
      > Hello to ALL,
      >
      > I'll explain my problem by pseudo code
      >
      > class A
      > {
      > int x;
      > char y;
      > string z;
      > }
      >
      > class B
      > {
      > GetVariable(int i)
      > {
      > A a = new A();
      > //Now I want to return the ith varaible
      > //of class A
      > }
      > }
      >
      > class C
      > {
      > GetVar()
      > {
      > B b = new B();
      > b.GetVariable(0 ) //it shud return x
      > b.GetVariable(1 ) //it shud return y
      > b.GetVariable(2 ) //it shud return z
      > }
      > }
      >
      > Now how do I implement the GetVariable(int i), in C#
      > The reason I have class B is because there are lot's of classes
      > like class A, and i don't want to edit them all.
      >
      > Any Suggestions...
      >
      > Thanks In advance...
      > Cheers...
      >
      > Bye
      >[/color]


      Comment

      • batista

        #4
        Re: Coding Problem, Help Needed

        Thanks For Replies,

        Pipo and Nicholas Paldino,

        Nicholas Paldino, I understand that I'll need to declare GetVariable()
        virtual in all classes,
        but how do i implement a function like this.

        I mean how do i number the variables of a class, so that i can ask for
        ith variable in the class.

        Plz help me on that...
        Thanks In Advance...
        Cheers...

        Bye

        Comment

        • Dan Neely

          #5
          Re: Coding Problem, Help Needed



          "batista" wrote:[color=blue]
          > I mean how do i number the variables of a class, so that i can ask for
          > ith variable in the class.[/color]

          AFAIK you'll have to hardcode it. The compiler is free to reorder
          variables inside a data structure to optimize performance. ie putting 4
          chars sequentially so they all fell in the same 32bit chunk when on the
          memory bus.

          Comment

          • Deviant

            #6
            Re: Coding Problem, Help Needed

            You could use Reflection to enumerate the fields in the class (although
            they would have to be public, which seems to defeat the object).

            The easiest solution would be to replace all the variables in Class A
            with an object array..


            batista wrote:[color=blue]
            > Thanks For Replies,
            >
            > Pipo and Nicholas Paldino,
            >
            > Nicholas Paldino, I understand that I'll need to declare GetVariable()
            > virtual in all classes,
            > but how do i implement a function like this.
            >
            > I mean how do i number the variables of a class, so that i can ask for
            > ith variable in the class.
            >
            > Plz help me on that...
            > Thanks In Advance...
            > Cheers...
            >
            > Bye[/color]

            Comment

            • justncase80

              #7
              Re: Coding Problem, Help Needed

              this is ugly but in desperation you could just do somethign like this:

              object GetVariable(int x)
              {
              switch(x)
              {
              case(0):
              return x;
              case(1):
              return y;
              case(2):
              return z;
              default: return null;
              }
              }

              Comment

              Working...