Brent Waggoner, Web Developer


Cannot Invoke a Non-Delegate Type: Only VB Programmers Will Understand This!

tl;dr: C# uses square braces [], not parentheses, for indexes. Using the wrong one will throw this error.

Short and sweet today: I was working with a client who has recently moved from Visual Basic to C#. While implementing an SHA hash, was getting an error message he didn’t expect when running his program, he was getting the following message:

Cannot invoke a non-delegate type

Here’s a simplified version of the code:

dynamic sha1 = System.Security.Cryptography.SHA1.Create();
dynamic inputBytes = Encoding.ASCII.GetBytes(value);
dynamic hash = sha1.ComputeHash(inputBytes);

dynamic sb = new StringBuilder();
for (int i = 0; i <= hash.Length - 1; i++)
{
     sb.Append(hash(i).ToString("X2"));
}
return sb.ToString();

hash(i).ToString(“X2”) was throwing the error. Googling didn’t shed much light on the issue, since it’s such a general message, and the IDE wasn’t showing any errors. So what was the issue?

When the C# compiler sees () at the end of a call, it assumes that it’s a function call (a.k.a. a delegate). For this reason, C# uses square braces [] to indicate an index. My client, coming from Visual Basic, had used parentheses instead. The IDE didn’t see any syntactical issues with them, but the compiler did. Changing the line to:

hash[i].ToString("X2")

resolved this issue completely. And now you know the rest of the story. For more on delegates, read check out this post.

I Build Things

Need a solution for your website or business? Contact Me!


Recent Posts

What I Do

I love to build things. I love making the web work.

Tools I Use:

  • C#
  • .NET MVC
  • Javascript / jQuery
  • Angular
  • PHP
  • SQL / MySQL

But what really matters is solving your problems. I can do that too.

What I've Done

  • JointCharge joint supplement is a great product that needed a great site. With a responsive layout and a custom eCommerce solution, they're helping more pets than ever.

  • IlluminedNation helps match the right person to the right job. We automated their process so companies could more easily benefit from their experise.

  • Diamond Deluxe is one of three sites that needed a responsive makeover without a whole new design. Using Bootstrap and some custom menus, we moved it into the mobile age.

What Can I Do For You?

Need a web site? Fill out the form below and I'll contact you to work out the details. Or email me directly at brent.waggoner@gmail.com

Thanks for your request!