473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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?

Nov 15 '05 #1
7 1618

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)
}
-----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?

Nov 15 '05 #2

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)
}
-----Original Message-----

In your second example, try the key word "is" to determinethe 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?

.

Nov 15 '05 #3
bB
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" <pe*******@hotmail.com> wrote in message news:ud**************@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?

Nov 15 '05 #4
Thanks Cole,

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

"Cole Breidenbach" <cy*********@yahoo.com> escribió en el mensaje
news:04****************************@phx.gbl...

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)
}
-----Original Message-----

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)
}
-----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?

.

Nov 15 '05 #5
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" <ti*@aterminal.free-serve.co.uk.dontspamme> escribió en el mensaje news:uX*************@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" <pe*******@hotmail.com> wrote in message news:ud**************@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?

Nov 15 '05 #6
~toki wrote:
Thanks Cole,

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

"Cole Breidenbach" <cy*********@yahoo.com> escribió en el mensaje
news:04****************************@phx.gbl...
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)
}

-----Original Message-----

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)
}
-----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?

.


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

Nov 15 '05 #7
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:eO**************@TK2MSFTNGP11.phx.gbl...
~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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Bowling | last post by:
I'm trying to get the day of month with: int dayofmonth; dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH); and I get the compile time error of saying get(int) is a not-static method. Is...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
0
by: Stephen M | last post by:
Hi, I feel like i'm being a little dense: I have created an HttpModule to do some security checking for a webservice. The HttpModule hijacks any request to the webservice, and gets passed a...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
by: RJDev | last post by:
Hi, Is there anyone who can help me in resolving the Application Name of my IIS Aaplication. I want to check the application name to see what configuration i need to use because multiple virtual...
8
by: nephish | last post by:
hey there, is this ok? class MyClass { var $start; var $finish; function MyClass($start, $finish) { $this->start = $start; $this->finish=$finish,
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
9
by: =?ISO-8859-1?Q?Janne_H=E4rk=F6nen?= | last post by:
Hello, Is there a simple way to resolve declaring class of a method at runtime ? Consider this simple example: $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on cygwin Type...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.