I'm trying to read out the contents off a Setting inside my Application. Below is the code i'm having troubles with:
private bool checkGrid()
{
string playlists = Spotify_Extender.Properties.Settings.Default.Playlists;
MessageBox.Show(playlists);
string[] split1;
if (playlists.Contains(";"))
{
MessageBox.Show("Multiple Links");
split1 = playlists.Split(';');
}
else
{
MessageBox.Show("One Link");
split1 = new string[1];
split1[0] = playlists;
}
MessageBox.Show("Array Length: " + split1.Length);
int lines = 0;
for (int i = 0; i < split1.Length; i++)
{
MessageBox.Show("Check #" + i + " - " + split1[i] + " - Length: " + split1[i].Length);
if (split1[i].Length >= 22)
{
MessageBox.Show(i + " - " + split1[1]);
lines++;
}
}
int rows = this.playlistGrid.Rows.Count;
MessageBox.Show(lines + "");
if (rows == lines)
return true;
return false;
}
The code should be easy to understand and it should work as far as i am aware, but it doesn't. I entered this in my Setting:
If i run the program now, my first MessageBox prints out exactly what i entered, the second one prints out "One Link" and the third prints "Array Length: 1". Now we get to the part i'm having troubles with. The next Message is this:
So the length of the text is 22 as displayed in the MessageBox, but down below this statement isn't true:
if (split1[i].Length >= 22)
I'm really confused by this and it also does this when i check this:
if (split1[i] != "")
Any help is appreciated, because i don't know what to do, since my code should be fine. Thanks for your time!
You should have split[i] and not split[1]
Related
so I currently have a program that adds an item to a list in a format such as
username,Index it then adds one to the index in this code below however. It is only adding one to the item that has been added most recently.
Console.WriteLine("There are currently: " + AntiSpam.Count);
int Index = 0;
foreach (string s in AntiSpam)
{
Console.WriteLine("Found User: " + s.Split(',')[0]);
AntiSpam[Index] = s.Split(',')[0] + "," + (int.Parse(s.Split(',')[1]) + 1).ToString();
Index++;
}
Basically this returns the data There are currently: 10
Found User: someone. It then goes again for another loop of this code and shows the same result again.
EDIT
I have managed to make my code work by using this code
for (var i = 0; i < AntiSpam.Count; i++)
{
AntiSpam[i] = AntiSpam[i].Split(',')[0] + "," + (int.Parse(AntiSpam[i].Split(',')[1]) + 1).ToString();
Console.WriteLine("Text is {0}", AntiSpam[i]);
}
However if possible I would like to know why this works and the first doesn't
If you're going to be indexing a list, just do for rather than foreach. This avoids the need (and possible confusion) of using a separate variable to keep track of the AntiSpam.IndexOf(s) which is basically what you were trying to do with Index:
Console.WriteLine("There are currently: " + AntiSpam.Count);
int index;
string s;
for(int i=0; i < AntiSpam.Count, i++)
{
string[] parts = AntiSpam[i].Split(',');
username = parts[0];
Console.WriteLine("Found User: " + username);
if (parts.Length > 1)
{
index = int.Parse(parts[1])
AntiSpam[i] = username + "," + (index + 1).ToString();
}
}
I am trying to add a specific part of a string to a label in c#
I only want the string up to the space to be displayed (in line 6 of this code)
bool BoolSpace = s.Contains(" ");
if (BoolSpace == true)
{
int IntSpacePos = s.IndexOf(" ");
int StrPos = IntSpacePos - 1;
LblLmcCode1.Text = LblLmcCode1.Text + s(0, StrPos);
}
else
{
LblLmcCode2.Text = LblLmcCode2.Text + '\n' + s;
}
However line 6 is returning an error method name expected about the 's' of s(0, StrPos)
You should use Substring method:
s.Substring(0, StrPos);
All together with fewer lines:
LblLmcCode1.Text = LblLmcCode1.Text + s.Substring(0, s.IndexOf(" ") - 1);
Also as BoolSpace is a boolean itself no need to check if it is equal to true but it is enough to write if(BoolSpace), and better still just place the Contains in the statement:
if(s.Contains(" "))
{
LblLmcCode1.Text = LblLmcCode1.Text + s.Substring(0, s.IndexOf(" ") - 1);
}
else
{
LblLmcCode2.Text = LblLmcCode2.Text + '\n' + s;
}
I am trying to print a multiline string on a paper from my windows forms application and it does print fine but the formatting is applying only to the first line which is being printed slightly to the right and other lines are being aligned to left for which I would like all the lines to be printed just like the first line
Example
ID : value
Head : value
Hand : value
as you can see from the above ID is printer slightly to the right but head and hand lines are not aligned with the first line
sb.Append(strTempMessage + NEW_LINE);
strTempMessage = AlignNumeric(" ", PAGE_LEFT_MARGIN);
strTempMessage += AlignMessage(strPickSlipNote,0);
sb.Append(strTempMessage + NEW_LINE);
public string AlignMessage(string strMessage, int intBeginningBlankSpace)
{
strMessage = strMessage.Trim();
return AlignNumeric(strMessage, intBeginningBlankSpace);
}
public string AlignNumeric(string strMessage, int intBeginningBlankSpace)
{
intBeginningBlankSpace += strMessage.Length;
if (intBeginningBlankSpace > 0)
return strMessage.PadLeft(intBeginningBlankSpace, ' ');
else
return strMessage;
}
what changes should I make to get all the lines aligned?
I am not too sure if its the most efficient way doing this but you can add tab after appending new line e.g. sb.Append(strTempMessage + NEW_LINE + "\t");
This helped solve
public string FormatPickSlipNoteLines(string input)
{
if (string.IsNullOrEmpty(input))
return "";
else if (input.Length < PAGE_COLUMN_SIZE)
return ALIGN_PAGE_CENTER + input + ALIGN_PAGE_LEFT;
else
{
string[] str = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
StringBuilder sbFormatter = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
sbFormatter.Append(" " + str[i] + NEW_LINE);
}
return sbFormatter.Append(NEW_LINE + ALIGN_PAGE_LEFT).ToString();
}
}
I'm reading in some text, line by line, and I'd like to tokenize the words and create 1-grams and 2-grams, but I think there's a problem with my indexing because I either get an index error or it'll say that the item I'm trying to modify in my dictionary doesn't exist, which is totally weird, since I wrote the code to first make the dictionary item and if it already exists, to increment a counter.
Basically, my dictionaries are of the form (n-gram string, frequency int)
System.IO.StreamReader lines = new System.IO.StreamReader("myfile");
while (true)
{
string line = lines.ReadLine().ToLower();
if (line == null) break;
if (line.Trim().Length == 0) continue;
string[] tokens = Regex.Split(line, "[^\\w']+");
for (int i = 0; i < tokens.Count()-1; i++)
{
try
{
one_gram.Add(tokens[i], 1);
two_gram.Add(tokens[i] + " " + tokens[i + 1], 1);
}
catch
{
one_gram[tokens[i]]++;
two_gram[tokens[i] + " "+tokens[i + 1]]++;
}
}
}
Can anyone look at my code and tell me where I went wrong? The problem seems to occur at the end of the for loop at the first line, but if I do
for(int i=0;i<tokens.Count()-3;i++)
then the error happens in the second line... but I'm not sure exactly what's causing it.
EDIT: As per suggestions, I tried using the ContainsKey method, but I still get an error near the end of the first line saying that I'm adding a Key that already exists, even though the if statements are supposed to catch that?!
for (int i = 0; i < tokens.Count()-1; i++)
{
if (one_gram.ContainsKey(tokens[i]))
{
one_gram[tokens[i]]++;
}
if (two_gram.ContainsKey(tokens[i] + " " + tokens[i + 1]))
{
two_gram[tokens[i] + " " + tokens[i + 1]]++;
}
one_gram.Add(tokens[i], 1);
two_gram.Add(tokens[i] + " " + tokens[i + 1], 1);
}
You need to use an else (or break):
for (int i = 0; i < tokens.Count() - 1; i++)
{
// Save yourself typing errors by creating variables to hold
// the key values and then you can just use the variable name
var oneGramKey = tokens[i];
var twoGramKey = string.Format("{0} {1}", tokens[i], tokens[i + 1]);
if (one_gram.ContainsKey(oneGramKey))
{
one_gram[oneGramKey]++;
}
else
{
one_gram.Add(oneGramKey, 1);
}
if (two_gram.ContainsKey(twoGramKey))
{
two_gram[twoGramKey]++;
}
else
{
two_gram.Add(twoGramKey, 1);
}
}
I am cycling through the contents of a two-dimensional array containing the result of a Punnett Square calculation for gene crosses. I need to summarize the result so that the user can readily see the unique instances. I can accomplish this by putting the result into a text box, but when I try and use a ListBox to display the data, part of the information is getting lost, namely a translation of the AaBBCc type data to something that directly relates to the traits that the user initially selected.
This is the main block of code for the operation:
foreach (string strCombination in arrUniqueCombinations)
{
int intUniqueCount = 0;
decimal decPercentage;
foreach (string strCrossResult in arrPunnettSQ)
{
if (strCrossResult == strCombination)
{
intUniqueCount++;
}
}
decPercentage = Convert.ToDecimal((intUniqueCount*100)) / Convert.ToDecimal(intPossibleCombinations);
txtReport.AppendText(strCombination + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%."+ Environment.NewLine);
lstCrossResult.Items.Add(DecodeGenome(strCombination) + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%.");
}
For appending the data to the textbox I use this code and it works perfectly:
txtReport.AppendText(DecodeGenome(strCombination) + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%."+ Environment.NewLine);
Giving the result:
Trait 1 Het.,Trait 3 appears 16 times or 25%.
For adding the result to a list box, this works:
lstCrossResult.Items.Add(strCombination + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%.");
Giving the result:
AaBBCc appears 16 times or 25%.
But the contents of strCombination is AaBBCc and I need it translated to "Trait 1 Het.,Trait 3", which I accomplish with this bit of code:
private string DecodeGenome(string strGenome)
{
string strTranslation = "";
int intLength = strGenome.Length;
int intCounter = intLength / 2;
string[] arrPairs = new string[intLength / 2];
//Break out trait pairs and load into array
for (int i = 1; i <= intLength; i++)
{
arrPairs[i / 2] = strGenome.Substring((i-1),2);
i++;
}
foreach (string strPair in arrPairs)
{
char chFirstLetter = strPair[0];
char chSecondLetter = strPair[1];
intCounter = intCounter - 1;
if (Char.IsUpper(chFirstLetter))
{
if (!Char.IsUpper(chSecondLetter))
{
if (intCounter > 0)
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)) + " Het.,");
}
else
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)));
}
}
}
else
{
if (!Char.IsUpper(chSecondLetter))
{
if (intCounter > 0)
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)) + ",");
}
else
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)));
}
}
}
}
return strTranslation;
}
That has no problem displaying in a text box, but when I try and put it as an item into a list box it turns it into null. Instead of:
"Trait 1 Het.,Trait 3 appears 16 times or 25%."
I get:
" appears 16 times or 25%."
I have tried adding the results to an ArrayList, then populating the listbox after everything is processed, but the result is the same.
Any clues as to why the list box is not accepting the translated AaBBCc information would be greatly appreciated.
strTranslation is never set. Everything is pushed to txtReport.AppendText