I was wondering if you cloud help me?
I have an array that consist of items and prices and qty.
If the item exist it the array it must update the price and the qty, If it doesn't exits it must be added
Here is my code that i have tried:
if (line.Contains(ItemCode))
{
string[] details = line.Split(new string[] { "|" }, StringSplitOptions.None);
{
for (int i = 0; i < details.Length; i++)
{
if (details[i].Contains(ItemCode))
{
string[] line_details = details[i].Split(',');
string replace = line_details[2].Trim() + "," + line_details[3].Trim();
double NewQty = double.Parse(Qty) + double.Parse(line_details[2]);
double NewPrice = (double.Parse(UnitPrice) * double.Parse(Qty));
double NewUnitPrice = NewPrice + double.Parse(line_details[3]);
string new_replace = NewQty + "," + NewUnitPrice;
line = line.Replace(replace, new_replace);
}
}
}
}
else
{
line = line + "\"Detail\",0," + Qty + "," + (double.Parse(UnitPrice) * double.Parse(Qty)) + "," + InclusivePrice + ",\"" + UnitUsed + "\"," + TaxType + "," + DiscountType + "," + DiscountPercentage + ",\"" + ItemCode + "\",\"" + Description + "\"," + SearchType + "," + "\"\"" + ",\"" + MultiStore + "\"|" + Environment.NewLine;
}
it is not working could you maby assist me on this?
Arrays in C# cannot have entries added to them after being initialised. You're better off using a List<String> instead, where you can add and remove entries from the list. Alternatively consider a Dictionary<Int32, String>, which would let you use the ItemCode as an identifier to make finding a given entry easier.
As a furthur point, instead of storing all your item data in a delimited string, make a new Class for them, with the various details as properties, and then you can use a theoretical Dictionary<Int32, ItemObject> for better clarity.
Related
I have text that contains spaces.
I want all the possibilities. For example, merge a space and leave another.
For example, this is the first possibility:
the first word, then a space, the second word, then a space, then the third word.
The second possibility:
the first word, then without a space, then the second word, then a space, then the third word.
The third possibility:
the first word, then a space, then the second word, then without a space, then the third word ... etc..
I want to do this, but in a loop, especially if the number of words is more than five, six, or more.
static void Main(string[] args)
{
string test = "aaa bbb ccc";
var ch = test.Split(' ');
var t1 = ch[0] + " " + ch[1] + " " + ch[2];
var t2 = ch[0] + "" + ch[1] + " " + ch[2];
var t3 = ch[0] + " " + ch[1] + "" + ch[2];
var t4 = ch[0] + "" + ch[1] + "" + ch[2];
Console.WriteLine(t1);
Console.WriteLine(t2);
Console.WriteLine(t3);
Console.WriteLine(t4);
string test2 = "aaa bbb ccc ddd";
var ch2 = test2.Split(' ');
var z1 = ch2[0] + " " + ch2[1] + " " + ch2[2] + " " + ch2[3];
var z2 = ch2[0] + "" + ch2[1] + " " + ch2[2] + " " + ch2[3];
var z3 = ch2[0] + " " + ch2[1] + "" + ch2[2] + " " + ch2[3];
var z4 = ch2[0] + " " + ch2[1] + " " + ch2[2] + "" + ch2[3];
var z5 = ch2[0] + "" + ch2[1] + "" + ch2[2] + " " + ch2[3];
var z6 = ch2[0] + " " + ch2[1] + "" + ch2[2] + "" + ch2[3];
var z7 = ch2[0] + "" + ch2[1] + "" + ch2[2] + "" + ch2[3];
Console.WriteLine(z1);
Console.WriteLine(z2);
Console.WriteLine(z3);
Console.WriteLine(z4);
Console.WriteLine(z5);
Console.WriteLine(z6);
Console.WriteLine(z7);
Console.ReadLine();
}
So if we have n words we have n - 1 possible spaces. Let 0 be absence of space, when 1 be a presence of space:
[0, 0, 0] => Word1Word2Word3Word4
[0, 0, 1] => Word1Word2Word3 Word4
[0, 1, 0] => Word1Word2 Word3Word4
[0, 1, 1] => Word1Word2 Word3 Word4
...
[1, 1, 1] => Word1 Word2 Word3 Word4
So far so good we should enumerate all 2 ^ (n - 1) binary masks:
using System.Collections.Generic;
using System.Linq;
using System.Text;
...
private static IEnumerable<string> Solution(params string[] words) {
return Enumerable
.Range(0, 1 << (words.Length - 1))
.Select(mask => {
StringBuilder sb = new StringBuilder(words[0]);
for (int i = 0; i < words.Length - 1; ++i) {
if ((mask & (1 << i)) != 0)
sb.Append(' ');
sb.Append(words[i + 1]);
}
return sb.ToString();
});
}
Demo (fiddle):
var result = string.Join(Environment.NewLine, Solution("A", "B", "C", "D"));
Console.WriteLine(result);
Output:
ABCD
A BCD
AB CD
A B CD
ABC D
A BC D
AB C D
A B C D
The code you provided works for a limited number of words, but becomes impractical as the number of words increases. A more efficient way to generate all possibilities of a string containing spaces is to use loops and recursion. Here's an example of a function that takes a string and generates all possible combinations of that string (with and without spaces):
private static List<string> GenerateCombinations(string[] words, int index)
{
if (index == words.Length)
{
return new List<string> { "" };
}
var result = new List<string>();
var subCombinations = GenerateCombinations(words, index + 1);
foreach (var subCombination in subCombinations)
{
result.Add(words[index] + subCombination);
result.Add(words[index] + " " + subCombination);
}
return result;
}
You can call this function with the string you want to generate combinations for:
string test = "aaa bbb ccc ddd";
var words = test.Split(' ');
var combinations = GenerateCombinations(words, 0);
foreach(var combination in combinations)
{
Console.WriteLine(combination);
}
The function uses recursion to generate all possible combinations of strings. The index parameter is used to keep track of the current word in the word array, and the recursion stops when the index equals the length of the word array. The function uses two lists to store combinations: one for the current index and one for the next index. The current list is added to the next list in two ways, with and without spaces.
This approach will work for any number of words and will generate all possible combinations efficiently.
I am creating an Array using List in C#. List data coming from a web service. I am using this array to create a table in PDF(spire.pdf). I want to add a header every 15th element. I can add the first one. From my method header value replaced an element in the array afterwords.
For example if I have 35 element in the list and I want to add the first row as the header row rankListArray[i] = "No;" + "BRN;" + "Exp.Date;" + "Vehicle No;" + "Policy No;" + "Name of Insured;" + "Additional Covers;" + "Sum Insured;" + "NCB;" + "Amount Due"; to the array.
Then in the 15th position again I need to add this row. Then in the 30th position
public class PrintedMotorRenewalCompactData
{
public string DUE_DATE { get; set; }
public string CUST_NAME { get; set; }
public string POLICY_NO { get; set; }
public string VEHI_NO { get; set; }
public double NCB_PERC { get; set; }
public double SUM_INS { get; set; }
public double PRM_AMT { get; set; }
public string COVERS { get; set; }
public int BRANCH { get; set; }
public string BRANCH_NAME { get; set; }
public string POL_STAT { get; set; }
}
IList<PrintedMotorRenewalCompactData> rankList;
for (int i = 0; i < rankList.Count; i++)
{
if ((i % 15) == 0) {
rankListArray[i] = "No;" + "BRN;" + "Exp.Date;" + "Vehicle No;" + "Policy No;" + "Name of Insured;" + "Additional Covers;" + "Sum Insured;" + "NCB;" + "Amount Due";
rankListArray[i + 1] =
(i + 1) + ";" +
rankList[i].BRANCH + ";" +
rankList[i].DUE_DATE + ";" +
rankList[i].VEHI_NO + ";" +
rankList[i].POLICY_NO + ";" +
rankList[i].CUST_NAME + ";" +
rankList[i].COVERS + ";" +
rankList[i].SUM_INS.ToString("N", CultureInfo.InvariantCulture) + ";" +
rankList[i].NCB_PERC + ";" +
rankList[i].PRM_AMT.ToString("N", CultureInfo.InvariantCulture);
} else {
rankListArray[i + 1] =
(i + 1) + ";" +
rankList[i].BRANCH + ";" +
rankList[i].DUE_DATE + ";" +
rankList[i].VEHI_NO + ";" +
rankList[i].POLICY_NO + ";" +
rankList[i].CUST_NAME + ";" +
rankList[i].COVERS + ";" +
rankList[i].SUM_INS.ToString("N", CultureInfo.InvariantCulture) + ";" +
rankList[i].NCB_PERC + ";" +
rankList[i].PRM_AMT.ToString("N", CultureInfo.InvariantCulture);
}
}
Let's simplify you issue. I will assume that you are to produce a List<string> from your List<customObject>.
We start with a List with all letters of the alphabet. We will insert a header every 3 rows.
var header = "Hi, I'm Header!";
var headerRepeatsEvery = 3.0; // .0 to avoid int division.
var inputs = Enumerable.Range((int)'A', 26).Select(x => ((char)x).ToString()).ToList();
Writing a part of exected result we have :
Header!
1;A
2;B
3;C
Header!
4;D
5;E
The header are in index : {0,4,8,12,16, .. etc}
That look like : X * 4
Let's try to find how many of those header we will need :
List Size | Header Count
0 0
1 1
2 1
3 1
4 2
A simple division won't give use the result 4/3 = 1.3333.
We need to round it to the next int, and that's Math.Ceiling
Now the rest is simple for every Header index, we have to insert the header.
foreach (var index in headerIndexes)
{
alpha.Insert(index, headerText);
}
Now we have our expected result we just need to make it a array : ToArray().
Online Demo
Your final code may look like this :
var csvLikeLines = rankList.Select((x, i) => $"{i + 1};{x.property1};{x.property2}"));
var indexes = Enumerable.Range(0,(int)Math.Ceiling(csvLikeLines.Count()/ 15))
.Select(x => x * 16);
foreach (var index in indexes)
{
csvLikeLines.Insert(index, headerTest);
}
rankListArray = csvLikeLines.ToArray();
I will recommend using something more rebust for concataining property value with ';' semi colon. Soon enought one of the value will have a \n, \r, or semi colon and everything will go boom.
As you are using it to create a report the strongest argument for using a mapper like CsvHelper is that you will be able to arrange the column order, delete, or rename one. Without going through the nightmareof remembering that column 7 is 'propertyFoo'. it will keep the header and the column easly maintanable.
I have, code who do for me "x + y = z"
if (command.Contains("+")) // string polecenie = "";'
{
polecenie = "{" + command + ")";
polecenie = polecenie.Replace("+", "}(");
double a = Convert.ToDouble(Between(polecenie, "{", "}"));
double b = Convert.ToDouble(Between(polecenie, "(", ")"));
double wyn = a + b;
richTextBox1.Text += a + " + " + b + " is " + wyn + "\r\n";
}
And when 'command' is "4+5","3 + 4" or something like this code works, but when i try to do "4 + 3 + 23" it don't work. Final string with starting 'command' "4+5+6", polecenie is: "{4}(5}(6)"... The Between Method:
public string Between(string content, string First, string Last)
{
string end = "";
int Plc1 = content.IndexOf(First) + First.Length;
int Plc2 = content.IndexOf(Last);
end = content.Substring(Plc1, Plc2 - Plc1);
return end;
}
How can I do that? (I want it to work with all possible additions ("4+4","34+5+54","43+4+65+54"...)
You could use the DataTable object to not re-invent the wheel.
richTextBox1.Text = string.Format("{0} is {1}\r\n", command,
(new System.Data.DataTable()).Compute(command, string.Empty));
This would support +, -, *, / and % (mod) operators. For more: https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
I have values assigned to a variable as follows:
private string[] numbers = new string[] {"1" , "2" , "3" , "4" , "5"};
then i take my number variable and i filter through each as follows
protected void display()
{
bonusdiv.Visible = true;
panelbonus.InnerHtml = "";
foreach (string option in numbers)
{
string optiontext = option;
if (option.Length == 1)
{
optiontext = "" + optiontext;
}
if (panelBonusselections.Contains(option))
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"optionselected\"/>";
}
else
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"option\"/>";
}
}
}
now the question i pose is,i want to assign the values mentioned above into a single instance,something like
numbers=[5]; //but this must still use the same logic as above to store the values from 1 up to 5.Then how do i cater for this change in my foreach loop?
will it be something like the bellow?
int numbers=[5]
for (i=1,i>5, i++) {
foreach (string option in numbers)
{
string optiontext = option;
if (option.Length == 1)
{
optiontext = "" + optiontext;
}
if (panelBonusselections.Contains(option))
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"optionselected\"/>";
}
else
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"option\"/>";
}
You can use LINQ's Enumerable.Range() to create a sequence and Select() to project it to strings:
string[] numbers = Enumerable.Range(1, 5).Select(i => i.ToString()).ToArray();
Or as a method:
string[] GetNumbersStringArray(int length)
{
return Enumerable.Range(1, length).Select(i => i.ToString()).ToArray();
}
First let me present the code:
private void AdicionarFormula(string x, string y, string Materia)
{
MessageBox.Show("%" + x + " = " + y + "#" + Materia);
StreamWriter Escritor = new StreamWriter(Properties.Settings.Default.ArquivoDeFormulas, true);
Escritor.WriteLine("%" + x + " = " + y + "#" + Materia);
Escritor.Close();
LerFormulas(true);
}
The issue is: instead of the output being "%x = y#Materia" it is actually "
%x = y
#Materia"
I dont want it to create a new line for "#Materia".
Your parameter 'y' contains char a newline. For example, try it in your code:
string.Format("%" + x + " = " + y + "#" + Materia).Replace("\n","")