|
oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string.
It is just like the SQL statement that we write.
e.g. Select * from table where name like ('abc','def','ghi')
I am trying to send the variable in this format to the stored procedure.
|
|
|
|
|
It should be outside the loop, as in the example I posted.
|
|
|
|
|
|
Thanks, but I just tried this,
strHolderString = strServerName.ToString();
strHolderString = strHolderString.TrimEnd(',');
strFinalList = new StringBuilder(strHolderString);
here the strHolderString is a string variable and I am getting the result that I was looking for.
Thanks once again to everyone.
|
|
|
|
|
Just decrement the Length. Why create two Strings and another StringBuilder?
|
|
|
|
|
What Piebald said; or else what I often do when I do not want the StringBuilder:
string result="";
string sep="";
foreach(string s in strings) {
result+=sep+"\""+s+"\"";
sep=",";
}
|
|
|
|
|
I'd still use String.Format -- I find it easier to read than a bunch of concatenations.
|
|
|
|
|
I'd like to go with PIEBALDconsult on this one, but thanks for the help
|
|
|
|
|
I use it sparingly, string.Format I mean.
|
|
|
|
|
I use it whenever I can -- muaa haa ha ha!
|
|
|
|
|
strHolderString = strServerName.ToString();
strHolderString = strHolderString.TrimEnd(',');
strFinalList = new StringBuilder(strHolderString);
I just tried the above and it works for me. Here the strHolderString is a string variable and I used the TrimEnd function to get rid of the trailing comma. The strFinalList is a StringBuilder variable.
Thanks once again to all
|
|
|
|
|
There's pretty much no reason to avoid StringBuilder, it's in the core Framework and the memory footprint of creating a new object is likely to be less than all the wasted strings you make in a concatenation session.
|
|
|
|
|
Based on your posts below, have you tried using string.Join?
string example = String.Join(", ", string[] exList);
"I have a theory that the truth is never told during the nine-to-five hours. "
— Hunter S. Thompson
|
|
|
|
|
|
An alternative approach is to use a list of strings instead of a StringBuilder to build your list and then concatenate them as you need. Here's one way to do this:
public class FormattedStrings : List<string>()
{
public override string ToString()
{
StringBuilder sb = new StringBuilder();
bool started = false;
foreach (string item in this)
{
if (started)
{
sb.AppendFormat(",\"{0}\"", item);
}
else
{
started = true;
sb.AppendFormat("\"{0}\"", item);
}
}
return sb.ToString();
}
} I am assuming that you have assigned the @ symbol when you built your StringBuilder .
|
|
|
|
|
Ew, yuck, duplicated code. Needless boolean, needless if/else .
Stick the comma on the end and decrement it off when you're done.
|
|
|
|
|
To be fair, this was just knocked up in the CP editor and doesn't reflect what I would push out the door.
I did look at doing it your way, but saw that you'd already posted that so it seemed that there wasn't much point to showing something that was already done - this is purely to stimulate the mind into thinking of other ways of solving the problem.
|
|
|
|
|
I want to encrypt image by other image using Xor operation in c# as follow:
private byte[] EncypteXoRImage(byte[] inputImage, byte[] byImage)
{
byte[] Result = (byte[])inputImage.Clone();
if (byImage == null)
return Result;
for (int k = inputImage.Length / 3; k < inputImage.Length; k++)
{
Result.SetValue(Convert.ToByte((byte)Result.GetValue(k) ^ (byte)byImage.GetValue(k % byImage.Length)), k);
}
return Result;
}
where inputImage is picture which i want to encrypt,the problem is when i want to display encrypted picture on the picturebox,the result is:
the encrypted part was displayed by a gray background
|
|
|
|
|
Code doesn't seem to make sense. Could you try this?
private byte[] EncypteXoRImage(byte[] inputImage, byte[] byImage)
{
if (byImage == null)
return (byte[])inputImage.Clone();
byte[] result = new byte[inputImage.Length];
for (int k = 0; k < result.Length; k++)
{
result[k] = (byte)(inputImage[k] ^ byImage[k]);
}
return result;
}
|
|
|
|
|
You can make your method a lot easier to understand by using actual array references instead of GetValue and SetValue - and also by throwing awy unnecessary conversions:
private byte[] EncypteXoRImage(byte[] inputImage, byte[] byImage)
{
byte[] Result = (byte[])inputImage.Clone();
if (byImage == null)
return Result;
for (int k = inputImage.Length / 3; k < inputImage.Length; k++)
{
Result[k] ^= byImage[k % byImage.Length];
}
return Result;
}
However, it will have no affect on your problem - since the code is equivalent.
Out of interest, I tried this with two of my pictures:
private void myButton_Click(object sender, EventArgs e)
{
Image i = Image.FromFile(@"D:\Temp\MyPic.jpg");
Image j = Image.FromFile(@"D:\Temp\MyOtherPic.jpg");
pictureBox1.Image = i;
pictureBox2.Image = j;
MemoryStream msI = new MemoryStream();
i.Save(msI, System.Drawing.Imaging.ImageFormat.Bmp);
MemoryStream msJ = new MemoryStream();
j.Save(msJ, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] res = EncypteXoRImage(msI.ToArray(), msJ.ToArray());
MemoryStream msK = new MemoryStream(res);
Image k = Image.FromStream(msK);
pictureBox3.Image = k;
}
private byte[] EncypteXoRImage(byte[] inputImage, byte[] byImage)
{
byte[] Result = (byte[])inputImage.Clone();
if (byImage == null)
return Result;
for (int k = inputImage.Length / 3; k < inputImage.Length; k++)
{
Result[k] ^= byImage[k % byImage.Length];
}
return Result;
}
Apart from being a pretty useless encryption technique (I could work out what both pictures were from the composite, pretty much, in the same way that I can read Captcha codes), it worked - I did not get a solid grey background with either version of your method.
So I assume it is your conversion from Image to byte array that is at fault.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Hi,
I have this error in my code that am having difficulties in rectifying it. Initially, my code looked like this..
private void CalculateTheMortgage()
{
double loanAmount = (double)txtLoanAmount.CurrentValue;
double taxesPerYear = (double)txtPropertyTax.CurrentValue;
double downPayment = (double)txtDownPayment.CurrentValue;
double interestRate = (double)udInterest.Value / 100;
double termOfLoan = (double)(udTerm.Value * 12);
double propertyTax = (double)txtPropertyTax.CurrentValue;
double insurance = (double)txtInsurance.CurrentValue;
double payment = (loanAmount - downPayment) * (Math.Pow((1 + interestRate/12), termOfLoan) * interestRate)/(12 * (Math.Pow((1+interestRate/12), termOfLoan) - 1));
payment = payment + (propertyTax + insurance) / 12;
txtPayment.CurrentValue = (int)payment;
}
and i changed it a little to this...
private void CalculateTheMortgage()
{
double loanAmount = Convert.ToDouble(txtLoanAmount.Text);
double taxesPerYear = Convert.ToDouble(txtPropertyTax.Text);
double downPayment = Convert.ToDouble(txtDownPayment.Text);
double interestRate = (double)udInterest.Value / 100;
double termOfLoan = (double)(udTerm.Value * 12);
double propertyTax = Convert.ToDouble(txtPropertyTax.Text);
double insurance = Convert.ToDouble(txtInsurance.Text);
double payment = (loanAmount - downPayment) * (Math.Pow((1 + interestRate / 12), termOfLoan) * interestRate) / (12 * (Math.Pow((1 + interestRate / 12), termOfLoan) - 1));
payment = payment + (propertyTax + insurance) / 12;
txtPayment.CurrentValue = (int)payment;
}
which removed the error message below. Now i wanna change this
txtPayment.CurrentValue = (int)payment; piece of code so that the same error cannot occur again. There is a blue line under 'CurrentValue'
This is the error am getting...
'System.Windows.Forms.TextBox' does not contain a definition for 'CurrentValue' and no extension method 'CurrentValue' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)
Please help me here
Thanks
|
|
|
|
|
textbox.Text ?
|
|
|
|
|
When i write it like this
txtPayment.Text = (int)payment; am getting this error
Cannot implicitly convert type 'int' to 'string' with a blue line under '(int)'
|
|
|
|
|
Don't convert it to int when trying to put it into a textbox.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
txtPayment.Text = payment.ToString();
or:
txtPayment.Text = payment.ToString("0.00 $");
|
|
|
|