Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting multiple values from a dataset ASP.NET(VB) 3 37
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
Tool Box 2 34
vb.net checkbox 7 41
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. Spend 30 short minutes with us to learn:   • Key questions to ask when considering a partnership to accelerate your business into the cloud • Pitfalls and mistakes other partners…

911 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

21 Experts available now in Live!

Get 1:1 Help Now