473,386 Members | 1,752 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,386 software developers and data experts.

What's the typeof() function for?

Hi,

Would someone give me an example for the typeof() function? What's that
for?
Thanks for help.

Jason
Nov 17 '05 #1
13 6032


"Jason Huang" wrote:
Hi,

Would someone give me an example for the typeof() function? What's that
for?
Thanks for help.

Jason

Hi

if u need to use serilize the object u have to specify the type of that
object
that situation we need to use (typeof("Class Name"), Class object)

Regards
sambath R

Nov 17 '05 #2
Hi Jason,
typeof returns a Type instance that contains all of the type information
for a particular type, this is used for reflection where you want to look
inside a type to see maybe how many methods it has, the return type of a
method etc all dynamically. For example you could use the type information
to get all of the static methods of the string class like:
using System;
//using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(string);

MethodInfo[] methods = t.GetMethods();

foreach (MethodInfo method in methods)
{
if(method.IsStatic)
{
Console.WriteLine(method.Name);
}
}

Console.ReadLine();
}
}
}
This is a really quick and brief explanation, start researching more into
reflection to see the power of this information.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"Jason Huang" wrote:
Hi,

Would someone give me an example for the typeof() function? What's that
for?
Thanks for help.

Jason

Nov 17 '05 #3
Jason,

If you run through a collection with more types of objects and you want to
handle those than if they are not all derived from the same parent and you
only needs the derived methods/properties, than you need to know what type
of object you are handling.

Where in my opinion the collection of controls in a control is the nicest
sample, if you want to change all checkboxes than you need to know that the
type of the control is a checkbox.

I hope this helps,

Cor
Nov 17 '05 #4
// static stuff
private static int uniqueID= 0;
private static int GetUniqueID()
{
lock(typeof(TestStatic))
{
return uniqueID++; // returns zero at start
}
}

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
Cor Ligthert [MVP] <no************@planet.nl> wrote:
If you run through a collection with more types of objects and you want to
handle those than if they are not all derived from the same parent and you
only needs the derived methods/properties, than you need to know what type
of object you are handling.

Where in my opinion the collection of controls in a control is the nicest
sample, if you want to change all checkboxes than you need to know that the
type of the control is a checkbox.


But in that case, the best thing to use is "as" or "is":

CheckBox cb = foo as CheckBox;
if (cb != null)
{
...
}

or

if (foo is CheckBox)

typeof(...) is used when you need an expression which evaluates to
Type, eg for ArrayList.ToArray.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Jon,

Interesting, will you be so kind to explain this to me more, what is the
equivalent of this in C# (seriously not a challenge).

\\\
Private Sub SetAllCheckStateTrue(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
If TypeOf ctr Is CheckBox Then
DirectCast(ctr, CheckBox).CheckState = CheckState.Checked
SetAllCheckStateTrue(ctr)
End If
Next
End Sub
///

Thanks in advance.

Cor
"Jon Skeet [C# MVP]" <sk***@pobox.com> schreef in bericht
news:MP************************@msnews.microsoft.c om...
Cor Ligthert [MVP] <no************@planet.nl> wrote:
If you run through a collection with more types of objects and you want
to
handle those than if they are not all derived from the same parent and
you
only needs the derived methods/properties, than you need to know what
type
of object you are handling.

Where in my opinion the collection of controls in a control is the nicest
sample, if you want to change all checkboxes than you need to know that
the
type of the control is a checkbox.


But in that case, the best thing to use is "as" or "is":

CheckBox cb = foo as CheckBox;
if (cb != null)
{
...
}

or

if (foo is CheckBox)

typeof(...) is used when you need an expression which evaluates to
Type, eg for ArrayList.ToArray.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7
Jeff Louie <je********@yahoo.com> wrote:
// static stuff
private static int uniqueID= 0;
private static int GetUniqueID()
{
lock(typeof(TestStatic))
{
return uniqueID++; // returns zero at start
}
}


That should very rarely be used though. See
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Jon,

Not needed

private void SetAllCheckStateTrue(Control parentCtr)
{
foreach (Control ctr in parentCtr.Controls)
{
if (ctr is CheckBox)
{
((CheckBox)ctr).CheckState = CheckState.Checked;
SetAllCheckStateTrue(ctr);
}
}
}

:-)

Cor
Nov 17 '05 #9
Cor Ligthert [MVP] <no************@planet.nl> wrote:
Interesting, will you be so kind to explain this to me more, what is the
equivalent of this in C# (seriously not a challenge).

\\\
Private Sub SetAllCheckStateTrue(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
If TypeOf ctr Is CheckBox Then
DirectCast(ctr, CheckBox).CheckState = CheckState.Checked
SetAllCheckStateTrue(ctr)
End If
Next
End Sub
///


Well, the equivalent but without the extra cast cost is:

private SetAllCheckStateTrue (Control parentCtr)
{
foreach (Control ctr in parentCtr.Controls)
{
CheckBox cb = ctr as CheckBox;
if (cb != null)
{
cb.CheckState = CheckState.Checked;
SetAllCheckStateTrue(ctr);
}
}
}

I *suspect* you meant to put the recursive call outside the "if"
though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
Cor Ligthert [MVP] <no************@planet.nl> wrote:
Not needed

private void SetAllCheckStateTrue(Control parentCtr)
{
foreach (Control ctr in parentCtr.Controls)
{
if (ctr is CheckBox)
{
((CheckBox)ctr).CheckState = CheckState.Checked;
SetAllCheckStateTrue(ctr);
}
}
}


While that works, there's no need to effectively cast twice - once in
the "is" and once on the following line. Using "as" and then checking
for null is a bit more efficient and just as readable, so for patterns
like the above it's better. (I use direct casting when it's an error
for the reference to be anything other than the target type - I like an
appropriate exception to be generated immediately.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
Jon,

Completely clear,

Thanks,

Cor
Nov 17 '05 #12
Hi,

To get a reference to a Type using a class instead of an instance.

If you have an instance you use GetType() which return an instance of the
Type class. If cases that you do not have an instance you get a similar
result using typeof( Type).
Why you need for or how you use a Type is another matter, check MSDN or just
post back for further info.

by the way, typeof is an operator, not a function, remember that C# has no
"global" function any function( or method) should be declared inside a
class.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
Hi,

Would someone give me an example for the typeof() function? What's that
for?
Thanks for help.

Jason

Nov 17 '05 #13
Not to worry. I linked to your site!
http://www.geocities.com/jeff_louie/OOP/oop25.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #14

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

Similar topics

10
by: InvisibleMan | last post by:
Hi, Thanks for any help in advance... Okay, I have the JS listed below that calls for the display of the (DIV) tag... cookie function not included, as don't feel its necessary but you'll get the...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
7
by: seeker | last post by:
Hi, Does anybody know what's wrong with the following code: <div align="center"><a href="flashplay.php" onclick="NewWindow(this.href,'Listen this!','280','280','no','random');return false" ...
8
by: johnsonholding | last post by:
Here is the code for a pop-up window that works in Firefox and not in IE - I get a java error or something, Here is the code : </script> <SCRIPT language="JavaScript"...
2
by: Muckeypuck | last post by:
hello, i would like to write a function that takes a webcontrol type as a parameter and returns an array of controls based on the type some thing like: ...
2
by: Sun Liwen | last post by:
Hi, all: I met something wierd when I reading the latest json.js. look at the code segment. especially notice the "START HERE" and "END HERE" place... (function () { //...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
3
by: phocis | last post by:
I wrote a pair of functions to enable scoped or referenced setTimeout calls. I did this because I have an object factory that creates multiple objects and those objects need to delay a few calls on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.