Solved

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

Posted on 2011-02-21
3
1,410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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