Refering To Objects By Strings

KeysCoder

Member
Joined
Apr 10, 2006
Messages
18
Location
Florida Keys
Programming Experience
3-5
Sorry if this is a real basic question but I can't seem to find how to refer to an object with a simple string variable. If a user wants to set a default font, for instance, I want to be able to persist that preference on the hard drive by storing a string representation of the font - like it's name- not the font itself. Is there a way to do that?

Basically, I'm looking for the opposite of the ToString method. I want to use the contents of a string as code.
 
Last edited:
Not sure I understand the problem. Why not just write Font.Name and any other info (size, style) to a settings file?
 
Create a user setting of type Font in Application Settings and store the default font there.
 
If you really want to create an object from a text string, you can do it using some reflection code. For example, the following code will create a custom DataSet object which is found in a project called 'AssemblyName'. First you retrieve the Type of the object (the True indicates to throw an exception if the type is not found) and then create the DataSet object afterwards.

VB.NET:
Dim t As Type = Type.GetType("AssemblyName." & DatasetTypeName, True)
Dim ds As DataSet = CType(Activator.CreateInstance(t), DataSet)
This can be useful if you need to store object names in a database or something, so the object can be created and used in a generic fashion.

mafro
 
Are we talking about font here? If so the .net framework already has built in functions to convert certain type to and from a string. Thats how the designer works. A font would be done like so...

VB.NET:
[COLOR=black]'Font [COLOR=black]To String[/COLOR]....
 
Dim tc As TypeConverter = TypeDescriptor.GetConverter(GetType(Font))
Dim fontString As String = tc.ConvertToString(font)
 
 
'String To Font....
 
Dim newFont As Font = CType(tc.ConvertFromString(fontString), Font)[/COLOR]

The same is true for colors, just a different type converter
 
Hehe nice vis - I do always seem to find the hard way of doing things first.. ;)
 
Even vis is doing it the hard way. JohnH had the right idea. Use the in-built serialisation abilities of VB 2005. Create a Font setting at design time. Then at run time it's just:
VB.NET:
myControl.Font = My.Setings.MyFont
to load and:
VB.NET:
My.Setings.MyFont = myControl.Font
to save. In fact, if you use property binding in the designer then you don't even have to do that as it's done for you automatically. All you have to is provide a FontDialog for the user to change the font of the control at run time and everything else is taken care of for you.
 
By "the hard way" I mean a way that requires more than one line of code, which the easy way does. The even easier way requires no code at all, just setting properties in the designer. Obviously there'll be more code behind the scenes to accomplish the task but if I don't have to write it I don't care. Less code = easier.
 
Back
Top