Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6
This is the forum to discuss the Wrox book Beginning Visual Basic 2005 by Thearon Willis, Bryan Newsome; ISBN: 9780764574016
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 22nd, 2008, 05:06 PM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default late binding

How will you eliminate the late binding error from the following code without turning option strict off?


Option Strict On
Imports System.Math

Public Class Form1
    Function MathOne() As Array
        Dim ArrayOne(20) As Double
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayOne(i) = Cos(t)
            t += PI / 10
        Next
        Return ArrayOne
    End Function


    Function MathTwo() As Array
        Dim ArrayTwo(20) As Double
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayTwo(i) = (Sin(t)) ^ 2 + (MathOne(i)) ^ 2
            t += PI / 10
        Next
        Return ArrayTwo
    End Function
End Class

  #2 (permalink)  
Old August 22nd, 2008, 05:29 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I have a better question:

How can you eliminate incredibly bad performance by rewriting this code to something reasonable?

*********

I assume this is a homework question, and generally people who inhabit forums don't answer homework questions. We (or at least I) believe that students won't learn if we do their work for them.

If you at least make an *attempt* at solving the problem and you get to a point where you are getting an error message or bad result and just need a hint...yeah, then we'll offer help. But don't expect people to do all your work for you.
  #3 (permalink)  
Old August 22nd, 2008, 05:42 PM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is not a homework question. This is code similar to one in the application that I am developing professionally. If your knowledge about VB is primitive enough not to understand this simple code, stay away from such forums. At least you can stop replyng to the topics and do favor on others.

  #4 (permalink)  
Old August 22nd, 2008, 05:45 PM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is not a homework question. This is code similar to one in the application that I am developing professionally. If your knowledge about VB is primitive enough not to understand this simple code, stay away from such forums. At least you can stop replying to the topics and do favor on others.
Quote:
quote:Originally posted by Old Pedant
 I have a better question:

How can you eliminate incredibly bad performance by rewriting this code to something reasonable?

*********

I assume this is a homework question, and generally people who inhabit forums don't answer homework questions. We (or at least I) believe that students won't learn if we do their work for them.

If you at least make an *attempt* at solving the problem and you get to a point where you are getting an error message or bad result and just need a hint...yeah, then we'll offer help. But don't expect people to do all your work for you.

  #5 (permalink)  
Old August 22nd, 2008, 06:24 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

It's still horrible code.

As written, you invoke the function MathOne( ) each time through the loop in the MathTwo( ) function.

So you duplicate all that work creating an array, filling it with cosine numbers, etc., 20 times!

At a minimum, you should have written something like

    Function MathTwo() As Array
        Dim ArrayTwo(20) As Double
        Dim m1 As Array = MathOne( )
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayTwo(i) = Sin(t)^2 + m1(i)^2
            t += PI / 10
        Next
        Return ArrayTwo
    End Function

Won't solve the late binding problem/question, but will perform 20 times better.

There are several ways to fix the late binding problem.

If you really insist on calling MathOne() 20 times in your loop (ugh), then you could do it by simply coding:
      ArrayTwo(i) = Sin(t) ^ 2 + CType(MathOne(), Double())(i) ^ 2

Another way would be to change my code above from
        Dim m1 As Array = MathOne( )
to
        Dim m1 As Double() = CType(MathOne( ), Double() )

But, of course, probably the best way would be to avoid the use of the undifferentiated type Array and declare the functions as
   Function MathOne( ) As Double( )
and
   Function MathTwo( ) As Double( )
which, in my mind, is by far the cleaner way. (But be sure to still do the
        Dim m1 As Double() = MathOne()
of course! To avoid the extra 19 calls to MathOne funciton.)


And, yes, I can think of a handful of more ways this could be done.

Anyway, if you were trying to test my knowledge, you didn't try too hard.

****************

What the heck...here's the cleanest way, as I see it. Changes (think I got them all) from your code are in red:

Code:
Public Class Form1
    Function MathOne() As Double()
        Dim ArrayOne(20) As Double
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayOne(i) = Cos(t)
            t += PI / 10
        Next
        Return ArrayOne
    End Function


    Function MathTwo() As Double()
        Dim ArrayTwo(20) As Double
        Dim m1 As Double() = MathOne()
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayTwo(i) = Sin(t) ^ 2 + m1(i) ^ 2
            t += PI / 10
        Next
        Return ArrayTwo
    End Function
End Class
  #6 (permalink)  
Old August 22nd, 2008, 06:54 PM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. If you had given such a nice answers in the first place, you could have avoided frustration on both sides.
Quote:
quote:Originally posted by Old Pedant
 It's still horrible code.

As written, you invoke the function MathOne( ) each time through the loop in the MathTwo( ) function.

So you duplicate all that work creating an array, filling it with cosine numbers, etc., 20 times!

At a minimum, you should have written something like

    Function MathTwo() As Array
        Dim ArrayTwo(20) As Double
        Dim m1 As Array = MathOne( )
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayTwo(i) = Sin(t)^2 + m1(i)^2
            t += PI / 10
        Next
        Return ArrayTwo
    End Function

Won't solve the late binding problem/question, but will perform 20 times better.

There are several ways to fix the late binding problem.

If you really insist on calling MathOne() 20 times in your loop (ugh), then you could do it by simply coding:
     ArrayTwo(i) = Sin(t) ^ 2 + CType(MathOne(), Double())(i) ^ 2

Another way would be to change my code above from
        Dim m1 As Array = MathOne( )
to
        Dim m1 As Double() = CType(MathOne( ), Double() )

But, of course, probably the best way would be to avoid the use of the undifferentiated type Array and declare the functions as
Function MathOne( ) As Double( )
and
Function MathTwo( ) As Double( )
which, in my mind, is by far the cleaner way. (But be sure to still do the
        Dim m1 As Double() = MathOne()
of course! To avoid the extra 19 calls to MathOne funciton.)


And, yes, I can think of a handful of more ways this could be done.

Anyway, if you were trying to test my knowledge, you didn't try too hard.

****************

What the heck...here's the cleanest way, as I see it. Changes (think I got them all) from your code are in red:

Code:
Public Class Form1
    Function MathOne() As Double()
        Dim ArrayOne(20) As Double
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayOne(i) = Cos(t)
            t += PI / 10
        Next
        Return ArrayOne
    End Function


    Function MathTwo() As Double()
        Dim ArrayTwo(20) As Double
        Dim m1 As Double() = MathOne()
        Dim t As Double = 0
        For i As Integer = 0 To 20 Step 1
            ArrayTwo(i) = Sin(t) ^ 2 + m1(i) ^ 2
            t += PI / 10
        Next
        Return ArrayTwo
    End Function
End Class
  #7 (permalink)  
Old August 22nd, 2008, 07:06 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Yes, but if you had presented it as a *REAL* problem, instead of as something that looks like homework (honest, it does! It's nearly identical to many homework problems I have seen), I wouldn't have made the wrong assumption.

Plus if you hadn't made that huge mistake of calling the same (invariant) function every time through the loop, I wouldn't have thought it was student work.

I know, a lot of people don't like to reveal what projects they are really working one. Afraid somebody is going to steal their ideas and/or code. But it's still better to give us something at least *close* to the real problem, instead of completely made-up examples.

**********

p.s.: I worked at MS on the VB.NET team for 3 years. And, yes, this really did look like an "exercise for the student" problem, even as MS hands them out.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Late Binding with COM DLL Rajashekhar G H Access 1 November 4th, 2011 01:33 PM
late binding,help? alexcym Visual Basic 2005 Basics 2 April 7th, 2008 03:26 PM
Subform Late Binding probitaille Access VBA 0 August 18th, 2007 02:02 AM
Late Binding Blade XML 3 July 17th, 2003 06:49 AM



All times are GMT -4. The time now is 09:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.