The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,887 other followers

Archive for the ‘VB.NET 11.0’ Category

VB.NET: If you want to cast use DirectCast or TryCast; if you want to convert, use CType

Posted by jpluimers on 2014/07/03

I’ve done quite a bit of VB.NET maintenance lately.

Most of that code was riddled with CType, both for conversions and casts. Quite a bit code had Option Explicit and Option Strict Off. A lot of those CType constructions had empty Try / Catch / End Try blocks around them.

Those empty catch blocks are a code smell. They pretend to be able to survive any exceptional disaster, but in practice you can’t. You have to indicate what kinds of disasters you can handle, for instance if a meteorite hits your data center (thanks George Stocker).

Turning off Option Strict can be OK under many circumstances (the default is off), but having Option Explicit off is usually a code smell as well, just like On Error Resume Next (which was also in plenty of the source code).

I do understand a lot of VB.NET source comes from people having programmed in VB 6, VBScript or VBA for a long time where those constructs were more common. But writing code in the 21st century is much more about writing code that you can prove to be right. Having proper error handling and compiler type checking is a big part of that.

It pays to go with the idiom, for example read the good and bad ways of vb.net – Safest way to check for integer.

Back to CType: basically you have do distinguish between conversions and casts. The reason is that when you know it will be a form of cast, CType is way to expensive. And if you know you will be doing conversions, than casting is not what you want.

Casting Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

The C language specification describes an abstract computer, not a real one – The Old New Thing – Site Home – MSDN Blogs

Posted by jpluimers on 2014/04/09

Interesting read:

The C language specification describes an abstract computer, not a real one – The Old New Thing – Site Home – MSDN Blogs.

In other words: any language that merges null behaviour in the underlying storage will have a problem somwehere.

So if you want to have true nullable types, your null flag should be stored outside the underlying storage.

The .NET framework 2 and up, most database management systems and many other environment support that.

But most languages don’t support it for pointer types. So there will be portions of address spaces either inaccessible, or only accessible when skipping the null pointer checks.

Note that the thread above contains some very interesting bits, for instance this one:

Matt 28 Mar 2013 5:58 PM #

@MarkY “Dereferencing null is undefined?  Cool!  I thought it was guaranteed to crash, just like a false assertion or something.  So crashing is the OS guarantee, not the language guarantee apparently.”

Nope. It’s not an OS guarantee either. The OS won’t ever normally allocate memory at address zero, but there’s nothing to stop you telling it to. Try doing “VirtualAlloc(1, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)” on your pre-Windows8 machine.

In fact, this is the reason why null-dereferences in kernel mode are often exploitable as elevation of privilege attacks. The null-page is mappable and within the user-addressable region of memory, so if the kernel dereferences a null pointer, it reads attacker controllable data.

And btw, this is the reason why on Linux and Windows8+ you can’t map the null-page.

–jeroen

via: The C language specification describes an abstract computer, not a real one – The Old New Thing – Site Home – MSDN Blogs.

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Borland C++, Borland Pascal, C, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C++, C++ Builder, Database Development, Delphi, Development, Pascal, Quick Pascal, Software Development, Turbo Pascal, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

.NET/C#/VB.NET – Default value for generics (via: Stack Overflow)

Posted by jpluimers on 2014/02/05

Looking for the pesky little differences between C# and VB.NET, I stumbled over this nice question by Micah Martin on default values for generics in VB.NET as compared to C#. Actually there were 3 questions, so I did a bit of post-editing:

How do I create the default for a generic in VB.NET? in C# I can call:
T variable = default(T);

  1. How do I do this in VB?
  2. If this just returns null (C#) or nothing (VB.NET) then what happens to value types?
  3. Is there a way to specify for a custom type what the default value is? For instance what if I want the default value to be the equivalent to calling a parameterless constructor on my class.

User Konrad Rudolph – Stack Overflow. promptly gave three answers:

Question 1:

Dim variable As T ‘ or ‘ Dim variable As T = Nothing ‘ or ‘ Dim variable As New T() Notice that the latter only works if you specifiy either the New or the Structure constraint for the generic type.

Question 2:

For value types all members of the struct are “nulled” out, i.e. all reference type members are set to null (Nothing) and all value types are in turn nulled out. And no, since string is a reference type, it does not result in "" for strings as suggested in the other answer.

Question 3:

No, there’s no way to specify this. There are some threads about this on Stack Overflow already, e.g. here. Jon has posted an excellent explanation why this is.

–jeroen

via: c# – Default value for generics – Stack Overflow.

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

.net – xcopy ASP.NET / WinForms deployment: find common location to access relative files to it (via: Stack Overflow)

Posted by jpluimers on 2013/12/25

StackOverflow user Joe (sorry, no last name) helped me big time by answering my question on Business logic shared by ASP.NET / WinForms: find the location of the assembly to access relative files to it.

Before showing the code at the bottom of this blog post, let me explain the question in more detail:

Basically I was in the midst of refactoring some ‘inherited’ business logic code that – before refactoring – for the ASP.NET side needs to be initialized with an absolute path, but on the WinForms / WPF side only with a relative path to a GetExecutingAssembly directory.

To ease xcopy deployment, I wanted all configuration settings to be relative. But I hadn’t found a common means for these platforms to obtain a directory usable as a root for accessing relative files.

That way I could put identical settings in both the Web.config and App.config, heck even generate them based on a common fragment, whithout having to hard-code absolute path names.

I knew about Assembly.GetExecutingAssembly, but in ASP.NET that location is not where the web site is (both IIS and the WebDevelopment server make use of temporary locations to store the assemblies).

ASP.NET does have Server.MapPath and HostingEnvironment.MapPath, but I didn’t want to make the business logic depend on ASP.NET.

Joe came up with this solution, which works dandy: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, ASP.NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, F#, Prism, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0, Web Development | Leave a Comment »

.NET/C#: Igor Ostrovsky wrote a few great MSDN magazine articles helping you write better threading code

Posted by jpluimers on 2013/09/17

Igor Ostrovsky wrote a few very nice MSDN magazine articles. Not all of them have ended up in the list at MSDN magazine, so here is a more complete list:

Though the articles show the majority of sample code in C#, the actual topics are of great interest to any developer writing .NET code or interfacing to it.

Some keywords in his articles: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, .NET CF, C, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C++, Delphi, Development, F#, LINQ, PLINQ, Prism, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

 
%d bloggers like this: