Resolving a variable class name

  • Thread starter Thread starter ~toki
  • Start date Start date
T

~toki

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?
 
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)
}
 
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)
}
 
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.
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?
 
Thanks Cole,

the only problem is that "MyClass" can be "Some", "thing", "anyword" or
anything else.
 
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" <[email protected]> escribió en el mensaje 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.
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 said:
Thanks Cole,

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


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 .. :)
 
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?
 
Back
Top