Outlook.Items Restrict() weird behavior - c#

I just want to filter my Mails with the Restrict-Method like so:
restriction += "[ReceivedTime] < '" + ((DateTime)time).ToString("yyyy-MM-dd HH:mm") + "'";
var count = oFolder.Items.Restrict(restriction).Count;//Cast<object>().ToList();
for (int i = 0; i < count; i++)
{
var crntReceivedTime = ((OutLook.MailItem)oFolder.Items.Restrict(restriction).Cast<object>().ToList()[i]).ReceivedTime;
if (crntReceivedTime > time)
{
string t = "";
}
}
Theoretically the line string t = ""; should never be called, because I determined the Items to never have entries which ReceivedTime's value is bigger than time.
The problem is the line gets called, what means the restricted Items Collection contains entries which its shouldn't contain.
Did I do something wrong or is the Restrict()-method just failing?

Firstly, you are using multiple dot notation. You are calling Restrict (which is expensive even if it is called once) on each step of the loop. Call it once, cache the returned (restricted) Items collection, then loop over the items in that collection.
Secondly, what is the full restriction? You are using += to add an extra restriction on ReceivedTime. What is the actual value of the restriction variable?
Edit: I had no problem with the following script executed from OutlookSpy (I am its author - click Script button, paste the script, click Run):
restriction = " [ReceivedTime] < '2011-06-11 00:00' "
set Folder = Application.ActiveExplorer.CurrentFolder
set restrItems = Folder.Items.Restrict(restriction)
for each item in restrItems
if TypeName(item) = "MailItem" Then
Debug.Print item.ReceivedTime & " - " & item.Subject
End If
next

Related

How to make this code more functional or 'prettier'

I've been working on a project where I need on a button press that this line gets executed.
if (listView1.SelectedItems[0].SubItems[3].Text == "0") //Checks to see Value
{
listView1.SelectedItems[0].SubItems[3].Text = "1";// If Value is Greater, Increase and Change ListView
questionNumberLabel.Text = listView1.SelectedItems[0].SubItems[3].Text;// Increase and Change Label
}
Now I have this repeated about 10 times with each value increasing by one. But I know that this is ugly, and dysfunctional. As well as conflates the file size. I've tried a few things. Primarily this method.
if (listView1.SelectedItems[0].SubItems[3].Text == "0")
{
for (var i = 1; i < 100;)
{
if (!Int32.TryParse(listView1.SelectedItems[0].SubItems[3].Text, out i))
{
i = 0;
}
i++;
listView1.SelectedItems[0].SubItems[3].Text = i.ToString();
Console.WriteLine(i);
}
}
But instead of just adding one, it does the 100 instances and ends. The reason this is becoming a pain in the *** is because the
listView1.SelectedItems[0].SubItems[3].Text
is just that - it's a string, not an int. That's why I parsed it and tried to run it like that. But it still isn't having the out come I want.
I've also tried this
string listViewItemToChange = listView1.SelectedItems[0].SubItems[3].Text;
Then parsing the string, to make it prettier. It worked like it did before, but still hasn't given me the outcome I want. Which to reiterate is, I'm wanting the String taken from the list view to be changed into an int, used in the for loop, add 1, then restring it and output it on my listView.
Please help :(
You say you want the text from a listview subitem converted to an int which is then used in a loop
so - first your creating your loop variable, i, then in your loop you're assigning to it potentially 3 different values 2 of which are negated by the, i++. None of it makes sense and you shouldn't be manipulating your loop variable like that (unless understand what you're doing).
if you move statements around a little..
int itemsToCheck = 10; // "Now I have this repeated about 10 times "
for (var item = 0; item < itemsToCheck; item++)
{
int i;
if (!Int32.TryParse(listView1.SelectedItems[item].SubItems[3].Text, out i))
{
i = 0;
}
i++;
listView1.SelectedItems[item].SubItems[3].Text = i.ToString();
Console.WriteLine(i);
}
Something along those lines is what you're looking for. I haven't changed what your code does with i, just added a loop count itemsToCheck and used a different loop variable so your loop variable and parsed value are not one in the same which will likely be buggy.
Maybe this give you an idea. You can start using this syntax from C# 7.0
var s = listView1.SelectedItems[0].SubItems[3].Text;
var isNumeric = int.TryParse(s, out int n);
if(isNumeric is true && n > 0){
questionNumberLabel.Text = s;
}
to shortcut more
var s = listView1.SelectedItems[0].SubItems[3].Text;
if(int.TryParse(s, out int n) && n > 0){
questionNumberLabel.Text = s;
}

Second for loop isn't running inside my update

This if statement within the update() have 2 for-loop, but it only runs the first one after the if condition is activated, and I don't know why.
I'm building a code for path optimizing in unity. Currently I have to find out the path that came across the nodes/points/positions with a certain positions array that the index is the order the path should follow. Some path between 2 nodes are repeated , ex: A to B and B to A is consider the same path and shall thicken the width of line AB eventually rendered. So I tried to sort out the position array into 2 different array for comparing if any of the pair of nodes(or we can say line) is repeated. And I encountered a problem in if statement within the update().
The first should sort out the original array for later comparison. The second one is just for testing if the first one do their job. No comparing yet. However after hitting play and satisfy the if statement I can see all the Debug.log in the first one, everything is normal, the sorting is normal, while the second one just doesn't print anything at all.
I tried comment out the first one, and the second one will run.
I tried to put second one outside the if statement, after it, and without commenting the first one, the second one won't run.
I tried to put the second one before the first one, in the if statement, the second one will run and the first one won't.
So I think this might be some kind of syntax error or am I using the if statement wrong? Please help.
if (l > 0)//activate when we choose any preset processes
{
for (int n = 0; n <= positions.Length; n++)//this loop will sort all the pos1 and pos 2 into array for current frame
{
curPos_1 = positions[n];//current position of node 1
curPos_2 = positions[n + 1];
Debug.Log("CURPOS_1 of line number " + n + " is " + curPos_1);
Debug.Log("CURPOS_2 of line number " + n + " is " + curPos_2);
flag[n] = 0;
Pos_1[n] = curPos_1;
Pos_2[n] = curPos_2;
Debug.Log("POS_1 array of line number " + n + " is " + Pos_1[n]);
Debug.Log("POS_2 array of line number " + n + " is " + Pos_2[n]);
}
for (int o = 0; o <= positions.Length; o++)
{
Debug.Log("flag of number " + o + " is " + flag[o]);
}
}
As described, all for loop should print something. Not just one of it.
Have you checked your Unity Console Window ?
In your first loop you get the next item but its condition will fail at the end, i.e. off by one.
Correct code should be something like this:
var floats = new float[100];
for (var i = 0; i < floats.Length - 1; i++)
{
var f1 = floats[i];
var f2 = floats[i + 1];
}
Now, Unity, has a behavior of ON ERROR RESUME NEXT, so it's highly probable that an error has occured but you haven't seen it (did you turn off the red icon for toggling errors in console ?).
Also, for some conditions only you know about (you didn't post the whole context), it could work once after you've changed some state of your program.

I have multiple text boxes (tbVarName1, tbVarName2, ... tbVarName[n]) and I want to increment them in a for loop to assign the text to something

I have a multiple text boxes that I want to assign their string content to a variable however I'm not sure how to increment the text boxes. They are named, tbVarName1, tbVarname2, et cetera. Below is the for loop I have, right now I just have tbVarName1 hard coded in.
I have researched some of what other people have done and have only found tips for doing it in VB.
for(seriesIndex = 1; seriesIndex <= 4; seriesIndex++)
{
dataChart.Series["Variable " + seriesIndex].LegendText = tbVarName1.Text
}
At the end of this I would like the the legends to be updated to what's in the text boxes
Another way to do it is using the Controls collection from the Form (assuming that all TextBoxes are direct children of the form)
var ctrl = this.Controls.OfType<TextBox>();
for(seriesIndex = 1; seriesIndex <= 4; seriesIndex++)
{
TextBox t = ctrl.FirstOrDefault(c => c.Name == "tbVarName" + i);
if(t != null) dataChart.Series["Variable " + seriesIndex].LegendText = t.Text;
}
This will not require an array but you could end with a bigger loop if you have many controls of type textbox and it is not worth the effort if the TextBox are children of different containers (panels, groupboxes)
There are various ways to do this, one way is to add the controls into an array, for example:
var controls = new [] { tbVarName1, tbVarName2, tbVarName3 };
And now you can access them by index:
for(seriesIndex = 1; seriesIndex <= 4; seriesIndex++)
{
dataChart.Series["Variable " + seriesIndex].LegendText = controls[seriesIndex - 1].Text;
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// Like this
// Note: arrays start at zero
}

Save Richtextbox text in TabControl to a file

I have a code that will write me all the text from all RichTextBoxes that are in the TabPage. The problem is that it does not save any text.
string projectFile = projectPathFolder + #"\" + projectName + #"\" + projectName + ".project";
for (int i = 0; i < tabControl1.RowCount; i++)
{
RichTextBox richText = tabControl1.Controls[i] as RichTextBox;
using (var stream = new StreamWriter(File.Create(projectFile)))
{
stream.Write(scintilla.Text);
}
File.WriteAllText(projectFile, "// " + tabControl1.TabPages[i].Text + "\n\n" + richText.Text, Encoding.UTF8);
}
RowCount is the wrong property to use as it returns the number of rows, not tabs. E.g. you could have 6 tabs but RowCount may still only be 1 if the tabs are narrow and the control wide.
Firstly, are you trying to enumerate all pages in the TabControl? If that's the case, use TabCount in your for loop, not RowCount
for (int i = 0; i < tabControl1.TabCount; i++)
If you only want to use the current tab, then get rid of the loop and just use tabControl1.SelectedTab.
Secondly, the line trying to get the RichTextBox is never going to work. A TabControl can only host TabPage instances, and trying to access RichTextBox instances via the Controls property is simply not going to work. You will need to get the TabPage first and then either enumerate the controls on it to find the RichTextBox, or if there is only a single control per page then you could directly access it.
RichTextBox richText = tabControl1.TabPages[i].Controls[0] as RichTextBox;
I'm not at all sure why you have that using block to write content to projectFile - it's just going to get overwritten by the call to File.WriteAllText.
As an aside, joining paths by hand is bad practice - try using Path.Combine, e.g. Path.Combine(projectPathFolder, projectName, projectName + ".project");
Edit: One other point. You're using as RichTextBox, which generally means you accept the cast may not be valid and return null. However, you're not doing an explicit null check on that result. If you expect failure, then wrap the File.WriteAllText statement in a null check. If you don't expect failure, then make the cast explicit and let it crash at the point of the cast - better to fail early than later.
RichTextBox richText = (RichTextBox)tabControl1.TabPages[i].Controls[0];
Hope this helps.

If-else statement with radio buttons and a for loop

I've gotten an issue that keeps happening and I'm unsure what the problem is. I need to create a program in Microsoft Visual studio where radioButton1 is for finding the sum of consecutive numbers and radioButton2 is for finding the factorial of a number.
There will be three buttons, one for FOR loop, one for WHILE loop, and one for DO-WHILE loop. I'm working on the FOR button.
What I'm trying to do is by making a selection, then pressing one of the buttons, it'll find the answer using the loop clicked in a Message box.
This is what I've gotten so far:
private void button1_Click(object sender, System.EventArgs e)
{
if ((radioButton1.Checked == true) && (radioButton2.Checked == false))
{
int sum = 0;
int number = int.Parse(numericUpDown1.Value.ToString());
for (int i = 1; i <= number; i++)
{
sum = sum + i;
MessageBox.Show("The sum of the consecutive numbers leading up to " + number + " is " + sum + ".");
}
MessageBox.Show("The sum of the consecutive numbers leading up to " + number + " is " + sum + ".");
}
else if ((radioButton2.Checked == true) && (radioButton1.Checked == false))
{
int product = 1;
int number = int.Parse(numericUpDown1.Value.ToString());
for (int i = 1; i <= number; i++)
{
product *= i;
MessageBox.Show("The factorial of the numbers leading up to " + number + " is " + product + ".");
}
MessageBox.Show("The factorial of the numbers leading up to " + number + " is " + product + ".");
}
else
{
MessageBox.Show("Invalid");
}
}
I keep receiving this message:
"'Lab5'does not contain a definition for 'radioButton2.CheckedChanged' and no extension method 'radioButton2.CheckChanged' accepting a first argument of type 'Lab5' could be found (are you missing a using directive or an assembly reference?)."
And I honestly have no idea what that means.
Any help would be greatly appreciated.
I want to keep it in an if-else statement simply because I don't want the message box for radioButton1 to pop up when radioButton2 is selected.
You have probably inadvertently added a handler to that radio button (e.g. by double clicking it in the designer) and then removed it. When the solution is building, it is looking for that function but can't find it.
Check Lab5.Designer.cs for the radioButton2 code. It will look something like this:
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(102, 162);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 1;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); /* THis is the offending line! */
That last line is trying to add an event handler referencing a non-existant method. You can either remove it here.
Also, when the program builds, you should be able to double-click on the compile error and the IDE will take you to the source of the problem.
Looks like a homework. Either way, you need to implement a handler for your radio buttons for the event CheckChanged. Also, if you put those radio buttons in a group, either one can be checked and in the checkchanged event you should populate some functionMode or something. Example, when RadioButton1 is checked, you should store a variable that'll contain which formula to apply, on the other one checked you should change that. And in the button submit event, use that variable to see the current mode and apply that formula.

Categories

Resources