473,763 Members | 6,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting in C#

What is the C# equivelant of the following VB:

CType(controlTe mplate, 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.ne u.edu
Nov 16 '05 #1
7 7096
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.co m

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

CType(controlTe mplate, 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.ne u.edu

Nov 16 '05 #2
Hi David,

Use:
((TextBox)contr olTemplate).Tex t = ""

HTH,
Rakesh Rajan

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

CType(controlTe mplate, 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.ne u.edu

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

((TextBox)contr olTemplate).Tex t = "";

Regards

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

What is the C# equivelant of the following VB:

CType(controlTe mplate, 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.n eu.edu> wrote in message
news:Og******** ******@TK2MSFTN GP09.phx.gbl...
What is the C# equivelant of the following VB:

CType(controlTe mplate, 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.ne u.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
invalidcastexce ption.

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
invalidcastexce ption.


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.co m>
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
3478
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 good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
231
23228
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 (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
35
2701
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
3677
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; but it complains for the following lines
7
30789
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 along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will work. At least, that's how it appears to me. Could someone explain when to use old-style parenthesized casts vs. the Convert() methods?
2
2168
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 has similar structure of the classes in my application. The test worked fine, the casting I want to do must work. I compared the structure of the test with the application to ensure they where similar (inheriting twice, base class implements...
7
13705
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
2228
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 type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
9
3464
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
4344
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 : char *tmp = NULL;
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.