|
Do like this :
int year = 2009;
int month = 2;
int nos = 0;
DateTime startDate = new DateTime(year, month, 1);
DateTime endDate = startDate.AddMonths(1);
while (startDate.DayOfWeek != DayOfWeek.Monday)
startDate = startDate.AddDays(1);
for (DateTime result = startDate; result < endDate; result = result.AddDays(7))
nos+=1;
Hope this help.
Cheers.
|
|
|
|
|
Here's a method you could use...
int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
{
DateTime dt = new DateTime(year, month, 1);
int result = 0;
for(int i = 0; i < DateTime.DaysInMonth(year, month); i++)
{
if(dt.DayOfWeek == dayToCount)
result++;
dt.AddDays(1);
}
return result;
}
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Improved...
int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
{
DateTime dt = new DateTime(year, month, 1);
int firstInstance = 1;
if((int)dt.DayOfWeek > (int)dayToCount)
firstInstance += (7 - (int)dt.DayOfWeek) + (int)dayToCount;
else if((int)dt.DayOfWeek < (int)dayToCount)
firstInstance += (int)dayToCount - (int)dt.DayOfWeek;
return firstInstance <= (DateTime.DaysInMonth(month, year) - 28) ? 5 : 4;
}
...I think the logic is right.
[EDIT] That was way off, I fixed now... I think!
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I'm not too sure about the various constants, but it looks like The Right Way to do it
|
|
|
|
|
On second thoughts, it does not look much improved
The constants are OK, they will never need to change
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
harold aptroot wrote: it looks like The Right Way to do it
really?
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
*reconsiders*
Yes?
No seriously, no matter how wrong it was, not having a loop made it much closer than all those other entries..
|
|
|
|
|
What I came up with is similar to that:
public static int
F
(
int Year
,
int Month
,
System.DayOfWeek DayOfWeek
)
{
int d = (int) DayOfWeek - (int) (new System.DateTime ( Year , Month , 1 )).DayOfWeek ;
if ( d < 0 )
{
d += 7 ;
}
int n = System.DateTime.DaysInMonth ( Year , Month ) - 28 ;
return ( d < n ? 5 : 4 ) ;
}
|
|
|
|
|
What??? It does not work?
What's with the 'Bad Answer' again?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Obviously you do not need any loop or conditional for this.
Have a look at:
int mondays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 6).DayOfWeek) / 7;
What remains of this homework is for you to figure out why the expression is what it is!
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
No, it's obvious that no loop is required, but I still have two conditionals in mine.
I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week.
On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.
|
|
|
|
|
IMO it works for all years and months, I checked for this year only.
With a day-of-week parameter it becomes:
int Xdays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 7-dayOfWeek).DayOfWeek) / 7;
where dayOfWeek is 0=SU, 1=MO, ... 6=SA
[ADDED] so you can also use (int)DayOfWeek [/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Wednesday, November 18, 2009 3:36 PM
|
|
|
|
|
PIEBALDconsult wrote: I expect the OP's teacher only wants to prove that he's smarter than his students
The teacher may be shaded then by a cheating student.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Lol guys Thank you very much for the help but actually this ain't for homework hehe, this is part of a payroll system that I am developing at work and with the deadline stress and from being tired because been programming 8 hours strait i couldn't find a solution for this so i turned to some help hehe thank you again guys.
|
|
|
|
|
I have a text file
Each line is the record as shown below
000071000456789987000000008250000000000000000000
in top line :
00007 = id field
1000456789987 = sn field
000000008250000 = amount field
000000000000000 = blank field
The question is :
How can I read each line in that file?
help me please very important for me
|
|
|
|
|
Member 4737022 wrote: How can I read each line in that file?
StreamReader()[^] should do it.
|
|
|
|
|
another idea would be using the File.ReadAllLines method :P
this is easily found on google
Here we rest... So why not make the best of it?
|
|
|
|
|
|
there is no need for loop with the ReadAllLines method.. It returns a string array containing all the lines of a file
Here we rest... So why not make the best of it?
|
|
|
|
|
He said he was using this file as a database, which tend to be large. I'd personally prefer not to have an entire database in a string, whereas I can read a line, perform operations and discard of it keeping my memory footprint down.
|
|
|
|
|
You can use StreamReader[^] class to read the file line by line.
If your fields are with fixed lengths you can use the String.Substring[^] method to extract the fields from the line. Here is an example:
string filePath = @"Path_to_your_file_here";
string line;
int idLength = 5, snLength = 13, amountLength = 15, blankLength = 15;
if (File.Exists(filePath))
{
StreamReader file = null;
try
{
file = new StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
string id = line.Substring(0, idLength);
string sn = line.Substring(idLength, snLength);
string amount = line.Substring(idLength + snLength, amountLength);
string blank = line.Substring(idLength + snLength + amountLength, blankLength);
}
}
finally
{
if (file != null)
file.Close();
}
}
I hope this helps.
Regards,
Nuri Ismail
|
|
|
|
|
Very thanks mr nuri ismail
|
|
|
|
|
You're welcome!
Regards
|
|
|
|
|
use StreamReader
int id;
int sn;
double amount;
int blank;
StreamReader sr = new StreamReader("C:\FileFolder\myfile.txt");
String strInput="";
strInput=sr.ReadLine();
while ((strInput = sr.ReadLine()) != null )
{
try
{
id = int.Parse(strInput.Substring(0,5));
sn=double.Parse(strInput.Substring(5,13));
}
catch(Exception e)
{
}
}
NB. Just a learner so my code is not the best but there is a general idea
|
|
|
|
|
Do you want to have all the records in memory at once? Or do you want to read each particular record based on its ID or SN (e.g. GetRecord ( 7 ) )?
What are you going to do with the data?
|
|
|
|