DirectCast in .NET 2.0

  • Thread starter Thread starter Sahil Malik
  • Start date Start date
Sahil,

It's not a C# feature, but a VB feature. What are you trying to do?
 
The closest equivalent in C# is to put the type you want to cast to in
parenthesis in front of the reference variable you want to cast. Note that
both will fail with an exception if the cast is invalid.

Dim tb As TextBox
tb = DirectCast(Sender, TextBox)

TextBox tb;
tb = (TextBox)sender;
 
Sahil,

I know nothing about Net 2.0

However I am almost sure that especially Jay wants to help you about this in
the VBNet language group, and when you ask it him he will give all C#
answers as well..

:-)

Cor
 
Thanks Rob. I thought I had used DirectCast in C# too .. :-/ ... I've been
programming in both C# and VB.NET concurrently which is why I end up writing
code that looks like this.

Dim conn as SqlConnection ;

And yeah VB.NET 2.0 does have DirectCast.

Thanks for your replies gentlemen !

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




Rob Windsor said:
The closest equivalent in C# is to put the type you want to cast to in
parenthesis in front of the reference variable you want to cast. Note that
both will fail with an exception if the cast is invalid.

Dim tb As TextBox
tb = DirectCast(Sender, TextBox)

TextBox tb;
tb = (TextBox)sender;

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/


Sahil Malik said:
I can't find it .. what am I missing?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Huh?"Almost"

I did not know you would read this now you see how much trust I have in you.

:-)

Cor
 
should not we use tb = (sender as TextBox);
and then check if tb is null

Rob Windsor said:
The closest equivalent in C# is to put the type you want to cast to in
parenthesis in front of the reference variable you want to cast. Note that
both will fail with an exception if the cast is invalid.

Dim tb As TextBox
tb = DirectCast(Sender, TextBox)

TextBox tb;
tb = (TextBox)sender;

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/


Sahil Malik said:
I can't find it .. what am I missing?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Yes, you can use the "as" operator to do a safe cast but that is not what
the original poster was asking about.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/


Abhijeet Dev said:
should not we use tb = (sender as TextBox);
and then check if tb is null

Rob Windsor said:
The closest equivalent in C# is to put the type you want to cast to in
parenthesis in front of the reference variable you want to cast. Note
that both will fail with an exception if the cast is invalid.

Dim tb As TextBox
tb = DirectCast(Sender, TextBox)

TextBox tb;
tb = (TextBox)sender;

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/


Sahil Malik said:
I can't find it .. what am I missing?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Back
Top