|
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?
|
|
|
|
|
I am in a course in C# and have gotten a task to refactor my earlier task so it uses interfaces and abstract classes instead. In this task i need to make the interface with specific properties and methods, one of these properties should only have a getter but not a setter, this is a string object to hold the ID of the animal added.
My question is, how do i set this property? I can't give it a value in the constructor, and i cant work with it in the interface, and i cant do something with it in the abstract class since it's not static.
Cake
|
|
|
|
|
Under normal conditions, you would use either a private backing field:
private string _MyProperty = "hello";
public string MyProperty
{
get { return _MyProperty; }
} Or a private setter:
public string MyProperty { get; private set; } In either case you can set the value in the class constructor, or any other method of the class - but not from the outside world.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|