Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. I am a Beginner, could you help please?.

I am a Beginner, could you help please?.

Scheduled Pinned Locked Moved Visual Basic
helpquestionlearning
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 10118706
    wrote on last edited by
    #1

    Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.

    Sub Main
    console.writeline(getmyresult(2)) 'takes a while'
    console.writeline(getmyresult(3)) 'takes a while'
    console.writeline(getmyresult(2)) 'Should be instant'
    console.writeline(getMyresult(3)) 'Should be instant'
    End Sub

    function getMyresult(X as interger) as integer
    dim Y as integer=LongCompute(X)
    return Y
    end function

    function LongCompute(X as integer) as integer
    system.threading.thread.sleep(1000)
    return x^2
    end function

    D P C 3 Replies Last reply
    0
    • M Member 10118706

      Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.

      Sub Main
      console.writeline(getmyresult(2)) 'takes a while'
      console.writeline(getmyresult(3)) 'takes a while'
      console.writeline(getmyresult(2)) 'Should be instant'
      console.writeline(getMyresult(3)) 'Should be instant'
      End Sub

      function getMyresult(X as interger) as integer
      dim Y as integer=LongCompute(X)
      return Y
      end function

      function LongCompute(X as integer) as integer
      system.threading.thread.sleep(1000)
      return x^2
      end function

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      GetMyResult has to maintain it's own cache of result values. Create a collection to store Key/Value pairs, usually a Dictionary(Of Integer, Integer). The Key value will be the integer you pass in and the result will be the value returned by LongCompute. Basically, when GetMyResult is called, it uses the value passed in to search the Dictionary for that value. If it's found, it can return the value associated with that key. If it's not found, then you call LongCompute with the passed in value, get the result, and add the new key/value pair to the dictionary. Lastly, return the result back to the caller.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      P 1 Reply Last reply
      0
      • M Member 10118706

        Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.

        Sub Main
        console.writeline(getmyresult(2)) 'takes a while'
        console.writeline(getmyresult(3)) 'takes a while'
        console.writeline(getmyresult(2)) 'Should be instant'
        console.writeline(getMyresult(3)) 'Should be instant'
        End Sub

        function getMyresult(X as interger) as integer
        dim Y as integer=LongCompute(X)
        return Y
        end function

        function LongCompute(X as integer) as integer
        system.threading.thread.sleep(1000)
        return x^2
        end function

        P Offline
        P Offline
        PrissySC
        wrote on last edited by
        #3

        Why are you sleeping? When you put in a "sleep" the the application goes to sleep. You are blocking the thread from running. There are some instances where you need to use 0, but that is rare. And I would write this ... Function GetResult (ByVal num as Integer) as Integer return num^2 End Function Getmyresult is not needed at all. Sleep is not needed. Also, Public Main and Private Function. When you say "cache", I am not sure if you are using the term, but you can cache ... http://stackoverflow.com/questions/533861/writing-a-cache-function-in-vb-net[^] The cache class ... http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx[^]

        D M 2 Replies Last reply
        0
        • D Dave Kreskowiak

          GetMyResult has to maintain it's own cache of result values. Create a collection to store Key/Value pairs, usually a Dictionary(Of Integer, Integer). The Key value will be the integer you pass in and the result will be the value returned by LongCompute. Basically, when GetMyResult is called, it uses the value passed in to search the Dictionary for that value. If it's found, it can return the value associated with that key. If it's not found, then you call LongCompute with the passed in value, get the result, and add the new key/value pair to the dictionary. Lastly, return the result back to the caller.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          P Offline
          P Offline
          PrissySC
          wrote on last edited by
          #4

          Just curious Dave and Poster, but what does this mean in context - "cache"? I guess the simplicity of the code has me puzzled - two-stepping for a single computation. I can't see beyond that. I use a dictionary and actually truly cache with the class. Purpose?

          D 1 Reply Last reply
          0
          • P PrissySC

            Why are you sleeping? When you put in a "sleep" the the application goes to sleep. You are blocking the thread from running. There are some instances where you need to use 0, but that is rare. And I would write this ... Function GetResult (ByVal num as Integer) as Integer return num^2 End Function Getmyresult is not needed at all. Sleep is not needed. Also, Public Main and Private Function. When you say "cache", I am not sure if you are using the term, but you can cache ... http://stackoverflow.com/questions/533861/writing-a-cache-function-in-vb-net[^] The cache class ... http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx[^]

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Because his code is an example!

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            M 1 Reply Last reply
            0
            • P PrissySC

              Just curious Dave and Poster, but what does this mean in context - "cache"? I guess the simplicity of the code has me puzzled - two-stepping for a single computation. I can't see beyond that. I use a dictionary and actually truly cache with the class. Purpose?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              To hold a table of values that have already been computed so you don't have to run through a potentially time-consuming calculation again to return the same value for the given input.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              P 1 Reply Last reply
              0
              • D Dave Kreskowiak

                To hold a table of values that have already been computed so you don't have to run through a potentially time-consuming calculation again to return the same value for the given input.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                P Offline
                P Offline
                PrissySC
                wrote on last edited by
                #7

                Exactly the same way that I do on the accounting side of my apps, but I exlicitly set out to cache. Sorry to inject! I had curiosity get the best of me as I was looking at the code and realizing that it is a textbook example has answered the question. Once again, sorry! Thanks for the replies, and let me know if you solve the threading problem. LOL ... Attempted multitasking this day. :wtf:

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Because his code is an example!

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  M Offline
                  M Offline
                  Member 10118706
                  wrote on last edited by
                  #8

                  Dear Dave, Many thanks for explaining it to me as well instead of just giving me the code, My teacher will be impressed haha Many Thanks 101

                  1 Reply Last reply
                  0
                  • P PrissySC

                    Why are you sleeping? When you put in a "sleep" the the application goes to sleep. You are blocking the thread from running. There are some instances where you need to use 0, but that is rare. And I would write this ... Function GetResult (ByVal num as Integer) as Integer return num^2 End Function Getmyresult is not needed at all. Sleep is not needed. Also, Public Main and Private Function. When you say "cache", I am not sure if you are using the term, but you can cache ... http://stackoverflow.com/questions/533861/writing-a-cache-function-in-vb-net[^] The cache class ... http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx[^]

                    M Offline
                    M Offline
                    Member 10118706
                    wrote on last edited by
                    #9

                    Prissy, Thank you very much for the code I appreaciate the help im glad Dave explained it to me as this would of been cheating :P :-O

                    D 1 Reply Last reply
                    0
                    • M Member 10118706

                      Prissy, Thank you very much for the code I appreaciate the help im glad Dave explained it to me as this would of been cheating :P :-O

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      I don't do it any other way! Good Luck!

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      • P PrissySC

                        Exactly the same way that I do on the accounting side of my apps, but I exlicitly set out to cache. Sorry to inject! I had curiosity get the best of me as I was looking at the code and realizing that it is a textbook example has answered the question. Once again, sorry! Thanks for the replies, and let me know if you solve the threading problem. LOL ... Attempted multitasking this day. :wtf:

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        Threading problem?? What threading problem??

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        1 Reply Last reply
                        0
                        • M Member 10118706

                          Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.

                          Sub Main
                          console.writeline(getmyresult(2)) 'takes a while'
                          console.writeline(getmyresult(3)) 'takes a while'
                          console.writeline(getmyresult(2)) 'Should be instant'
                          console.writeline(getMyresult(3)) 'Should be instant'
                          End Sub

                          function getMyresult(X as interger) as integer
                          dim Y as integer=LongCompute(X)
                          return Y
                          end function

                          function LongCompute(X as integer) as integer
                          system.threading.thread.sleep(1000)
                          return x^2
                          end function

                          C Offline
                          C Offline
                          Christianirwan
                          wrote on last edited by
                          #12

                          Is it VB 6 or VB .NET? I don't know about return statement in VB 6? I'm forgot to choose Question. Oh, it's a VB .NET Discussion. I don't know. Sorry.

                          M 1 Reply Last reply
                          0
                          • C Christianirwan

                            Is it VB 6 or VB .NET? I don't know about return statement in VB 6? I'm forgot to choose Question. Oh, it's a VB .NET Discussion. I don't know. Sorry.

                            M Offline
                            M Offline
                            Member 10118706
                            wrote on last edited by
                            #13

                            Haha

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups