FrancineTaylor
asked on
FontConverter won't work in my VB.NET code, works just fine in C#
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().ConvertFro mString(te xt);
}
The converted code, in VB.NET:
Public Shared Function StringToFont(ByVal text As String) As Font
Return DirectCast(New FontConverter().ConvertFro mString(te xt), 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.Type Converter = _
System.ComponentModel.Type Descriptor .GetConver ter(GetTyp e(Font))
Dim font1 As Font = _
CType(converter.ConvertFro mString("A rial, 12pt"), Font)
...and got the same result trying to use font1. Any ideas?
The original:
public static Font StringToFont(string text) {
return (Font)new FontConverter().ConvertFro
}
The converted code, in VB.NET:
Public Shared Function StringToFont(ByVal text As String) As Font
Return DirectCast(New FontConverter().ConvertFro
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.Type
System.ComponentModel.Type
Dim font1 As Font = _
CType(converter.ConvertFro
...and got the same result trying to use font1. Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
ASKER
Dim xFont As System.Drawing.Font = New System.Drawing.Font("Arial
...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?