Solved

FontConverter won't work in my VB.NET code, works just fine in C#

Posted on 2011-02-21
3
1,375 Views
Last Modified: 2012-05-11
I have a really simple C# function that I converted and use in VB.NET code.

The original:

public static Font StringToFont(string text) {
        return (Font)new FontConverter().ConvertFromString(text);
}

The converted code, in VB.NET:

Public Shared Function StringToFont(ByVal text As String) As Font
        Return DirectCast(New FontConverter().ConvertFromString(text), Font)
End Function

There was no error thrown when StringToFont was called, but when I did a mouseover of the value it showed {Name = Reference to a non-shared member requires an object reference. Size=12}, however, clicking the plus sign and drilling down into the object showed that all the properties had been set, even the Name property.

Then when I tried to assign the font to a label, I got the "Object reference not set to an instance of an object." error.

Just to be sure it wasn't some quirk of my objects, I pasted in the microsoft example:

        Dim converter As System.ComponentModel.TypeConverter = _
            System.ComponentModel.TypeDescriptor.GetConverter(GetType(Font))

        Dim font1 As Font = _
            CType(converter.ConvertFromString("Arial, 12pt"), Font)

...and got the same result trying to use font1.  Any ideas?

0
Comment
Question by:FrancineTaylor
  • 2
3 Comments
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 34947436
I just tried to make it even simpler:

Dim xFont As System.Drawing.Font = New System.Drawing.Font("Arial", 12)

...and I get the same error.  Doing a mouseover of the object gets

{Name = Reference to a non-shared member requires an object reference. Size=12

Is there something wrong with my system?  Is this a bug in VB.NET?
0
 
LVL 12

Accepted Solution

by:
Hairbrush earned 500 total points
ID: 34950367
Hi Francine

When I try Dim xFont As System.Drawing.Font = New System.Drawing.Font("Arial", 12) on my PC I get the same issue when I hover over xFont, but it works when I assign the font to a label.  I guess it is just a quirk of how the debugger interacts with the way the Font class is designed and doesn't indicate a problem, per se.

As for why you are getting an 'Object reference not set to an instance of an object' error, could it be the label that isn't properly instantiated?
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 35030896
You're right, the mouseover error message does seem to be harmless.  The real reason the error was being thrown was completely unrelated.  Thanks for responding...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now