|
-
Jan 25th, 2008, 09:57 AM
#1
Thread Starter
Frenzied Member
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];
}
}
}
-
Jan 25th, 2008, 11:05 AM
#2
Thread Starter
Frenzied Member
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
-
Jan 25th, 2008, 11:41 AM
#3
Re: Count Items in an array
For a text box (or any string)
c# Code:
string myStr = "bleh"; MessageBox.Show(myStr.Length.ToString());
For an array:
c# Code:
string[] myStr = { "hi", "bye", "never" }; MessageBox.Show(myStr.Length.ToString());
That should be string[] for the second one... doesn't seem to like that character..
-
Jan 25th, 2008, 11:45 AM
#4
Thread Starter
Frenzied Member
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?
-
Jan 25th, 2008, 11:54 AM
#5
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.
-
Jan 25th, 2008, 11:56 AM
#6
Thread Starter
Frenzied Member
Re: Count Items in an array
lol easier said then done
-
Jan 25th, 2008, 12:01 PM
#7
Thread Starter
Frenzied Member
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;
}
}
}
-
Jan 25th, 2008, 12:22 PM
#8
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.
-
Jan 25th, 2008, 12:28 PM
#9
Thread Starter
Frenzied Member
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
-
Jan 25th, 2008, 12:49 PM
#10
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.
-
Jan 25th, 2008, 01:18 PM
#11
Thread Starter
Frenzied Member
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;
}
}
}
-
Jan 25th, 2008, 01:27 PM
#12
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.
-
Jan 25th, 2008, 01:51 PM
#13
Thread Starter
Frenzied Member
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;
}
-
Jan 25th, 2008, 01:55 PM
#14
Thread Starter
Frenzied Member
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;
}
}
-
Jan 25th, 2008, 02:15 PM
#15
Re: Count Items in an array
-
Jan 25th, 2008, 02:21 PM
#16
Thread Starter
Frenzied Member
Re: Count Items in an array
yeah what i have above works perfectly, but i want to check for errors now
-
Jan 25th, 2008, 02:23 PM
#17
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...
-
Jan 25th, 2008, 02:26 PM
#18
Thread Starter
Frenzied Member
Re: Count Items in an array
i wanted to throw an error if its less then 4 in lenght
-
Jan 25th, 2008, 02:31 PM
#19
Re: Count Items in an array
So why not just use a simple if() check to find out?
-
Jan 25th, 2008, 02:57 PM
#20
Thread Starter
Frenzied Member
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;
}
}
-
Jan 25th, 2008, 02:59 PM
#21
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.
-
Jan 26th, 2008, 06:09 AM
#22
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|