Resolving a variable class name

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

    #1

    Resolving a variable class name

    Having this:

    public class MyClass : UserControls
    {
    public int number=25;

    public MyClass {
    new TestClass(this) ;
    }
    }

    This Works!!!!!!!!!! !!!!!!!!!!
    public class TestClass {
    public TestClass(MyCla ss myclass) {
    Console.Write(m yclass.number);
    }
    }

    This doesn't Works :(
    public class TestClass {
    public TestClass(UserC ontrol myclass) {
    Console.Write(m yclass.number);
    }
    }

    How can i resolve the acces to "MyClass.number " if MyClass is a variable name?

  • Cole Breidenbach

    #2
    Resolving a variable class name


    In your second example, try the key word "is" to determine
    the type of class passed in. If the object is a MyClass,
    cast the object to a MyClass and access the Number
    property.

    Example:
    if(myclass is MyClass)
    {
    MyClass myclass2 = (MyClass)myclas s
    Console.WriteLi ne(myclass.Numb er)
    }
    [color=blue]
    >-----Original Message-----
    >Having this:
    >
    >public class MyClass : UserControls
    >{
    > public int number=25;
    >
    > public MyClass {
    > new TestClass(this) ;
    > }
    >}
    >
    >This Works!!!!!!!!!! !!!!!!!!!!
    >public class TestClass {
    > public TestClass(MyCla ss myclass) {
    > Console.Write(m yclass.number);
    > }
    >}
    >
    >This doesn't Works :(
    >public class TestClass {
    > public TestClass(UserC ontrol myclass) {
    > Console.Write(m yclass.number);
    > }
    >}
    >
    >How can i resolve the acces to "MyClass.number " if[/color]
    MyClass is a variable name?[color=blue]
    >[/color]

    Comment

    • Cole Breidenbach

      #3
      Resolving a variable class name


      Reposting:

      In your second example, try the key word "is" to determine
      the type of class passed in. If the object is a MyClass,
      cast the object to a MyClass and access the Number
      property.

      Example:
      if(myclass is MyClass)
      {
      MyClass myclass2 = (MyClass)myclas s
      Console.WriteLi ne(myclass2.Num ber)
      }
      [color=blue]
      >-----Original Message-----
      >
      >In your second example, try the key word "is" to[/color]
      determine[color=blue]
      >the type of class passed in. If the object is a MyClass,
      >cast the object to a MyClass and access the Number
      >property.
      >
      >Example:
      >if(myclass is MyClass)
      >{
      > MyClass myclass2 = (MyClass)myclas s
      > Console.WriteLi ne(myclass.Numb er)
      >}
      >[color=green]
      >>-----Original Message-----
      >>Having this:
      >>
      >>public class MyClass : UserControls
      >>{
      >> public int number=25;
      >>
      >> public MyClass {
      >> new TestClass(this) ;
      >> }
      >>}
      >>
      >>This Works!!!!!!!!!! !!!!!!!!!!
      >>public class TestClass {
      >> public TestClass(MyCla ss myclass) {
      >> Console.Write(m yclass.number);
      >> }
      >>}
      >>
      >>This doesn't Works :(
      >>public class TestClass {
      >> public TestClass(UserC ontrol myclass) {
      >> Console.Write(m yclass.number);
      >> }
      >>}
      >>
      >>How can i resolve the acces to "MyClass.number " if[/color]
      >MyClass is a variable name?[color=green]
      >>[/color]
      >.
      >[/color]

      Comment

      • bB

        #4
        Re: Resolving a variable class name

        You can't access the public instance variable 'number' in your second example, because instances of UserControl do not have a public variable 'number'. MyClass is a type of UserControl, but UserControl is not a type of MyClass.

        Use:

        myclass.GetType () == typeof(MyClass)

        to check if the supplied argument is an instance of MyClass, and then cast it to a MyClass before trying to access number:

        if (myclass.GetTyp e() == typeof(MyClass) )
        {
        Console.WriteLi ne("number = " + ((MyClass)mycla ss).number);
        }

        Tim.
        "~toki" <pedorro77@hotm ail.com> wrote in message news:udygt8PlDH A.3024@tk2msftn gp13.phx.gbl...
        Having this:

        public class MyClass : UserControls
        {
        public int number=25;

        public MyClass {
        new TestClass(this) ;
        }
        }

        This Works!!!!!!!!!! !!!!!!!!!!
        public class TestClass {
        public TestClass(MyCla ss myclass) {
        Console.Write(m yclass.number);
        }
        }

        This doesn't Works :(
        public class TestClass {
        public TestClass(UserC ontrol myclass) {
        Console.Write(m yclass.number);
        }
        }

        How can i resolve the acces to "MyClass.number " if MyClass is a variable name?

        Comment

        • ~toki

          #5
          Re: Resolving a variable class name

          Thanks Cole,

          the only problem is that "MyClass" can be "Some", "thing", "anyword" or
          anything else.



          "Cole Breidenbach" <cybreid2000@ya hoo.com> escribió en el mensaje
          news:041901c395 03$4fd167b0$a40 1280a@phx.gbl.. .[color=blue]
          >
          > Reposting:
          >
          > In your second example, try the key word "is" to determine
          > the type of class passed in. If the object is a MyClass,
          > cast the object to a MyClass and access the Number
          > property.
          >
          > Example:
          > if(myclass is MyClass)
          > {
          > MyClass myclass2 = (MyClass)myclas s
          > Console.WriteLi ne(myclass2.Num ber)
          > }
          >[color=green]
          > >-----Original Message-----
          > >
          > >In your second example, try the key word "is" to[/color]
          > determine[color=green]
          > >the type of class passed in. If the object is a MyClass,
          > >cast the object to a MyClass and access the Number
          > >property.
          > >
          > >Example:
          > >if(myclass is MyClass)
          > >{
          > > MyClass myclass2 = (MyClass)myclas s
          > > Console.WriteLi ne(myclass.Numb er)
          > >}
          > >[color=darkred]
          > >>-----Original Message-----
          > >>Having this:
          > >>
          > >>public class MyClass : UserControls
          > >>{
          > >> public int number=25;
          > >>
          > >> public MyClass {
          > >> new TestClass(this) ;
          > >> }
          > >>}
          > >>
          > >>This Works!!!!!!!!!! !!!!!!!!!!
          > >>public class TestClass {
          > >> public TestClass(MyCla ss myclass) {
          > >> Console.Write(m yclass.number);
          > >> }
          > >>}
          > >>
          > >>This doesn't Works :(
          > >>public class TestClass {
          > >> public TestClass(UserC ontrol myclass) {
          > >> Console.Write(m yclass.number);
          > >> }
          > >>}
          > >>
          > >>How can i resolve the acces to "MyClass.number " if[/color]
          > >MyClass is a variable name?[color=darkred]
          > >>[/color]
          > >.
          > >[/color][/color]


          Comment

          • ~toki

            #6
            Re: Resolving a variable class name

            I need to get controls of the UserControl (or Forms) that call to my class (Screen)

            ---
            public Screen(MyClass parent)
            {
            this.parent = parent;

            System.Collecti ons.IEnumerator enume = parent.Controls .GetEnumerator( );
            int i = 0;
            while(enume.Mov eNext() && ((Control)enume .Current).GetTy pe().Name == "Button" ) i++;
            ....

            The problem is that the name "MyClass" is variable.
            The class Screen doesn't extend anything.
            Maybe, one solution is to extend it as UserControl and use the ParentForm property and do ParentForm.Cont rols.GetEnumera tor();

            But,,, must have a way to do it without extend it.


            "bB" <tim@aterminal. free-serve.co.uk.don tspamme> escribió en el mensaje news:uXUtQPQlDH A.988@TK2MSFTNG P10.phx.gbl...
            You can't access the public instance variable 'number' in your second example, because instances of UserControl do not have a public variable 'number'. MyClass is a type of UserControl, but UserControl is not a type of MyClass.

            Use:

            myclass.GetType () == typeof(MyClass)

            to check if the supplied argument is an instance of MyClass, and then cast it to a MyClass before trying to access number:

            if (myclass.GetTyp e() == typeof(MyClass) )
            {
            Console.WriteLi ne("number = " + ((MyClass)mycla ss).number);
            }

            Tim.
            "~toki" <pedorro77@hotm ail.com> wrote in message news:udygt8PlDH A.3024@tk2msftn gp13.phx.gbl...
            Having this:

            public class MyClass : UserControls
            {
            public int number=25;

            public MyClass {
            new TestClass(this) ;
            }
            }

            This Works!!!!!!!!!! !!!!!!!!!!
            public class TestClass {
            public TestClass(MyCla ss myclass) {
            Console.Write(m yclass.number);
            }
            }

            This doesn't Works :(
            public class TestClass {
            public TestClass(UserC ontrol myclass) {
            Console.Write(m yclass.number);
            }
            }

            How can i resolve the acces to "MyClass.number " if MyClass is a variable name?

            Comment

            • Girish Bharadwaj

              #7
              Re: Resolving a variable class name

              ~toki wrote:
              [color=blue]
              > Thanks Cole,
              >
              > the only problem is that "MyClass" can be "Some", "thing", "anyword" or
              > anything else.
              >
              >
              >
              > "Cole Breidenbach" <cybreid2000@ya hoo.com> escribió en el mensaje
              > news:041901c395 03$4fd167b0$a40 1280a@phx.gbl.. .
              >[color=green]
              >>Reposting:
              >>
              >>In your second example, try the key word "is" to determine
              >>the type of class passed in. If the object is a MyClass,
              >>cast the object to a MyClass and access the Number
              >>property.
              >>
              >>Example:
              >>if(myclass is MyClass)
              >>{
              >> MyClass myclass2 = (MyClass)myclas s
              >> Console.WriteLi ne(myclass2.Num ber)
              >>}
              >>
              >>[color=darkred]
              >>>-----Original Message-----
              >>>
              >>>In your second example, try the key word "is" to[/color]
              >>
              >>determine
              >>[color=darkred]
              >>>the type of class passed in. If the object is a MyClass,
              >>>cast the object to a MyClass and access the Number
              >>>property.
              >>>
              >>>Example:
              >>>if(myclass is MyClass)
              >>>{
              >>> MyClass myclass2 = (MyClass)myclas s
              >>> Console.WriteLi ne(myclass.Numb er)
              >>>}
              >>>
              >>>
              >>>>-----Original Message-----
              >>>>Having this:
              >>>>
              >>>>public class MyClass : UserControls
              >>>>{
              >>>> public int number=25;
              >>>>
              >>>> public MyClass {
              >>>> new TestClass(this) ;
              >>>> }
              >>>>}
              >>>>
              >>>>This Works!!!!!!!!!! !!!!!!!!!!
              >>>>public class TestClass {
              >>>> public TestClass(MyCla ss myclass) {
              >>>> Console.Write(m yclass.number);
              >>>> }
              >>>>}
              >>>>
              >>>>This doesn't Works :(
              >>>>public class TestClass {
              >>>> public TestClass(UserC ontrol myclass) {
              >>>> Console.Write(m yclass.number);
              >>>> }
              >>>>}
              >>>>
              >>>>How can i resolve the acces to "MyClass.number " if
              >>>
              >>>MyClass is a variable name?
              >>>
              >>>.
              >>>[/color][/color]
              >
              >
              >[/color]


              While I understand what you are trying to do, UserControl is the wrong
              *class* to pass. What you can do is either
              1. have a base interface... INeedTheseThing s.. Which just provides the
              required properties as you need (such as number et al).
              Each class that you are implementing that exposes that *number* property
              can be queried as

              INeedTheseThing s intt = myclass as INeedTheseThing s;
              if (intt != null ) {
              intt.number;...
              }

              So on..
              2. Otherwise, you can just pass the *Type* of the member variable itself
              as an argument and use reflection to cast the given type to the right
              *thinger*.

              Anyway, I think you probably need to explain the problem in detail for
              me/or someone else to help you out .. :)
              --
              Girish Bharadwaj

              Comment

              • ~toki

                #8
                Re: Resolving a variable class name

                I need to get controls of the UserControl (or Forms) that call to my class
                (Screen)

                ---
                public Screen(MyClass parent)
                {
                this.parent = parent;

                System.Collecti ons.IEnumerator enume = parent.Controls .GetEnumerator( );
                int i = 0;
                while(enume.Mov eNext() && ((Control)enume .Current).GetTy pe().Name ==
                "Button" ) i++;
                ....

                I wonder if i can only throw my usercontrol (Screen) on a form and get the
                controls.
                Ideas are welcome. (Maybe if i extend Screen as a usercontrol... but how can
                hidden all the properties (height, etc) )

                I hope that the question be explicit (i talk spanish)

                Item 2 sound good any reference to do it?


                "Girish Bharadwaj" <girishb@nowher e> escribió en el mensaje
                news:eOssRt0lDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
                > ~toki wrote:
                >
                >
                >
                > While I understand what you are trying to do, UserControl is the wrong
                > *class* to pass. What you can do is either
                > 1. have a base interface... INeedTheseThing s.. Which just provides the
                > required properties as you need (such as number et al).
                > Each class that you are implementing that exposes that *number* property
                > can be queried as
                >
                > INeedTheseThing s intt = myclass as INeedTheseThing s;
                > if (intt != null ) {
                > intt.number;...
                > }
                >
                > So on..
                > 2. Otherwise, you can just pass the *Type* of the member variable itself
                > as an argument and use reflection to cast the given type to the right
                > *thinger*.
                >
                > Anyway, I think you probably need to explain the problem in detail for
                > me/or someone else to help you out .. :)
                > --
                > Girish Bharadwaj
                >[/color]


                Comment

                Working...