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

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 7064
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
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: 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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.