Results 1 to 22 of 22

Thread: Count Items in an array

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Count Items in an array

    I was trying to make a text box, where i can put say 10 digit numbers 2423423434343

    then i will have a class that will count the numbers in the text box, and put *** for all the number except the last 4.

    Im not sure how to count the numbers that are entered, then loop through them.

    any ideas?

    Code:
    namespace Arrays
    {
        public class CreditCard
        {
            public static int cardDisplay(int cc)
            {
                int[] card = new int[cc];
                
            }
        }
    }

  2. #2

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    better idea, i think insted of doing an array is there a way to figure out how long a text box is? so if someone enters 23543343 it will count how many numbers are there 8, then take those numbers, and make it so the last 4 only show, and the rest are ****************8

  3. #3
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    For a text box (or any string)
    c# Code:
    1. string myStr = "bleh";
    2. MessageBox.Show(myStr.Length.ToString());

    For an array:
    c# Code:
    1. string[] myStr = { "hi", "bye", "never" };
    2. MessageBox.Show(myStr.Length.ToString());

    That should be string[] for the second one... doesn't seem to like that character..

  4. #4

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    Thanks timeshifer for the info...

    now i have a text box on my default.aspx page, and i made a class called displaycard info, with a function that takes the textbox paramater and counts it, then somehow it needs to return the same number entered but only have the last 4 show so if i enter 44455556666, it should kick back ****-****-****-6666

    anyideas?

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    Easy. Find the length of the text box, store it as an int. Find the last four characters of the text box, save it as a string. Send back (length-4) stars, + string.

  6. #6

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    lol easier said then done

  7. #7

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    this is what i have,but cant get it to work, i just wanted to pass in the number that was entered into the text box, then have it run through this class, and have the class kick out the number **** etc how i stated above..

    Code:
    namespace Arrays
    {
        public class CreditCard
        {
            public static int cardlenght;
    
            public static int cardDisplay(int cc)
            {
                string myStr = Convert.ToString(cc);
                cardlenght = myStr.Length.ToString();
    
                return cc;
            }
        }
    }

  8. #8
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    I'm assuming this is getting called through a button event of some kind...

    Regardless. YOu have the length of the string. You want to mask all but the last four digits. So, just run a simple for loop for the length - 4 and build a string with a * per loop.

  9. #9

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    yes, i have it in a button click

    Code:
            protected void Button1_Click(object sender, EventArgs e)
            {
                int cc = Convert.ToInt32(TextBox1.Text);
    
                TextBox2.Text = CreditCard.cardDisplay(cc).ToString();
    
                Label2.Text = CreditCard.cardlenght.ToString();
            }
    sorry for my stupidness, but i have no idea how to loop through and return -4 lol

  10. #10
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    You don't need to convert it to an int, since you're treating it like a string. Check out MSDN online for the structure of a for loop, and see how far you can get with it.

  11. #11

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    i looked still cant figure it out

    Code:
    namespace Arrays
    {
        public class CreditCard
        {
            public static string cardlast4;
    
            public static int cardDisplay(int cc)
            {
                string myStr = Convert.ToString(cc);
                cardlast4 = myStr.Substring(myStr.Length,-4);
    
                return cc;
            }
        }
    }

  12. #12
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Count Items in an array

    Change
    Code:
    cardlast4 = myStr.Substring(myStr.Length,-4);
    To
    Code:
    cardlast4 = myStr.Substring(myStr.Length -4, 4);
    The MSDN is a valuable tool.

    I'm sorry if this comes off as rude but if you're not sure on simple string manipulation, how do I know you're going to keep my credit card information secure? Sorry but I wouldn't trust any eCommerce applications you write until you're more seasoned.
    Last edited by Kasracer; Jan 25th, 2008 at 01:31 PM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  13. #13

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    so how do i add *** for the begiign now?

    Code:
            public static string cardLast4(string cc)
            {
                string last4;
                last4 = cc.Substring(cc.Length - 4, 4);
    
                return last4;
            }

  14. #14

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    k got it

    how to i error check this

    Code:
            public static string cardLast4(string cc)
            {
                string last4;
                last4 = "********" + cc.Substring(cc.Length - 4, 4);
    
                return last4;
            }
        }

  15. #15
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    Does that work?

  16. #16

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    yeah what i have above works perfectly, but i want to check for errors now

  17. #17
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    You have to be a bit more clear than that. You're asking for error checking help, but if the code works, then there are no errors...

  18. #18

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    i wanted to throw an error if its less then 4 in lenght

  19. #19
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    So why not just use a simple if() check to find out?

  20. #20

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Count Items in an array

    i did this

    Code:
            public static string GetHiddenCreditCardNumber(string creditCard)
            {
                string last4;
    
                if (creditCard.Length > 4)
                {
                    last4 = "********" + creditCard.Substring(creditCard.Length -4);
                    return last4;
                }
                else
                {
                    return creditCard;
                }
            }

  21. #21
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Count Items in an array

    I do hope you've been noticing how simple this code really is. Taking it the rest of the way shouldn't be hard for you.

  22. #22
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Count Items in an array

    Shouldn't you throw new Exception in the else {} block as you said?

    Also, you said if it's less than 4 in length, so your if statement should have > 3 or >= 4, not >4.

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