Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

809 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