Results 1 to 10 of 10

Thread: C# beginner Numering question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    C# beginner Numering question

    Hello,

    We have a program at work wich runs on C# (I am new to this).

    There is a function (seen in the code below) which numbers ID's.

    When I choose 3 letters like 'AZM' the numbering works.

    AZM001
    AZM002
    AZM003
    etc...

    But when I want to use numbers: 001001, 001002, 001003 it doesnt work.
    I get:
    001001 (this is good)
    011002 (this should be 001002)
    011003 (this should be 001003)

    I hope that you guys can help.

    Code:
    function GetPaddedPatNo(LastID, padding)
    {
      var snums = '0123456789';
      var sres = '';
      var i = 0;
       // remove all non-numerical characters
     
     for(i=0; i< LastID.length; i++)
      {
        if(snums.indexOf(LastID.substr(i,1)) >=  0)
          sres += LastID.substr(i,1);
      }
    
      var ires = new Number(sres);
      if((ires.NaN) || (ires == 0))
        ires = 0;
    
      if(ires > 9999)
        ires = ires % 10000;
    
     var nextPat = ires + 1;
      sres = nextPat + '';
      while(sres.length < padding)
        sres = '0' + sres;
    
      return(sres);
    }
    @INST_SHORTCODE + GetPaddedPatNo(LastPfxPatNo(@INST_SHORTCODE), 3);

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

    Re: C# beginner Numering question

    Do you want to add 00 the result ?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: C# beginner Numering question

    Quote Originally Posted by danasegarane View Post
    Do you want to add 00 the result ?
    I don't know if I understand you.

    001 is the studycode.
    Per study there are multiple participant which incriments by 1 001, 002, etc..

    So if a student is added to the study it looks like:
    001001,001002,001003,etc

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

    Re: C# beginner Numering question

    This worked for me

    Code:
    function padNumber(number,length) {
        var str = '' + number;
        while (str.length < length)
            str = '0' + str;
        return str;
    }
     function GetPaddedPatNo(number,pad)
      {
         var mystr=stripAlphaChars(number);
    	 var ires = new Number(mystr);
    	 var newNumber;
    	  if((ires.NaN) || (ires == 0))
    		ires = 0;
    
    	  if(ires > 9999)
    		ires = ires % 10000;
    
    	 alert(padNumber(mystr,pad));
    	 
    
      }
      function stripAlphaChars(pstrSource) 
    { 
    var m_strOut = new String(pstrSource); 
        m_strOut = m_strOut.replace(/[^0-9]/g, ''); 
    
        return m_strOut; 
    }
    call as

    return GetPaddedPatNo('4', 3);
    Last edited by danasegarane; Feb 11th, 2010 at 07:05 AM.
    Please mark you thread resolved using the Thread Tools as shown

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

    Re: C# beginner Numering question

    You don't need to write custom code to pad strings.
    csharp Code:
    1. var num = 1;
    2. var str = num.ToString("000");
    3.  
    4. MessageBox.Show(str);
    csharp Code:
    1. var num = 1;
    2. var str = num.ToString().PadLeft(3, '0');;
    3.  
    4. MessageBox.Show(str);
    csharp Code:
    1. var num1 = 1;
    2. var num2 = 2;
    3. var str = string.Format("{0:000}{1:000}", num1, num2);
    4.  
    5. MessageBox.Show(str);
    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

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

    Re: C# beginner Numering question

    I think question is in Javascript
    Please mark you thread resolved using the Thread Tools as shown

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

    Re: C# beginner Numering question

    Quote Originally Posted by danasegarane View Post
    I think question is in Javascript
    Now that I look at the original code, I think maybe you're right. That leads me to ask, why is this question in the C# forum?
    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

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

    Re: C# beginner Numering question

    Why indeed...Moved

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

    Re: C# beginner Numering question

    Please mark you thread resolved using the Thread Tools as shown

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: C# beginner Numering question

    Quote Originally Posted by jmcilhinney View Post
    Now that I look at the original code, I think maybe you're right. That leads me to ask, why is this question in the C# forum?
    Sorry for that.
    Normally I use VB, but a coworker asked me to take a look at it.

    Here is how is was solved:
    Code:
    function GetPaddedPatNo(LastID, padding)
    {
      var snums = '0123456789';
      var sres = '';
      var i = 0;
       // remove all non-numerical characters
     
     for(i=0; i< LastID.length; i++)
      {
        if(snums.indexOf(LastID.substr(i,1)) >=  0)
          sres += LastID.substr(i,1);
      }
    
      var ires = new Number(sres);
      if((ires.NaN) || (ires == 0))
        ires = 0;
    
      if(ires > 999)
        ires = ires % 1000;
    
     var nextPat = ires + 1;
      sres = nextPat + '';
      while(sres.length < padding)
        sres = '0' + sres;
    
      return(sres);
    }
    @INST_SHORTCODE + GetPaddedPatNo(LastPfxPatNo(@INST_SHORTCODE), 3);
    Thank you very much for helping guys..

    And see u soon at the VB.NET forums ;-)

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