Results 1 to 23 of 23

Thread: [RESOLVED] [2005] Option Strict On and Late Binding

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [RESOLVED] [2005] Option Strict On and Late Binding

    Hi!!

    I have Option Strict On and this code:
    vb Code:
    1. Private Sub Bs_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles B1.MouseEnter, B2.MouseEnter, B3.MouseEnter, B4.MouseEnter, B5.MouseEnter
    2.     sender.BackgroundImage = My.Resources.BHover
    3. End Sub
    And i get this error: "Option Strict On disallows late binding". I went searching how could i fix it. And the only thing i could find has to turn option strict off.

    Is there anyway of fixing with out turning strict off?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Option Strict On and Late Binding

    If you want only for that page then place this in the top of the page
    Code:
    option Strict Off
    If you want it for the whole project
    Code:
    To set Option Strict in the integrated development environment (IDE)
    
    On the Tools menu, choose Options.
    
    Open the Projects and Solutions node.
    
    Choose VB Defaults.
    
    Modify the Option Strict setting.
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [2005] Option Strict On and Late Binding

    directcast sender into whatever type it's supposed to be.

    As you have it there, it is still boxed in an object.

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [2005] Option Strict On and Late Binding

    dana - no, bad. Turning "strict off" is sloppy coding no matter how you cut it.

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Option Strict On and Late Binding

    Hi Sevenhallo,
    Here we develop application which does automation of
    Winword Documents.For that here our employees has so many
    version of MS Office.For getting the default version of Winword
    I normallly use LateBinding.Is it Bad
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Option Strict On and Late Binding

    The only bad thing would be to turn Option Strict Off.

  7. #7

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] Option Strict On and Late Binding

    Hack is totally right.

    Thks a lot sevenhalo, for the tip to use directcast. It worked.

    Now i ran into a similar problem:
    vb Code:
    1. Dim Helms(3), Armors(4), Pants(4), Gloves(4), Boots(4), SAS(3), SM(2), BCB(1), Shields(2), Wings(4), SSO(4), Misc(7), Tabelas(11) As String
    2. Dim Array(11) As Array
    3. Helms(0) = "Dk_Helms"
    4. Helms(1) = "Dw_Helms"
    5. Helms(2) = "Elf_Helms"
    6. Helms(3) = "Dl_Helms"
    7. Tabelas(0) = "BI_Helms"
    8. Array(0) = Helms
    9. Armors(0) = "Dk_Armors"
    10. Armors(1) = "Dw_Armors"
    11. Armors(2) = "Elf_Armors"
    12. Armors(3) = "Dl_Armors"
    13. Armors(4) = "Mg_Armors"
    14. Tabelas(1) = "BI_Armors"
    15. Array(1) = Armors
    16. ...
    17. 'This line is after a nested loop
    18. Dim CmdSelect As New OleDb.OleDbCommand("SELECT * FROM " & Array(Actual)(TabActual).ToString, MuShop.ClassAndItems)

    I get the same error were: "Array(Actual)(TabActual).ToString" but since its a jagged array i cant figure out how to solve this.
    Last edited by Lasering; Nov 2nd, 2007 at 09:34 AM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Option Strict On and Late Binding

    I think what dana is saying is that they do a lot of office automation apps which in many cases require late binding and therefor option strict needs to be off since option strict disallows late binding. Normally you can reference a specific office version and use early binding, but if your app needs to target various versions of office, then you can't set a specific version reference, you have to use late binding.

    One thing to note is that option strict can be set at the project level, but also at the module level, so it would be possible to have option strict ON for the project, but turn it off in certain modules where you need to use late binding. That way your entire project doesn't suffer from not getting the option strict syntax errors.

  9. #9

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] Option Strict On and Late Binding

    i fixed with CType.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  10. #10
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    To be honest, that code gives me a headache.

    You're working in an OOP language. Why are you not taking advantage of it? Divide things out into custom objects, for instance...
    Code:
        Private col As New Generic.List(Of IThings)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            col.Add(New Thing1)
            col.Add(New Thing2)
        End Sub
    
        Private Interface IThings
            ReadOnly Property Name() As String
        End Interface
        Private Class Thing1 : Implements IThings
            Public ReadOnly Property Name() As String Implements IThings.Name
                Get
                    Return "Thing1"
                End Get
            End Property
        End Class
        Private Class Thing2 : Implements IThings
            Public ReadOnly Property Name() As String Implements IThings.Name
                Get
                    Return "Thing2"
                End Get
            End Property
        End Class
    I have 2 things (Thing1 and Thing2). They both have Name in common. Now both Thing1 and Thing2 are "related" by IThings.

    I need a collection of these IThings, so I made col. Anything that implements IThings can be added to col and I know they will all have a "Name" property. Thing1 might also have properties that Thing2 doesn't, but they both are IThings.

    You can do this with your example. Take for instance Helm, Pants and Gloves. What do they all have in common? They're all armor. So now you need to have a class for each (helm, pants and gloves) and put together an interface for IArmor. Now what does all armor have? All armor has a name, it also has a type (sor instance, chainmail, leather studded, etc) and it probably has a condition (damaged, shiny, dirty, whatever).

    The possibilities and vastness of what you can do with these is endless, but realize you're working with a language that offers and expects objects.

  11. #11
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    The issue with early binding is solved with the ability to box objects. After you identify which version of office you are working with, you have a different set of insturctions to follow.

    Option Strict Off is never acceptable in my book.

  12. #12

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    i think my solution is faster and simples , maybe not so good but its good enought:
    vb Code:
    1. Dim CmdSelect As New OleDb.OleDbCommand("SELECT * FROM " & CType(Array(Actual), String())(TabActual).ToString, MuShop.ClassAndItems)
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  13. #13
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    You would be wrong and forever stuck in the mindset of a linear programmer.

  14. #14

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    what u mean with mindset of a linear programmer, because i'm portuguese and i dont really understand that expression
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  15. #15
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    I believe what he means is you are more or less approaching this the way someone would in a language that DOESN'T support all the object oriented features that VB.NET supports.

    You are basically bypassing some of the fundamental concepts and abilities in the language and making your code more cumbersome and ultimately harder to maintain.

  16. #16

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    he is right i could use the example he gave and "use" the object oriented features, but in this case its not that big a deal. This project is small and simple. To do a class for each "thing" (helm, armor, gloves, pants, shields, etc) would be a waste of time and code. Because that part of the code is the only code that uses it (its about 15 lines of code).
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  17. #17
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    I'm not saying you should change your code technique, I was just trying to let you know what he was getting at

  18. #18
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    Changing your technique wouldn't just improve those 15 lines of code. It would improve your entire program. The immediate benefit will only be seen in those 15 lines, but using the objects and storing information wouldn't be the indexing disaster you're potentially facing.

    Just a quick tip - take it or leave it.

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    Just wanted to add that Option Strict On and Late Binding is very possible. Use Reflection and your good to go.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    This code is bad:
    vb.net Code:
    1. Dim Helms(3) As String
    2.  
    3. Helms(0) = "Dk_Helms"
    4. Helms(1) = "Dw_Helms"
    5. Helms(2) = "Elf_Helms"
    6. Helms(3) = "Dl_Helms"
    This code is good:
    vb.net Code:
    1. Public Enum Helm
    2.     Dk
    3.     Dw
    4.     Elf
    5.     Dl
    6. End Enum
    Now Helm is a type and each type of helm is a member of that type. Everywhere you want to use a helm you use the Helm type and you are limited to only valid Helm values. If you use strings for each type of helm then you can assign any string you want and the compiler will never point out your error, perhaps leading to your deployed application crashing.
    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

  21. #21

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    Here is the entire code:
    vb Code:
    1. Dim Helms(3), Armors(4), Pants(4), Gloves(4), Boots(4), SAS(3), SM(2), BCB(1), Shields(2), Wings(4), SSO(4), Misc(7), Tabelas(11) As String
    2.         Dim Array(11) As Array
    3.         Helms(0) = "Dk_Helms"
    4.         Helms(1) = "Dw_Helms"
    5.         Helms(2) = "Elf_Helms"
    6.         Helms(3) = "Dl_Helms"
    7.         Tabelas(0) = "BI_Helms"
    8.         Array(0) = Helms
    9.         Armors(0) = "Dk_Armors"
    10.         Armors(1) = "Dw_Armors"
    11.         Armors(2) = "Elf_Armors"
    12.         Armors(3) = "Dl_Armors"
    13.         Armors(4) = "Mg_Armors"
    14.         Tabelas(1) = "BI_Armors"
    15.         Array(1) = Armors
    16.         Pants(0) = "Dk_Pants"
    17.         Pants(1) = "Dw_Pants"
    18.         Pants(2) = "Elf_Pants"
    19.         Pants(3) = "Dl_Pants"
    20.         Pants(4) = "Mg_Pants"
    21.         Tabelas(2) = "BI_Pants"
    22.         Array(2) = Pants
    23.         Gloves(0) = "Dk_Gloves"
    24.         Gloves(1) = "Dw_Gloves"
    25.         Gloves(2) = "Elf_Gloves"
    26.         Gloves(3) = "Dl_Gloves"
    27.         Gloves(4) = "Mg_Gloves"
    28.         Tabelas(3) = "BI_Gloves"
    29.         Array(3) = Gloves
    30.         Boots(0) = "Dk_Boots"
    31.         Boots(1) = "Dw_Boots"
    32.         Boots(2) = "Elf_Boots"
    33.         Boots(3) = "Dl_Boots"
    34.         Boots(4) = "Mg_Boots"
    35.         Tabelas(4) = "BI_Boots"
    36.         Array(4) = Boots
    37.         SAS(0) = "Dk_Swords"
    38.         SAS(1) = "Dk_Axes"
    39.         SAS(2) = "Dk_Spears"
    40.         SAS(3) = "Mg_Swords"
    41.         Tabelas(5) = "BI_Swords_Axes_Spears"
    42.         Array(5) = SAS
    43.         SM(0) = "Dk_Maces"
    44.         SM(1) = "Elf_Maces"
    45.         SM(2) = "Dl_Scepters"
    46.         Tabelas(6) = "BI_Scepters_Maces"
    47.         Array(6) = SM
    48.         BCB(0) = "Elf_Bows"
    49.         BCB(1) = "Elf_Crossbows"
    50.         Tabelas(7) = "BI_Bows_Crossbows"
    51.         Array(7) = BCB
    52.         Shields(0) = "Dk_Shields"
    53.         Shields(1) = "Dw_Shields"
    54.         Shields(2) = "Elf_Shields"
    55.         Tabelas(8) = "BI_Shields"
    56.         Array(8) = Shields
    57.         Wings(0) = "Dk_Wings"
    58.         Wings(1) = "Dw_Wings"
    59.         Wings(2) = "Elf_Wings"
    60.         Wings(3) = "Mg_Wings"
    61.         Wings(4) = "Dl_Wings"
    62.         Tabelas(9) = "BI_Wings"
    63.         Array(9) = Wings
    64.         SSO(0) = "Dk_Skills"
    65.         SSO(1) = "Dw_Spells"
    66.         SSO(2) = "Elf_Orbs"
    67.         SSO(3) = "Dl_Spells"
    68.         SSO(4) = "Mg_Skills"
    69.         Tabelas(10) = "BI_SSO"
    70.         Array(10) = SSO
    71.         Misc(0) = "Misc_Item_Bags"
    72.         Misc(1) = "Misc_Potions"
    73.         Misc(2) = "Misc_Pets"
    74.         Misc(3) = "Misc_Jewels"
    75.         Misc(4) = "Misc_Quest_Items"
    76.         Misc(5) = "Misc_BC_DS"
    77.         Misc(6) = "Misc_Rings_Pendents"
    78.         Misc(7) = "Misc_Others"
    79.         Tabelas(11) = "BI_Misc"
    80.         Array(11) = Misc
    81.         Try
    82.             MuShop.ClassAndItems.Open()
    83.             For Actual As Integer = 0 To Array.Length - 1
    84.                 For TabActual As Integer = 0 To Array(Actual).Length - 1
    85.                     Dim CmdSelect As New OleDb.OleDbCommand("SELECT * FROM " & CType(Array(Actual), String())(TabActual).ToString, MuShop.ClassAndItems)
    86.                     Dim Reader As OleDb.OleDbDataReader = CmdSelect.ExecuteReader
    87.                     While Reader.Read
    88.                         Dim CommandInsert As New OleDb.OleDbCommand("INSERT INTO " & Tabelas(Actual) & " ([Name], Category, [Number], [Level], [Level?], [Skill?], [Luck?], [Options?], Y, X, Path) VALUES (@Name, @Category, @Number, @Level, @PTLevel, @PTSkill, @PTLuck, @PTOptions, @Y, @X, @Path)", MuShop.ClassAndItems)
    89.                         CommandInsert.Parameters.AddWithValue("@Name", Reader("Name"))
    90.                         CommandInsert.Parameters.AddWithValue("@Category", Reader("Category"))
    91.                         CommandInsert.Parameters.AddWithValue("@Number", Reader("Number"))
    92.                         CommandInsert.Parameters.AddWithValue("@Level", Reader("Level"))
    93.                         CommandInsert.Parameters.AddWithValue("@PTLevel", Reader("Level?"))
    94.                         CommandInsert.Parameters.AddWithValue("@PTSkill", Reader("Skill?"))
    95.                         CommandInsert.Parameters.AddWithValue("@PTLuck", Reader("Luck?"))
    96.                         CommandInsert.Parameters.AddWithValue("@PTOptions", Reader("Options?"))
    97.                         CommandInsert.Parameters.AddWithValue("@Y", Reader("Y"))
    98.                         CommandInsert.Parameters.AddWithValue("@X", Reader("X"))
    99.                         CommandInsert.Parameters.AddWithValue("@Path", Reader("Path"))
    100.                         CommandInsert.ExecuteNonQuery()
    101.                     End While
    102.                     Reader.Close()
    103.                 Next
    104.             Next
    105.         Catch ex As Exception
    106.              'some code
    107.         Finally
    108.             MuShop.ClassAndItems.Close()
    109.         End Try
    Dk_Helms, Dw_Helms, Elf_Helms, BI_Helms are table names from DB (Access).
    Now can I still obtain the same result, using enumerations?
    Last edited by Lasering; Nov 4th, 2007 at 09:24 AM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

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

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    Hmmm... didn't notice they were table names. I assumed they were being used differently, and we all know what assuming does. You could still use enumerations and convert the values to strings but it's probably of no real benefit.

    That said, I'm not quite sure, if you're hard-coding the table names anyway, why you need those arrays at all.
    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

  23. #23

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2005] Option Strict On and Late Binding

    The code copies data from a set of tables to one table. To make clear he copies all the data from Dk_Helms, Dw_Helms, Elf_Helms, Dl_Helms and inserts this data into the BI_Helms. All the data from the armor tables and puts it in the BI_Armors. Etc
    So i used 2 loops, the first one loops through the BI tables (BI_Helms, BI_Armors, etc) then the second loop loops through the tables from which retrieve the data (DK_Helms, Elf_Helms, DW_Helms, etc).
    So he reads all from the Helms tables (because of the second loop) and adds all the data to the BI_Helms while he is reading from the helms Tables.
    If I was to make a command to each table (for example a command to copy the data from Dk_Helms to BI_Helms, Dw_Helms to BI_Helms, etc) i would get a lot of commands and repeated code. So the 2 loops relate the Helms tables with the BI_Helm table, the Armor Tables with BI_Armor, and do all the copying correctly. And if i want to add more tables i just need to add them to the array.
    That why the arrays.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

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