Results 1 to 4 of 4

Thread: [02/03] fontStyle.bold in a variable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    [02/03] fontStyle.bold in a variable

    I wish to use this statement:
    Code:
    Dim fnt As New Font(fntfam, fntSize, FontStyle.Bold, GraphicsUnit.Point)
    However instead of using Fontstyle.bold I'd like to use a variable in its place so it can be italic or regular etc...

    I want to do this:
    Code:
    Dim fntStyle As String = "fontstyle." & admHash("fontstyle")
    where admHash("fontstyle") contains either bold. italic or regular but I get an error

    I guess I can use a select case stmt if there is no alternative

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] fontStyle.bold in a variable

    What you're trying to do there may seem convenient but it doesn't make sense. You're creating a String object that contains the text "fonstyle.bold" or the like. That has no relationship to a FontStyle value as is. The Font constructor requires a FontStyle value, not a String object. You cannot simply turn strings into other types like that. That's like writing the word "cat" on a piece of paper and expecting it to meow.

    Some types provide a way for you to read a string and convert it to that type, and enumerations do that. You can convert the string "Bold" to a FontStyle like this:
    vb.net Code:
    1. Dim style As FontStyle = DirectCast([Enum].Parse(GetType(FontStyle), _
    2.                                                  "Bold"), _
    3.                                     FontStyle)
    That said, you're mcuh, much better off storing enumerated values as Integers rather than Strings. That's because the underlying type of basically all enumerations is Integer. For instance, the numeric value of FontStyle.Bold is 1, while FontStyle.Italic is 2. Try this:
    vb.net Code:
    1. Dim style As FontStyle = FontStyle.Regular
    2.  
    3. MessageBox.Show("style = " & style.ToString())
    4.  
    5. style = FontStyle.Bold Or FontStyle.Italic
    6.  
    7. MessageBox.Show("style = " & style.ToString())
    8.  
    9. Dim int As Integer = CInt(style)
    10.  
    11. MessageBox.Show("int = " & int)
    12.  
    13. Dim newStyle As FontStyle = DirectCast(int, FontStyle)
    14.  
    15. MessageBox.Show("newStyle = " & newStyle.ToString())
    That shows how you can just cast back and forth between Integer and an enumerated type, assuming that the Integer value corresponds to a valid value of that type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] fontStyle.bold in a variable

    Quote Originally Posted by jmcilhinney
    What you're trying to do there may seem convenient but it doesn't make sense. You're creating a String object that contains the text "fonstyle.bold" or the like. That has no relationship to a FontStyle value as is. The Font constructor requires a FontStyle value, not a String object. You cannot simply turn strings into other types like that. That's like writing the word "cat" on a piece of paper and expecting it to meow.

    Some types provide a way for you to read a string and convert it to that type, and enumerations do that. You can convert the string "Bold" to a FontStyle like this:
    vb.net Code:
    1. Dim style As FontStyle = DirectCast([Enum].Parse(GetType(FontStyle), _
    2.                                                  "Bold"), _
    3.                                     FontStyle)
    That said, you're mcuh, much better off storing enumerated values as Integers rather than Strings. That's because the underlying type of basically all enumerations is Integer. For instance, the numeric value of FontStyle.Bold is 1, while FontStyle.Italic is 2. Try this:
    vb.net Code:
    1. Dim style As FontStyle = FontStyle.Regular
    2.  
    3. MessageBox.Show("style = " & style.ToString())
    4.  
    5. style = FontStyle.Bold Or FontStyle.Italic
    6.  
    7. MessageBox.Show("style = " & style.ToString())
    8.  
    9. Dim int As Integer = CInt(style)
    10.  
    11. MessageBox.Show("int = " & int)
    12.  
    13. Dim newStyle As FontStyle = DirectCast(int, FontStyle)
    14.  
    15. MessageBox.Show("newStyle = " & newStyle.ToString())
    That shows how you can just cast back and forth between Integer and an enumerated type, assuming that the Integer value corresponds to a valid value of that type.

    I tried to run your example but I get the error:
    Code:
    'DirectCast' operand must have a reference type, but 'Integer' is a value type.
    Also, what I am trying to do is save many of the properties of a listview that are changeable at runtime. Is there an easier way to save the state of a control?

    Update: Yes, I do use search once in awhile I found this

    http://www.vbforums.com/showthread.p...89#post2941189

    but again it is for 2005 version - I think I need to upgrade. There is a databindings window in 02 but nothing was showing in the drop downs and I dont really know how to use it.
    Last edited by jeffnyc; Jul 13th, 2007 at 10:53 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] fontStyle.bold in a variable

    Change DirectCast to CType and it should work fine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width