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
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..
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?
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.
Re: Count Items in an array
lol easier said then done :(
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;
}
}
}
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.
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
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.
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;
}
}
}
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.
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;
}
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;
}
}
Re: Count Items in an array
Re: Count Items in an array
yeah what i have above works perfectly, but i want to check for errors now
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...
Re: Count Items in an array
i wanted to throw an error if its less then 4 in lenght
Re: Count Items in an array
So why not just use a simple if() check to find out?
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;
}
}
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.
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.