|
Nice example
|
|
|
|
|
Hi,
they are two completely different things:
Array:
An array is a set of homogeneous variables (I.E. they share the same type). it is declared in this way:
YourType[] yourArray = new YourType[noitems]
where noitems is the number of variables in the array.
To access a single variable you need to use an index. This index starts from 0, not from 1, so the array indicies will go from 0 to noitems-1 !!
YourType yourArrayVariable = yourArray[yourIndex];
Please keep in mind that if yourIndex points to a variable which does not exists in the array, a IndexOutOfRangeException will be thrown.
Arrays can also have multiple dimensions:
YourType[,] yourTable = new YourType[6,2];
it is a declaration of a table with 6 rows and 2 columns. You can imagine a single-dimension array as a single row, a two-dimensional array as a table and a three dimensional array as a cube.
There are also jagged arrays. While if you decalre your multi-dimensional array just like the code above you'll get a rectuagular table, with jagged arrays (or array of arrays), you can define your personal dimension for each element.
YourType[][] jaggedArray = new YourType[3][];
jaggedArray[0] = new YourType[3];
jaggedArray[0][2]
Loops are a completely different thing.
A loop enables you to do something repeatedly while a condition is true.
While loops will do the instructions inside their body while a condition is true. The condition is evaluated before the loop starts and, if it is false, the loop does not start.
<pre lang="c#">
while (condition){
}
There is another version of while which is the do...while loop. The only difference is that the condition is evaluated after the loop is executed. I.E. it is evaluated at the end, so the loop is executed at least once before evaluating the condition.
do{
}while(condition);
There is the for loop which is controlled by a variable which is usually an integer or, generally, a number:
for (declaration;condition;instruction){
}
for(int i = 0;i<5;i++){
}
The for loop has 3 different parts: the declaration of the control variable (actually, whatever instruction which has to be called before executing the first iteration of the loop), the condition (which is the condition to execute the loop as the other loops) and then the instruction, which is an instruction which is called at the end of each iteration. Usually, the for loop is used to increment a variable to access array elements.
string[] lines = new string[10];
for(int i = 0;i<10;i++){
Console.WriteLine(lines[i]);
}
Please keep in mind that the variable declared inside the decalration block of the for is valid for the for loop only and it is not available outside it.
If you want to make it available do this:
string[] lines = new string[10];
int i;
for(i = 0;i<10;i++){
Console.WriteLine(lines[i]);
}
And notice that the maximum value for i is 9 and not 10 as 10<10 = false.
The last loop is the best to deal with arrays. This is the foreach loop. This loop enables you to iterate over all the items in an array and store the current value in a variable.
This code will print all the numbers inside the array integers :
int[] integers = new int[20];
foreach(int i in integers){
Console.WriteLine(i);
}
Unfortunately, the foreach loop has not an index variable and, if you want it, you'll have to declare it explicitely.
Hope this helps.
Lusvardi Gianmarco
|
|
|
|
|
Could someone please explain to me why the callback method of an EventHandler<t> has to be static? According to the .NET documentation the delegate looks like this:
[SerializableAttribute]
public delegate void EventHandler<TEventArgs>(
Object sender,
TEventArgs e
)
No hint of static there. So, my second question would be;
If I want to declare a delegate which should take a static callback method, how to do that?
|
|
|
|
|
What makes you think that the method needs to be static ?
public class Foo
{
public event EventHandler<EventArgs> MyEvent;
public void Test()
{
var handler = MyEvent;
if (handler != null) handler(this, EventArgs.Empty);
}
}
public class Bar
{
private readonly Foo _foo;
public Bar()
{
_foo = new Foo();
_foo.MyEvent += MyEventHandler;
}
public void Test()
{
_foo.Test();
}
private void MyEventHandler(object sender, EventArgs e)
{
Console.WriteLine("Bacon.");
}
}
|
|
|
|
|
Well, Im not sure...
I tried to use the EventHandler<t> earlier, got error, and was under the impression that the non-static nature of the callback-method was the cause. So I changed the code declaring an explicit delegate and it worked. But now when I changed back it actually still works. So Im a little confused right now why it didnt work earlier.
Anyway. I can clearly see it works with a non-static method now. Just forget I asked
Thanks anyway.
|
|
|
|
|
Some statical analysis may flag it as "requires static" if you do not reference any non-static members of the class.
|
|
|
|
|
I have been trying to find a method to providing path(location) of deleted files from storage drives (Hard Disk, Removable drives etc.)
Does anyone know any classes/methods for finding deleted files, retrieving them or any tutorials or helps on the matter.
|
|
|
|
|
|
string strPhysicalFolder = Server.MapPath("..\\");
string strFileFullPath = strPhysicalFolder + StrFilename;
if (IO.File.Exists(strFileFullPath)) {
IO.File.Delete(strFileFullPath);
}
|
|
|
|
|
When providing an answer, it generally helps to read the requirements in the question. What you have proposed is how to delete a file, but what the OP asked was how to find deleted files.
|
|
|
|
|
Basically, I'm trying to find a way to encrypt the output of my software in such a way that I am only able to decrypt the output using php. However, a secure way to do it where the c# source code couldn't be decompiled to see how the output was encrypted.
C# Secure Encrypted Output -> Server -> PHP -> Plain-Text
I know that I cannot stop people from decompiling or cracking my C# software.. but maybe it's possible to secure some output with something like this? Basically, if someone took the "C# Secure Encrypted Output", they wouldn't be able to retrieve the plain-text without my php script.
Is this possible and how would I go about doing this?
|
|
|
|
|
No security expert, but I believe what you basically need is a public/private key pair or something similar.
you encrypt the output with the public key. Only the private key can unlock it. (authorization).
For info, encrypting with the private key, anyone can decrypt it with the public key (authentication).
This is how banks usually do it and how digital signatures work.
I've never done this myself, but at least it gives you something to go on, I hope.
|
|
|
|
|
Let me load up fiddler and take a look at the data you're sending..
Things that the user isn't supposed to know, should not reach the users' PC.
|
|
|
|
|
Have a look here[^]
Scroll down for the answers.
|
|
|
|
|
if (File.Exists(paraFile))
File.Delete(paraFile);
using (FileStream fs = new FileStream(paraFile, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(markLineOfRobotReason);
sw.WriteLine("");
foreach (string reason in robotRegisterReasons)
sw.WriteLine(reason);
sw.WriteLine("");
sw.WriteLine(markLineOfHumanReason);
sw.WriteLine("");
foreach (string reason in humanRegisterReasons)
sw.WriteLine(reason);
test++;
sw.WriteLine("");
sw.Write("第" + test + "次写末尾参数");
sw.WriteLine(markLineOfExecRate);
sw.WriteLine("");
sw.WriteLine(comboBox1.SelectedItem.ToString());
sw.WriteLine("");
sw.WriteLine(markLineOfIfShown);
sw.WriteLine("");
sw.WriteLine(checkBox1.Checked);
}
}
result is like this:
Quote: 第3次写末尾参数------------运行频率(小时):------------
1
------------是否显示作业窗口:------------
True
第6次写末尾参数------------运行频率(小时):------------
1
------------是否显示作业窗口:------------
True
第3次写末尾参数------------运行频率(小时):------------
1
------------是否显示作业窗口:------------
True
why?I think it should only 1 times...
|
|
|
|
|
The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem.
Based on current info, perahps you're wondering why
foreach (string reason in robotRegisterReasons)
sw.WriteLine(reason);
prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.
|
|
|
|
|
You got it,you are right,I made a mistake...Thank you after all.
|
|
|
|
|
Glad if it helped
|
|
|
|
|
validation sending me loopy this is the code but it returns any string i need to set the string value (Length) to >= to 3 and only monday to friday am I reaching ??
specDay = Microsoft.VisualBasic.Interaction.InputBox("What Day Would You Like To Check:");
string sPattern = "^(Mon|(T(ues|hurs))|Fri)(day|\\.)?$|Wed(\\.|nesday)?$|T((ue?)|(hu?r?))\\.?$";
int result = Array.BinarySearch(dayNameArray, specDay, StringComparer.OrdinalIgnoreCase);
if (System.Text.RegularExpressions.Regex.IsMatch(specDay, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
for (int dayCounter = 0; dayCounter < dayNameArray.GetLength(0); dayCounter++)
{
if (dayNameArray[dayCounter] == specDay)
{
Days = dayCounter;
}
}
}
else
{
MessageBox.Show("Not A valid Day Of Week");
specDay = Microsoft.VisualBasic.Interaction.InputBox("What Day Would You Like To Check:");
}
modified 22-Jan-15 20:33pm.
|
|
|
|
|
What are you trying to do? There's gotta be a simpler way.
|
|
|
|
|
input products made on each day then get the day and week produced only out of the array
It needs to be an input box as per assignment I just am doing my head in validating as its a project the specs for it i tried the combo box :P i cant find anything in the source material to guide as to what my instructors want...the advice they give is that the string be at least 3 letters and that when i run it the days inputed into the box must be monday tuesday wednedsay thursday or friday and no numbers ... if there is an easier way im up to learning that too
I can paste the whole code
private void btnInputBox_Click(object sender, EventArgs e)
{
int[,] toyArray = new int[4, 5]; var totalToys = 0;
string[] dayNameArray = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
string specDay;
int specWeek;
string val = "";
int specDayTotal;
string Output = "";
string Output1 = "Retrieve Products produced On A specific day";
string Output2 = " Products produced On That day are:";
string Output3 = "Sum of All Products Made = ";
int num = 0;
int Days=0;
string dOW = "";
for (int row = 0; row < toyArray.GetLength(0); row++)
{
for (int Col = 0; Col < toyArray.GetLength(1); Col++)
{
val = Microsoft.VisualBasic.Interaction.InputBox("How Many Toys Made for Week:" + (row + 1) + " " + dayNameArray[Col] + ": ");
try
{
while (!(int.TryParse(val, out num)))
{
MessageBox.Show("Not a valid number, try again.");
val = Microsoft.VisualBasic.Interaction.InputBox("How Many Toys Made for Week:" + (row + 1) + " " + dayNameArray[Col] + ": ");
}
}
catch (Exception)
{
MessageBox.Show("Value entered is not in a valid format");
}
toyArray[row, Col] = num;
}
}
for (int row = 0; row < toyArray.GetLength(0); row++)
{
for (int Col = 0; Col < toyArray.GetLength(1); Col++)
{
Output += " " + toyArray[row, Col] + " ";
totalToys += toyArray[row, Col];
}
Output += " " + "\r\n" + "";
}
specDay = Microsoft.VisualBasic.Interaction.InputBox("What Day Would You Like To Check:");
string sPattern = "^(Mon|(T(ues|hurs))|Fri)(day|\\.)?$|Wed(\\.|nesday)?$|T((ue?)|(hu?r?))\\.?$";
int result = Array.BinarySearch(dayNameArray, specDay, StringComparer.OrdinalIgnoreCase);
if (System.Text.RegularExpressions.Regex.IsMatch(specDay, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
for (int dayCounter = 0; dayCounter < dayNameArray.GetLength(0); dayCounter++)
{
if (dayNameArray[dayCounter] == specDay)
{
Days = dayCounter;
}
}
}
else
{
MessageBox.Show("Not A valid Day Of Week");
specDay = Microsoft.VisualBasic.Interaction.InputBox("What Day Would You Like To Check:");
}
specWeek = Int32.Parse(Microsoft.VisualBasic.Interaction.InputBox("What week Would You Like To Check:"));
try
{
while (!(specWeek <= 4)&& !(specWeek == num))
{
MessageBox.Show("Not a valid number, try again.");
specWeek = Int32.Parse(Microsoft.VisualBasic.Interaction.InputBox("What week Would You Like To Check:"));
}
}
catch (Exception)
{
MessageBox.Show("Must Be 1 to 4");
}
int Week = specWeek-1;
specDayTotal = toyArray[Week, Days];
txtOutput.Text += " " + "Monday Tuesday Wednesday Thursday Friday " + "\r\n" + Output + " " + Output1 + "\r\n" + " " + specDay + " Week " + (Week+1) + "\r\n" + Output2 + " = " + specDayTotal + "\r\n" + Output3 + totalToys;
}
}
|
|
|
|
|
Change your inputbox to a popup dialog with either a combobox of days or a collection of radio buttons. Validation eliminated, user input reduced, possibility of incorrect entries eliminated.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
It needs to be an input box as per assignment I just am doing my head in validating as its a project the specs for it i tried the combo box :P i cant find anything in the source material to guide as to what my instructors want...the advice they give is that the string be at least 3 letters and that when i run it the days inputed into the box must be monday tuesday wednedsay thursday or friday and no numbers ... if there is an easier way im up to learning that too
|
|
|
|
|
Member 11354386 wrote: must be monday tuesday wednedsay thursday or friday
I would probably just use a Hashset with a case-insensitive comparer, add the valid values, and then test for Contains.
Another option would be to use an enumeration and try Parsing the value.
See also: Dictionary<string,T>.BestMatch[^]
And remember that your teacher will likely know what you are capable of and will know if you turn in code you didn't write yourself, so take any ideas you may encounter and write your own solution.
|
|
|
|
|
Could You perhaps walk me through that enum parse I did try doing a Match with regex but so confused now this is the code I got and it works unless you abbreviate days mon tue etc this is the string
string sPattern = "^(Mon|(T(ues|hurs))|Fri)(day|\\.)?$|Wed(\\.|nesday)?$|T((ue?)|(hu?r?))\\.?$";
how can I modify the string to just Monday Tuesday etc?
|
|
|
|