469,586 Members | 2,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,586 developers. It's quick & easy.

Casting in C#

What is the C# equivelant of the following VB:

CType(controlTemplate, TextBox).Text = ""

I'm basically trying to use a more general object as an argument in one
of my functions and then change the behavior within the function based
on its type.

Regards,
David P. Donahue
dd******@ccs.neu.edu
Nov 16 '05 #1
7 6712
David,

You would do this:

// Cast to a textbox and set the text property.
((TextBox) controlTemplate).Text = string.Empty;

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
What is the C# equivelant of the following VB:

CType(controlTemplate, TextBox).Text = ""

I'm basically trying to use a more general object as an argument in one of
my functions and then change the behavior within the function based on its
type.

Regards,
David P. Donahue
dd******@ccs.neu.edu

Nov 16 '05 #2
Hi David,

Use:
((TextBox)controlTemplate).Text = ""

HTH,
Rakesh Rajan

"David P. Donahue" wrote:
What is the C# equivelant of the following VB:

CType(controlTemplate, TextBox).Text = ""

I'm basically trying to use a more general object as an argument in one
of my functions and then change the behavior within the function based
on its type.

Regards,
David P. Donahue
dd******@ccs.neu.edu

Nov 16 '05 #3
The equivelent is the cast operator

((TextBox)controlTemplate).Text = "";

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

What is the C# equivelant of the following VB:

CType(controlTemplate, TextBox).Text = ""

I'm basically trying to use a more general object as an argument in one
of my functions and then change the behavior within the function based
on its type.
Nov 16 '05 #4
Feel free to download the (supported & free) Demo Edition of our
Instant C# VB.NET to C# converter at:
http://www.instantcsharp.com

It'll save you a lot of time and does all possible VB.NET to C#
translations.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #5
In addition to the short form given by the others, this method is a bit
safer:

TextBox tb = controlTemplate as TextBox;
if (tb != null)
tb.Text = "";

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
What is the C# equivelant of the following VB:

CType(controlTemplate, TextBox).Text = ""

I'm basically trying to use a more general object as an argument in one
of my functions and then change the behavior within the function based
on its type.

Regards,
David P. Donahue
dd******@ccs.neu.edu

Nov 16 '05 #6
James Curran wrote:
In addition to the short form given by the others, this method is a bit
safer:

TextBox tb = controlTemplate as TextBox;
if (tb != null)
tb.Text = "";


I agree,
Casting using "As" will return null if cast fails instead of an
invalidcastexception.

HTH
JB
Nov 16 '05 #7
The Last Gunslinger <jb******@yahoo.com> wrote:
In addition to the short form given by the others, this method is a bit
safer:

TextBox tb = controlTemplate as TextBox;
if (tb != null)
tb.Text = "";


I agree,
Casting using "As" will return null if cast fails instead of an
invalidcastexception.


Why is that good though? If I've got something which *should* be a
TextBox, why is it good to mask the fact that my assumption is wrong?

There are times when it's better to use "as" - namely if your code
really doesn't know whether the cast should work or not - and there are
times when it's better to use a cast.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
231 posts views Thread by Brian Blais | last post: by
35 posts views Thread by ytrama | last post: by
7 posts views Thread by Jim Bancroft | last post: by
7 posts views Thread by S. Lorétan | last post: by
17 posts views Thread by sophia.agnes | last post: by
9 posts views Thread by Taras_96 | last post: by
101 posts views Thread by Tinkertim | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.