m.qayyum
m.qayyum

Reputation: 418

What is the C# equivalent of this code

This code is working fine in VB.Net how i convert it to C#.

 Dim alpha As String = "./;'[]<>?:""{}\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
    If InStr(alpha, e.KeyChar) Then e.Handled = True

Upvotes: 0

Views: 460

Answers (3)

slugster
slugster

Reputation: 49965

Note the use of the ampersand (@) at the start of the string:

string alpha = @"./;'[]<>?:""{}\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
if (alpha.IndexOfAny(new[] { e.KeyChar }) > -1) 
    e.Handled = true;

or a little more funky we can eliminate the if statement:

e.Handled = alpha.IndexOfAny(new[] { e.KeyChar }) > -1;

Upvotes: 0

Constantinos
Constantinos

Reputation: 29

string alpha = "./;'[]<>?:\"{}\\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

if(alpha.Contains(e.KeyChar))
e.Handled = true;

Besides that there is an excellent online tool that converts C# to VB.NET and vice versa http://www.developerfusion.com/tools/convert/vb-to-csharp/

Upvotes: -1

Reed Copsey
Reed Copsey

Reputation: 564851

In C#, this would be:

// Note the escaped string here: \\ instead of \, and \" for the quote
string alpha = "./;'[]<>?:\"{}\\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

if(alpha.Contains(e.KeyChar))
   e.Handled = true;

Upvotes: 4

Related Questions