|
It occurred to me late today that this might make a good Friday Programming Quiz. 
|
|
|
|
|
Maybe even two questions...
1) Given the following:
DayOfWeek startDay = DayOfWeek.Thursday;
DayOfWeek endDay = DayOfWeek.Monday;
DayOfWeek currentDay = DayOfWeek.Wednesday;
Write a C# publicly accessible function that determines whether or not the day falls between startDay and endDay . The function should work for any combination of startDay /endDay values, and should return a boolean value (true if in range, false if not in range).
Answer:
public bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
{
return (currentDay >= startDay || currentDay <= endDay);
}
2) Given the function above, change the function so that the class encapsulating it does not have to be instantiated in order to use the function.
Answer:
public static bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
{
return (currentDay >= startDay || currentDay <= endDay);
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I don't think that works with the "normal" case where startDay < endDay.
It's also not a very general solution (which is no crime).
Given a number (value) line, two points on that line that define a range, and another point to test inclusion.
Normal
...........
1 > 2 < 3
Inverted (endponts not in normal order)
...........
4 < 5 > 6
The test value could be in any of the spots labeled 1 - 6.
We then need to assign true/false to these spots.
Clearly 2 yields true, while 1 and 3 yield false.
The question is about the inverted range;
The OP needs to assign true to 4 and 6, false to 5.
The code I wrote in November was designed to allow assigning true to 5, false to 4 and 6.
I have now modified my helper method (and enum) so it returns one of six values (it was four previously).
The application then needs to provide a method that handles the translation to true/false.
|
|
|
|
|
I think this should cover it then...
public static void DayInRange(DayOfWeek currentDay, DayOfWeek startDay, DayOfWeek endDay)
{
if (startDay == endDay)
{
return true;
}
if (startDay < endDay)
{
return (currentDay >= startDay && currentDay <= endDay);
}
return (currentDay >= startDay || currentDay <= endDay);
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
That hurts my eyes.
(Not that I should talk. )
|
|
|
|
|
On keypress event i am disallow some of the some of the ascii value
like this
if (((e.KeyChar >= 91 && e.KeyChar <= 96) || e.KeyChar == 123 || e.KeyChar == 124 || e.KeyChar == 125 || e.KeyChar == 42 || e.KeyChar == 33 || e.KeyChar == 35 || e.KeyChar == 38 || e.KeyChar == 40 || e.KeyChar == 39 || e.KeyChar == 34 || e.KeyChar == 41 || e.KeyChar == 44 || e.KeyChar == 64 || e.KeyChar == 61 || e.KeyChar == 59 || e.KeyChar == 58 || e.KeyChar == 63 || e.KeyChar == 60 || e.KeyChar == 62) != false)<br />
{<br />
e.Handled = true;<br />
<br />
}
Now hat i want when user press space bar (Ascii value 32) then it will print '~' (Ascii value 126) in place of Ascii 32
if user enter
ABCD(SPACE)FGT THEN IT SHOULD PRINT
ABCD~FGT
|
|
|
|
|
Hi,
You may use the following code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Space Then
e.SuppressKeyPress = True
Dim i As Integer
i = Me.TextBox1.SelectionStart
Me.TextBox1.Text = Me.TextBox1.Text + "~"
Me.TextBox1.SelectionStart = i + 1
End If
End Sub
Hope this helps.
Vinay
ComponentOne LLC.
www.componentone.com
|
|
|
|
|
Will this work?
if ( e.KeyChar == ' ' ) e.KeyChar = '~' ;
And the code you posted looks very difficult to maintain.
|
|
|
|
|
Well no offense...but the code above is VB
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
|
|
|
|
|
No it isn't. Why do you say so? Or do you mean Vinay's response?
|
|
|
|
|
second option:
PIEBALDconsult wrote: Vinay's response
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
|
|
|
|
|
Hi guys. I need some help with the below.
foreach (CsvImp imp in res)
{
string prov = SearchPcode(imp.CallComments);
txtboxDisplay.Text += imp.CompanyCode + "\t" + imp.CallComments.ToString()
+ "\t" + prov + Environment.NewLine;
engine.WriteFile(@"C:\Sasol_" + DateTime.Today.ToString("ddMMyyyyhhmm") + ".csv", res);
}
I am reading in a file with FileHelpers and writing some of the fields to a text box. I am also using one of the imported fields and matching that to derive an extra field which I am able to write to the textbox as well. Now, my problem is when I use the FileHelper engine to write to file I can only write the data in the res . I need to write all that data plus the extra field prov to file.
Any idea how I do that? Or any other suggestions??
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Kwagga wrote: file with FileHelpers
I don't know what that is.
Kwagga wrote: I am also using one of the imported fields and matching that to derive an extra field
I don't know what this means.
Kwagga wrote: I need to write all that data plus the extra field prov to file.
Maybe I'm misunderstanding what you're asking, but shouldn't this work?
engine.WriteFile(@"C:\Sasol_" + DateTime.Today.ToString("ddMMyyyyhhmm") + ".csv", res + Environment.NewLine + prov);
|
|
|
|
|
Thanks for trying but that doesn't help. www.filehelpers.com
Thanks for trying though.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Kwagga wrote: Any idea how I do that?
I gotta lotsa ideas! No good ones though.
But seriously, I think we need a few more details first. Here are some assumptions I've made, let me know if I'm right or wrong.
engine is of type FileHelper
res is an array or collection of CsvImp objects
CsvImp is a class or struct containing data
What is FileHelper? A third party utility or a class that you or a colleague has written. If it's your own in-house class, you could write an overloaded function of WriteFile() that would accept a
string instead of a CsvImp object. If it's a commercial product, that will make it a little tougher.
BDF
A learned fool is more a fool than an ignorant fool.
-- Moliere
|
|
|
|
|
Big Daddy Farang wrote: engine is of type FileHelper
res is an array or collection of CsvImp objects
CsvImp is a class or struct containing data
All Correct.
Check the Site www.FileHelpers.com[^]
I have posted the question there as well but no help as the creaters of the tool don't seem to be checking the blogg anymore. So, now I'm slightly stuck.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
That's cool, so you probably have the source code available. Writing an overload of WriteFile() may be an option. I say "may be" because depending on what the ultimate disposition of your code is, you might not want to do that due to license issues, etc.
Another option would be to bypass the FileHelper engine and just write the prov string to the .csv file using a StreamWriter or similar. Of course that's quite messy if you're needing to do both at once:
engine.WriteFile("...", res);
// close the file
// re-open the file
streamWriter.WriteLine(prov);
// close the file
// back to top of foreach...
Is it possible to modify CsvImp to contain another field for prov? Then when the res collection gets written, the prov fields are included. Now we're deep into the "any other suggestions" territory.
BDF
A learned fool is more a fool than an ignorant fool.
-- Moliere
|
|
|
|
|
I have decided to just write the prov field out with StreamWriter. I now have two files which I guess I can manualy merge but hey, what else to do.
Thanks for the help and advice mate. I'll wait for the guys at FileHelpers to see if they have a solution to the problem I have at the moment.
Off to now. A bit late this side of the world.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
I think I have found a solution to my problem. I am writing the information in the textbox that I'm using to display the info out to a .txt file and this works 100% now.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Hi I am very new to .NET and C#..
Presently I am reading Jeffrey Richter's "CLR via C#" and here he always mentions (in Type and Member Basics Chapter) that the CLR can call a virtual method non-virtually..
How does CLR do that? What does this mean?
Could anybody explain or give some helpful links??
Thanks!!
|
|
|
|
|
IL has two ways of calling a method. Call and CallVirt. CallVirt basically does some extra checks and stuff to make sure the object the method is being called on is not null, and it also then works out exactly which method to call by looking at the object hierarchy and calling the appropriate virtual method for the actual object passed.
If you write the IL by hand, there is no reason why you couldn't call virtual methods with the Call command, what would be missed out though is the polymorphic part of the call, so exactly the method you specify on the type you specify will be called, rather than the CLR looking at the object hierarchy and finding the appropriate virtual method.
Lets see. Say you had two Classes.
BaseClass and SuperClass (which inherits from BaseClass)
BaseClass defines a method called "TestMethod", SuperClass overrides that method and provides it's own implementation.
In C#, if you create a superclass object, cast it to a BaseClass and then call TestMethod, because it's called with CallVirt, the CLR will locate and call the TestMethod on the SuperClass, and call that, despite the fact that the objects was cast to a BaseClass. This is because even though it's been cast to a BaseClass, the object is still a SuperClass object.
Now, if you edited the IL and changed the CallVirt to a Call command, (your now calling a virtual method non-virtually) that part of the CLR processing wouldn't happen, and it would call the BaseClass method, because that's the Type that has been provided in the call.
If you only ever write in C#, it's nothing really to worry about. C# compiles all instance method calls to a CallVirt command, even if they are not virtual. (they are therefore non-virtual methods being called virtually, which doesn't really matter at all).
This is only ever worth worrying about if you have code that references your code that is not written in C#, it may call your non virtual methods with Call, which is fine to begin with, but it means if you then change your C# non-virtual method to a virtual one, the calling code will be calling a virtual method with the Call command, which as shown above, gives unexpected results.
(Try defining the classes I described, compile it, and use ILDasm to output the IL. You can manually change the callvirt to call and use ILAsm to reassemble the IL. The behaviour of the program changes as I described)
Simon
|
|
|
|
|
Good post Simon. Do you have any good articles where I can learn something about IL ?
|
|
|
|
|
Good idea, maybe I should write up some of the bits about IL I've picked up.
Kenny Kerr has a good intro to MSIL series of artciles[^]
Other than that, I would recommend 'CLR via C#' (Jeffery Richter). Although it doesn't really go into IL that much, he does often describe how the CLR handles things. I picked up this stuff about Call and CallVirt from this book, and then trying things out with ILDasm/ILAsm myself.
Simon
|
|
|
|
|
Indeed a very good post.. You solved all my problems!!
|
|
|
|
|
I don't know how the CLR can choose to call a virtual method non-virtually, but the compiler can do certain optimizations, if it knows that the object on which the method is called is the most derived object. For eg.
interface IA
{
void Method();
}
struct S : IA
{
public void Method() { Console.WriteLine("S"); }
}
S s = new S(); s.Method();
The compiler figures out that S can't be extended any further and therefore compiles s.Method to a call instruction, rather than a callvirt.
|
|
|
|