Solved

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

Posted on 2011-02-21
3
1,397 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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