Connecting Tech Pros Worldwide Help | Site Map

Resolving a variable class name

~toki
Guest
 
Posts: n/a
#1: Nov 15 '05
Having this:

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

public MyClass {
new TestClass(this);
}
}

This Works!!!!!!!!!!!!!!!!!!!!
public class TestClass {
public TestClass(MyClass myclass) {
Console.Write(myclass.number);
}
}

This doesn't Works :(
public class TestClass {
public TestClass(UserControl myclass) {
Console.Write(myclass.number);
}
}

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

Cole Breidenbach
Guest
 
Posts: n/a
#2: Nov 15 '05

re: 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)myclass
Console.WriteLine(myclass.Number)
}
[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(MyClass myclass) {
> Console.Write(myclass.number);
> }
>}
>
>This doesn't Works :(
>public class TestClass {
> public TestClass(UserControl myclass) {
> Console.Write(myclass.number);
> }
>}
>
>How can i resolve the acces to "MyClass.number" if[/color]
MyClass is a variable name?[color=blue]
>[/color]
Cole Breidenbach
Guest
 
Posts: n/a
#3: Nov 15 '05

re: 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)myclass
Console.WriteLine(myclass2.Number)
}
[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)myclass
> Console.WriteLine(myclass.Number)
>}
>[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(MyClass myclass) {
>> Console.Write(myclass.number);
>> }
>>}
>>
>>This doesn't Works :(
>>public class TestClass {
>> public TestClass(UserControl myclass) {
>> Console.Write(myclass.number);
>> }
>>}
>>
>>How can i resolve the acces to "MyClass.number" if[/color]
>MyClass is a variable name?[color=green]
>>[/color]
>.
>[/color]
bB
Guest
 
Posts: n/a
#4: Nov 15 '05

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.GetType() == typeof(MyClass))
{
Console.WriteLine("number = " + ((MyClass)myclass).number);
}

Tim.
"~toki" <pedorro77@hotmail.com> wrote in message news:udygt8PlDHA.3024@tk2msftngp13.phx.gbl...
Having this:

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

public MyClass {
new TestClass(this);
}
}

This Works!!!!!!!!!!!!!!!!!!!!
public class TestClass {
public TestClass(MyClass myclass) {
Console.Write(myclass.number);
}
}

This doesn't Works :(
public class TestClass {
public TestClass(UserControl myclass) {
Console.Write(myclass.number);
}
}

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

~toki
Guest
 
Posts: n/a
#5: Nov 15 '05

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@yahoo.com> escribió en el mensaje
news:041901c39503$4fd167b0$a401280a@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)myclass
> Console.WriteLine(myclass2.Number)
> }
>[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)myclass
> > Console.WriteLine(myclass.Number)
> >}
> >[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(MyClass myclass) {
> >> Console.Write(myclass.number);
> >> }
> >>}
> >>
> >>This doesn't Works :(
> >>public class TestClass {
> >> public TestClass(UserControl myclass) {
> >> Console.Write(myclass.number);
> >> }
> >>}
> >>
> >>How can i resolve the acces to "MyClass.number" if[/color]
> >MyClass is a variable name?[color=darkred]
> >>[/color]
> >.
> >[/color][/color]


~toki
Guest
 
Posts: n/a
#6: Nov 15 '05

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.Collections.IEnumerator enume = parent.Controls.GetEnumerator();
int i = 0;
while(enume.MoveNext() && ((Control)enume.Current).GetType().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.Controls.GetEnumerator();

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


"bB" <tim@aterminal.free-serve.co.uk.dontspamme> escribió en el mensaje news:uXUtQPQlDHA.988@TK2MSFTNGP10.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.GetType() == typeof(MyClass))
{
Console.WriteLine("number = " + ((MyClass)myclass).number);
}

Tim.
"~toki" <pedorro77@hotmail.com> wrote in message news:udygt8PlDHA.3024@tk2msftngp13.phx.gbl...
Having this:

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

public MyClass {
new TestClass(this);
}
}

This Works!!!!!!!!!!!!!!!!!!!!
public class TestClass {
public TestClass(MyClass myclass) {
Console.Write(myclass.number);
}
}

This doesn't Works :(
public class TestClass {
public TestClass(UserControl myclass) {
Console.Write(myclass.number);
}
}

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

Girish Bharadwaj
Guest
 
Posts: n/a
#7: Nov 15 '05

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@yahoo.com> escribió en el mensaje
> news:041901c39503$4fd167b0$a401280a@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)myclass
>> Console.WriteLine(myclass2.Number)
>>}
>>
>>[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)myclass
>>> Console.WriteLine(myclass.Number)
>>>}
>>>
>>>
>>>>-----Original Message-----
>>>>Having this:
>>>>
>>>>public class MyClass : UserControls
>>>>{
>>>> public int number=25;
>>>>
>>>> public MyClass {
>>>> new TestClass(this);
>>>> }
>>>>}
>>>>
>>>>This Works!!!!!!!!!!!!!!!!!!!!
>>>>public class TestClass {
>>>> public TestClass(MyClass myclass) {
>>>> Console.Write(myclass.number);
>>>> }
>>>>}
>>>>
>>>>This doesn't Works :(
>>>>public class TestClass {
>>>> public TestClass(UserControl myclass) {
>>>> Console.Write(myclass.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... INeedTheseThings.. 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

INeedTheseThings intt = myclass as INeedTheseThings;
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

~toki
Guest
 
Posts: n/a
#8: Nov 15 '05

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.Collections.IEnumerator enume = parent.Controls.GetEnumerator();
int i = 0;
while(enume.MoveNext() && ((Control)enume.Current).GetType().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@nowhere> escribió en el mensaje
news:eOssRt0lDHA.2676@TK2MSFTNGP11.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... INeedTheseThings.. 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
>
> INeedTheseThings intt = myclass as INeedTheseThings;
> 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]


Closed Thread