Tech2 Forums - Powered by vBulletin

Increment return value of method every time it is called

This is a topic on Increment return value of method every time it is called within the Programmers Corner forums, part of the Technology category; Hi, I want to use a function in the code file of ASP.NET such that everytime the function is called, ...

Results 1 to 8 of 8
  1. #1
    Uber Newbie
    Join Date
    Jul 2009
    Posts
    0
    Liked
    0 times
    Reputation points
    0

    Increment return value of method every time it is called

    Hi,

    I want to use a function in the code file of ASP.NET such that everytime the function is called, its value increases by 1.

    I thought of a way but it didn't fully work out. I created a class for this and used a static variable in that but the lifetime of static variable in asp.net is till the time the app does not close.

    Code:
    public class Class1
    {
    	public Class1()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}  
      private static Int32 i =0 ;
      public Int32 return_number()
      {
        i++;
        return i;
      }
    
    }
    Hence, the static variable keeps on storing these values until the app is restarted i.e

    Q. 1
    Q. 2
    Q. 3
    For the second time

    Q. 4
    Q. 5
    Q. 6


    but I want the serial number to increment from 1,2, 3 every time application starts as it should be. Any work around?

  2. #2
    Uber Newbie
    Join Date
    Jan 2009
    Location
    some where I belong...
    Posts
    0
    Liked
    0 times
    Reputation points
    0

    Re: Increment return value of method every time it is called

    Quote Originally Posted by optimus_prime
    Hi,
    but I want the serial number to increment from 1,2, 3 every time application starts as it should be. Any work around?
    i don know asp but the logic remains the same, here you go..
    the variable is lost as soon as the application is closed so
    use a file to store the value and next time read value form the file. I think this should solve your problem.

  3. #3
    Uber Newbie
    Join Date
    Feb 2009
    Posts
    0
    Liked
    0 times
    Reputation points
    0

    Re: Increment return value of method every time it is called

    i was thinking same as Dronzer ..

  4. #4
    Uber Newbie
    Join Date
    Jun 2009
    Location
    Kolkata
    Posts
    0
    Liked
    0 times
    Reputation points
    0

    Re: Increment return value of method every time it is called

    Quote Originally Posted by aadisriram
    i was thinking same as Dronzer ..
    Thats the Only way of thinking for this problem.. Unless the data is stored it cant be retained..

  5. #5
    Uber Newbie
    Join Date
    Jul 2010
    Posts
    0
    Liked
    2 times
    Reputation points
    10

    Re: Increment return value of method every time it is called

    im not asp.net coder but php coder but can give you idea.
    you may store it to a file or in sql db ( if its just one variable then dont recommend sql db).

  6. #6
    Tech2 Members uniquerockrz's Avatar
    Join Date
    Apr 2010
    Location
    Kolkata
    Posts
    171
    Liked
    0 times
    Reputation points
    2

    Re: Increment return value of method every time it is called

    Store it in a file, retrive it on run
    Oops!

  7. #7
    Uber Newbie
    Join Date
    Jul 2010
    Posts
    0
    Liked
    2 times
    Reputation points
    10

    Re: Increment return value of method every time it is called

    I think bumping of old topics is not allowed.
    anyways, seems like you bumped a topics of my first day on chip. :mrgreen:

  8. #8
    Tech2 Members uniquerockrz's Avatar
    Join Date
    Apr 2010
    Location
    Kolkata
    Posts
    171
    Liked
    0 times
    Reputation points
    2

    Re: Increment return value of method every time it is called

    Had no work, just was answering a few unsolved posts, made a mistake at this one
    Oops!

Similar Threads

  1. To Talkaboom
    By htnakirs in forum PC Hardware
    Replies: 6
    Last Post: 12-11-2006, 08:34 PM

Posting Permissions

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