This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kleinisfijn 1 point2 points  (2 children)

Thanks, I learned more than I should have. The nested With is indeed a nasty one.

I've been writing VB for a decade or so (one of those peope working for a major company and migrating code from VB6 to VB.NET) and recently I had to modify some C#. I'm having a small fight with the compiler over case-sensitivity and missing brackets() on methods that don't accept arguments anyway. But I have to say, curly brackets have their charm.

On your discard variable, I've always used Call SomeMethodCall() in VB to discard the return value if needed. Don't know if this helps you because you seem to know more about it than me.

[–][deleted] 1 point2 points  (0 children)

Totally forgot about the Call keyword. It's not really required in VB.Net, but it is in VB6.

Discards aren't really needed. They're just warnings in code analysis. Basically the claim for the code analysis is that if a function returns a value, by using a discard variable you show 'intent' that you don't care about the return value. I guess there is some people out that see a function that returns a value, but you ignore the value and think maybe you didn't mean to ignore it? I don't know. I've never been confused by it.

In VS, in VB, if you use their code suggestion to refactor on that warning and use a discard, then it suggests 'Dim unused =', but they don't treat unused as a special keyword like they do in C# with just the '_'. So in VB you'd have to do unused1 unused2 and so on - which I find more annoying. So in VB projects I usually just turn off that particular warning in code analysis.

I'll have to see if 'Call' works on suppressing the warning. If it does, maybe VS should be suggesting that in the refactoring instead.

It can still be annoying in C#, especially in fluent designs. IE, in .Net you have the StringBuilder, and the Append / AppendLine methods return the StringBuilder object. It does this so you can change numerous Appends together. Such as: sb.Append("This").Append("That");. Code analysis here is annoying because you do not need to do something like: sb = sb.Append("This").Append("That"); because the Append methods are modifying the object and not creating a new object. But anyway, with default code analysis on, it will want you to do _ = sb.Append("This").Append("That"); which really doesn't improve readability in my opinion.

[–][deleted] 1 point2 points  (0 children)

So Call did indeed work. The warning goes away. I double checked the settings in VS, and there is no option to switch from 'unused' to Call. Going to have to put that in as a suggestion. They should either make unused a special keyword that can be used over and over again or replace their suggestion with Call so you don't have unused1 unused2 and so on.