|
Hi Guys,
Hope someone can help with this little problem I have because its driving me mad.
I need to calculate the period in which a date falls
so lets say a companies financial years starts on 1st Jan then
Jan = P1, Feb = P2 etc etc
That makes finding the period quite easy, i just take the month of the transaction.
The problem I'm having is when the FY starts on say 1st April 2009 then
Apr = P1, May = P2 etc etc
So .
15/06/2009 returns P3/2009
01/08/2009 returns P5/2009
but
15/01/2009 returns P10/2008
So far I'm using this
'Holds the date which financial year starts
Dim FYstart As Date = = CDate("01/04/2000")
'Move FYStart to current year
FYstart = DateValue(FYstart.Day & "/" & FYstart.Month & "/" & TranDate.Year)
'Months difference between transaction date and FYstart
Dim MonthDiff As Integer = -DateDiff(DateInterval.Month, TranDate, FYstart)
'Years different between the 2 dates
Dim YearDiff As Integer = Math.Truncate(MonthDiff / 12)
'Period number
Dim p As Integer
If MonthDiff <= 0 Then
p = 12 + (MonthDiff)
Else
p = MonthDiff
End If
Period = p
Year = TranDate.Year + YearAdj
Anyone have any pointers because everytime I think I've cracked it, I try a different FY Start / Tran Date combination and it gives me wrong answer
thanks in advance,
Daniel
|
|
|
|
|
Oops - you posted in the C# forum!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
drodgers wrote: but
15/01/2009 returns P10/2008
This is right. January is the tenth month of the 2008/09 tax year.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Would something this simple not work....
DateTime startDate = new DateTime(2009, 04, 01);
DateTime transactionDate = new DateTime(2009, 4, 01);
int monthDiff = transactionDate.Month - startDate.Month + 1;
if (monthDiff <= 0)
monthDiff = 12 + monthDiff;
MessageBox.Show(monthDiff.ToString());
|
|
|
|
|
Hello, I'm creating a program that will parse a for loop statement. I'm already done with some of the things that a parser does. Now my problem is about the pattern i created to check if the user's for loop syntax is correct.
First i ask the user to enter an example of a for loop statement syntax.
ex. for(int x=0; x<5; x++) Console.WriteLine("Hello World");
After that I'll just check if the example follows the syntax of a for loop statement.
To check if the syntax is correct i created a for loop statement pattern to compare it with the user's for loop statement.
Here is the pattern:
public static string forLoopPattern =
@"([for])" +
@"([(])" +
@"([int]\ \\)" +
@"([a-z])" +
@"([=])" +
@"([0-9])" +
@"([;])" +
@"([a-z])" +
@"([>,<,=,<=,>=,=<,=>])" +
@"([0-9])" +
@"([;])" +
@"([a-z])" +
@"([++,--])" +
@"([)])";
Is that pattern correct? If not what should i change or do to make it correct? Also, i don't know yet if that pattern really works or not. How will i be able to check if that pattern works?
Please Help..thanks..
|
|
|
|
|
You really should test it yourself and see if it works.
Some points thou...
- for loops don't rely on using an int data type
- someone may want two spaces between type and instance name
- instance names can be alot more than just a-z
- instance name can be any number (within limits of data type) and potentially negative too
- you can also apply more than just increment and decrements to the instance
etc...
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Further to what musefan said, broadly speaking, each of the three parts of the for loop control can contain an expression, with the first also being allowed to contain a variable declaration and the second being required to result in a boolean value.
Thus it would be legal to have the following:
...
for (MyObj = new MyObj(); MyObj; MyObj.MoveNext())
{
...
}
...
provided that MyObj implements an implicit bool conversion operator.
You can check out the C# language specification for the exact rules for the formation of a for loop; see section 8.8.3 of the C# Language Specification 1.2.doc, which you can find in your VS folder, in VC#\Specifications\1033.
|
|
|
|
|
And can also include the little-known , (COMMA) operator.
|
|
|
|
|
I thought that was only in C++? I don't know for sure though... I am at home now.
Cheers,
Vikram. (Proud to have finally cracked a CCC!) Recent activities:
TV series: Friends, season 10
Books: Fooled by Randomness, by Nassim Nicholas Taleb. Carpe Diem.
|
|
|
|
|
It seems C# limits it somehow, and I'm reading the spec now to see why.
"
8.8.3 The for statement
The for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly executes an embedded statement and evaluates a sequence of iteration expressions.
for-statement:
for ( for-initializer ; for-condition ; for-iterator ) embedded-statement
"
for ( int i = 0 ; i < 10 ; i++ , i++ )
{
System.Console.WriteLine ( i ) ;
}
or
int i ;
for ( i = 0 , i++ ; i < 10 ; i++ , i++ )
{
System.Console.WriteLine ( i ) ;
}
The comma operator is valid in the for-iterator, but not in the for-initializer and for-condition (they are allowed there in C), this may be an oversight.
I may ask on an MSDN forum later.
|
|
|
|
|
Yeah, you're right. Turns out you can even do multiple declarations:
for ( int i = 0, j = 4 ; i < 10; i++ , i++, j++ )
{
System.Console.WriteLine ( i + " " + j) ;
}
Another arcane fact about C# that I'll probably never use in my life
Cheers,
Vikram. (Proud to have finally cracked a CCC!) Recent activities:
TV series: Friends, season 10
Books: Fooled by Randomness, by Nassim Nicholas Taleb. Carpe Diem.
|
|
|
|
|
Yeah, obfuscation.
I checked and it's been like that since v1.1
|
|
|
|
|
Maybe you're right. I see no mention of the comma operator in either C# spec.
C-74 (Dennis Ritchey)
"
7.15 expression , expression
A pair of expressions separated by a comma is evaluated lefttoright
and the value of the left expression is discarded.
The type and value of the result are the type and value of the right operand. This operator groups lefttoright.
It should be avoided in situations where comma is given a special meaning, for example in actual arguments
to function calls (§7.1.6) and lists of initializers (§10.2).
"
C-99 (ANSI)
"
The left operand of a comma operator is evaluated as a void expression; there is a
sequence point after its evaluation. Then the right operand is evaluated; the result has its
type and value.95) If an attempt is made to modify the result of a comma operator or to
access it after the next sequence point, the behavior is undefined.
"
But C# has no comma operator, the effect is provided by:
C# (ECMA)
"
statement-expression:
invocation-expression
object-creation-expression
assignment
post-increment-expression
post-decrement-expression
pre-increment-expression
pre-decrement-expression
statement-expression-list:
statement-expression
statement-expression-list , statement-expression
"
And that's not allowed in the for-condition.
|
|
|
|
|
Ah...ok...thanks about that....
Now, my other problem is how to use the symbols '^', '?', '*', \s, \t, \n to include in the pattern. i already read how they work. its just that i don't know where to put them to make the pattern correct.
for example:
@"([for]\s*)";
is the location of the \s* correct? if not, where should i put it? Can anyone give me suggestions on where to put those things in the pattern...
|
|
|
|
|
Creating a pattern matching system for even a basic "For" loop is very complex, and would be rather more detailed that your example. For example, consider these cases which are legal, but which fail your existing pattern:
for (int i=0;i<9;i++)
for(int i=0;i<10;i++)
for(int i=0;i<9;i+=2)
for(i=1;i<9;i++)
for(int i=0;i<array.Length;i++)
for(int index=0;index<array.Length;index++)
and the following which passes, but which is not a good idea
for(int i=0;j<10;k++)
I suggest you have a look at Extended Backus-Naur form which is a metalanguage for describing languages!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
gamer1127 wrote: Is that pattern correct?
No. It's perfectly possible to have a statement that reads
for ( ; ; ) . Using a regular expression is not an effective method to accomplish this - a better method would be to use an Abstract Syntax Tree[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Now that you mentioned the Abstract Syntax Tree, I remember that we are required to use what they call a parse tree. is that also the same with Abstract Syntax Tree?
|
|
|
|
|
for(;; ) is also legal
Actually it's something like (no guarantees)
FOR = 'for' '(' {CSL(INIT)}? ';' {BOOL}? ';' {CSL(EXPR)}? ')'
CSL(x) = x (',' x)*
INIT = EXPR | DECL
BOOL = EXPR where the return type is a bool or can be implicitly converted to one, or has operators "true" and "false" (so for example indexing an array of bools is perfectly legal)
EXPR = any expression
DECL = any declaration possibly with initializer
any whitespace between tokens is ignored
float f;
bool[] b = new bool[] { false, true, true, true};
for(f = 3; b[(int)f]; f/=1.2f; ) ;
Is legal. I wouldn't especially recommend it, but it will compile and will do something that could be useful if b weren't a "constant" array.
|
|
|
|
|
For one thing, don't have the for and int in brackets.
You really need to learn more about Regular Expressions. I try to work them up a little at a time, not all at once.
And, as mentioned, this is not the right way to go for this task.
|
|
|
|
|
Is there such a way to do this?
I have a textbox on the GUI. I'm watching the string value from the other class. Whenever the string value is changed, I would like to update the textbox value.
|
|
|
|
|
Good Day Blubbo
Your Textbox has an event "Textchanged" use it, it does exactly what you want.
Hope it helps.
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
to clarify this... I know how to handle the event by textbox.texthchanged. However, that in class 'b', "string" object is only being processed. So when the string value in class "b" is changed, the class "a" would see that string value in class 'b' is changed, the value in textbox is updated.
|
|
|
|
|
Now that means you have create the events in a class level. The way we approach the design of our application in our Project initial state can lead to this requirement. I think there is a part of your system that was poorly designed. but the workaround this is to create an event and if the value of class changes and you can change the text property of your textbox.
Hope this Helps
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
I think he's going the other way... Updating the textbox when the string changes.
There are plenty of ways to do this, and people will vehemently disagree on which is best, but here's one possible method:
1) Have your class implement the System.ComponentModel.INotifyPropertyChanged interface, and add the PropertyChanged event as specified in the interface
2) Add a wrapper for it, just to keep things clean:
private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName));
}
3) Instead of having the string just be a variable, change it into a property, like so:
private string _myStringValue;
public string MyProperty
{
get { return _myStringValue; }
set { _myStringValue = value; PropertyChanged("MyProperty"); }
}
Then you just listen to the PropertyChanged event, and update the textbox when "MyProperty" comes through.
|
|
|
|
|
so that "PropertyChanged" idea would be placed in which class? (class 'a' or class 'b') as explained in my recent post.
|
|
|
|