473,324 Members | 2,166 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,324 software developers and data experts.

usable of typeof

Hello,

I have made this code in a class

private static Type[] Etats = new Type[]
{
typeof(Transition1),
typeof(Transition2),
typeof(Transition3)
}

i with with an indice of a table, calling a static method.
for helping me, all my transactions are inherited about an Transaction
object.

I'll wish like to do this :

Transition myTransitionGenerique;
(myTransitionGenerique as Etats[Idx]).MaProprieteStatique();

Do you know if it is possible ?
I'm using this for making an State Machin without create all transition
object.

Thanks

Jun 22 '06 #1
9 1180
la**********@gmail.com wrote:
Hello,

I have made this code in a class

private static Type[] Etats = new Type[]
{
typeof(Transition1),
typeof(Transition2),
typeof(Transition3)
}

i with with an indice of a table, calling a static method.
for helping me, all my transactions are inherited about an Transaction
object.

I'll wish like to do this :

Transition myTransitionGenerique;
(myTransitionGenerique as Etats[Idx]).MaProprieteStatique();

Do you know if it is possible ?
I'm using this for making an State Machin without create all transition
object.

Thanks


Hi Laurent,

The 'as' operator is a compile-time construct. It won't be of any help
here. Does 'MaProprieteStatique' get defined in your base 'Transition'
class? If so, you don't need to cast at all. You just need to instantiate
your object like this:

///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

Hope this helps,
-- Tom Spink
Jun 22 '06 #2
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)
///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

so your code won't do

Laurent,

Your code also won't work because typeof gives the Type object for the
actuall type and as such it doesn't have these static properties, but you
are on the right track.
What you need to do is to use a reflection and read the static proeprties
values using the Type objects
it is going to be something like

Transition myTransitionGenerique;
myTransitionGenerique =
Etats[Idx]).GetProperty("MaProprieteStatique").GetValue(null ,null) as
Transition;

If MaProprieteStatique is declared on the level of a base class you should
probably use GetProperty overload that accept BindingFlags and pass
BindingFlags.FlattenHierarchy.
--
HTH
Stoitcho Goutsev (100)
"Tom Spink" <ts****@gmail.com> wrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl... la**********@gmail.com wrote:
Hello,

I have made this code in a class

private static Type[] Etats = new Type[]
{
typeof(Transition1),
typeof(Transition2),
typeof(Transition3)
}

i with with an indice of a table, calling a static method.
for helping me, all my transactions are inherited about an Transaction
object.

I'll wish like to do this :

Transition myTransitionGenerique;
(myTransitionGenerique as Etats[Idx]).MaProprieteStatique();

Do you know if it is possible ?
I'm using this for making an State Machin without create all transition
object.

Thanks


Hi Laurent,

The 'as' operator is a compile-time construct. It won't be of any help
here. Does 'MaProprieteStatique' get defined in your base 'Transition'
class? If so, you don't need to cast at all. You just need to
instantiate
your object like this:

///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

Hope this helps,
-- Tom Spink

Jun 22 '06 #3
Stoitcho Goutsev (100) wrote:
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)
///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

so your code won't do

Laurent,

Your code also won't work because typeof gives the Type object for the
actuall type and as such it doesn't have these static properties, but you
are on the right track.
What you need to do is to use a reflection and read the static proeprties
values using the Type objects
it is going to be something like

Transition myTransitionGenerique;
myTransitionGenerique =
Etats[Idx]).GetProperty("MaProprieteStatique").GetValue(null ,null) as
Transition;

If MaProprieteStatique is declared on the level of a base class you should
probably use GetProperty overload that accept BindingFlags and pass
BindingFlags.FlattenHierarchy.


Hi,
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)


By his declaration, I assumed it was a method. Properties do not have
parentheses on the end, as per his original code. Still, you are correct
that static methods/properties are inaccessible by instance.

-- Tom Spink
Jun 22 '06 #4
"Tom Spink" <ts****@gmail.com> wrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl...
la**********@gmail.com wrote: The 'as' operator is a compile-time construct.


That's not true, is it? My understanding is that 'as' interrogates the
actual type of the object referred to by the variable, which is a runtime
operation.

///ark
Jun 22 '06 #5

Mark Wilden wrote:
"Tom Spink" <ts****@gmail.com> wrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl...
la**********@gmail.com wrote:

The 'as' operator is a compile-time construct.


That's not true, is it? My understanding is that 'as' interrogates the
actual type of the object referred to by the variable, which is a runtime
operation.


Left side evaluated at run-time, right-side evaluated at compile
time.

some what analogous to "If (x == 5)" where you can change the value
of "x" at run-time, but you can't change the value of "5".

Jun 22 '06 #6
Mark Wilden wrote:
"Tom Spink" <ts****@gmail.com> wrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl...
la**********@gmail.com wrote:

The 'as' operator is a compile-time construct.


That's not true, is it? My understanding is that 'as' interrogates the
actual type of the object referred to by the variable, which is a runtime
operation.

///ark


Hi Mark,

The 'as' operator does actually correspond to the MSIL instruction isinst,
which pops and tests the current object on the evaluation stack to see if
it's an instance of the supplied type, pushing null onto the stack if it
isn't and pushing the casted object to the evaluation stack.

You are totally correct, this is done at runtime, as it's dynamic type
checking, I guess I could have worded my response differently. I guess
what I meant to say is that casting is not dynamic. You cannot cast to a
type at runtime, where the type you want to cast to is dynamic.

Am I making any sense?

-- Tom Spink
Jun 22 '06 #7
"Tom Spink" <ts****@gmail.com> wrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...

You are totally correct, this is done at runtime, as it's dynamic type
checking, I guess I could have worded my response differently. I guess
what I meant to say is that casting is not dynamic. You cannot cast to a
type at runtime, where the type you want to cast to is dynamic.

Am I making any sense?


Yes, thanks. Your and James's responses cleared up my confusion.

///ark
Jun 22 '06 #8
The whole snippet is a mess, but as far as my french goes 'Propriete
Statique' means 'Static Property'
--
Stoitcho Goutsev (100)

"Tom Spink" <ts****@gmail.com> wrote in message
news:eq**************@TK2MSFTNGP05.phx.gbl...
Stoitcho Goutsev (100) wrote:
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)
///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///


so your code won't do

Laurent,

Your code also won't work because typeof gives the Type object for the
actuall type and as such it doesn't have these static properties, but you
are on the right track.
What you need to do is to use a reflection and read the static proeprties
values using the Type objects
it is going to be something like

Transition myTransitionGenerique;
myTransitionGenerique =
Etats[Idx]).GetProperty("MaProprieteStatique").GetValue(null ,null) as
Transition;

If MaProprieteStatique is declared on the level of a base class you
should
probably use GetProperty overload that accept BindingFlags and pass
BindingFlags.FlattenHierarchy.


Hi,
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)


By his declaration, I assumed it was a method. Properties do not have
parentheses on the end, as per his original code. Still, you are correct
that static methods/properties are inaccessible by instance.

-- Tom Spink

Jun 22 '06 #9
<la**********@gmail.com> a écrit dans le message de news:
11**********************@b68g2000cwa.googlegroups. com...

| I have made this code in a class
|
| private static Type[] Etats = new Type[]
| {
| typeof(Transition1),
| typeof(Transition2),
| typeof(Transition3)
| }
|
| i with with an indice of a table, calling a static method.
| for helping me, all my transactions are inherited about an Transaction
| object.
|
| I'll wish like to do this :
|
| Transition myTransitionGenerique;
| (myTransitionGenerique as Etats[Idx]).MaProprieteStatique();
|
| Do you know if it is possible ?

Malheureusement, non.

Vous essayez de transformer une instance d'un objet vers une instance d'un
type et ce n'est pas possible.

Vous étiez développeur Delphi ? Là, vous avez les types « classes » qui
peuvent posséder les membres virtuelle, mais avec .NET, ni les propriétés,
ni les méthodes et ni les constructeurs statiques participent dans le
polymorphisme.

Joanna

--
Joanna Carter [TeamB]
Ingénieur conseil en logiciel
Jun 23 '06 #10

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

Similar topics

4
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
4
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' '...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool...
4
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.