Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I translate VB.NET's CType() to C#?

I have this code segment in VB.NET:

CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)

What is appropriate code in C#?

like image 799
Michael Avatar asked Sep 03 '12 14:09

Michael


People also ask

Can you convert VB to C?

The VB to C# code converter from the SharpDevelop team is now a standalone extension to Visual Studio. Once installed, you can convert an entire VB.NET project to C# by opening the solution, right clicking the solution node in the Solution Explorer and selecting Convert to C#.

Can we convert VB.Net to C#?

Although VB.Net and C# are very close cousins (syntax notwithstanding), there are a few key differences which will cause problems during conversion. These can be dealt with either before conversion or after. (Hello, compiler errors.)

What does CType mean in Visual Basic?

CType is compiled inline, which means that the conversion code is part of the code that evaluates the expression. In some cases, the code runs faster because no procedures are called to perform the conversion.

What is CType in C#?

CType or DirectCast CType Function returns the result of explicitly converting an expression to a specific data type, object, structure, class, or interface. CType(expression, typename)


1 Answers

In VB.Net CType(object, type) casts an object to a specific type.

There are two ways to accomplish this in C#:

Bitmap image = pbImageHolder.Image as Bitmap;
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);

or

Bitmap image = (Bitmap)(pbImageHolder.Image);
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
like image 92
Dennis Traub Avatar answered Oct 23 '22 09:10

Dennis Traub