|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Concatenate two Integers '2009/10' (VB)
Hi,
I found the some code which populates a dropdown list with current year, -4 years and + 11 years, ie. 2005 to 2020; Sub Populate_YearList() ddl_year.Items.Add("Include All") 'Year list can be extended Dim intYear As Integer For intYear = DateTime.Now.Year - 4 To DateTime.Now.Year + 11 ddl_year.Items.Add(intYear) Next ddl_year.Items.FindByValue(DateTime.Now.Year).Selected = True End Sub The rest of the code can be seen here: aspnet.4guysfromrolla.com/demos/Cal_2.aspx However, I would like it appear as 2005/06, 2006/07, 2007/08, etc. When I try to add even the "/" I get 'Object not set as a reference' error or 'Input string not formatted properly'. Any suggestions would be most appreciated. Thanks in advance. James. |
|
#2
|
||||
|
||||
|
The problem is your intYear variable is specifically defined and an integer, and an integer cannot have a "/" in it. But that code is old now anyway. If you're using Visual Studio 2008, I'd write it like this:
Code:
Sub Populate_YearList()
ddl_year.DataSource = Enumerable.Range(DateTime.Now.Year - 4, 15).Select(Function(i) String.Format("{0}/{1:N2}", i, i-1999) )
ddl_year.DataBind()
ddl_year.Items.FindByValue(DateTime.Now.Year).Selected = True
End Sub
__________________
Primary Forum: .Net Development Holy cow, I'm now an ASP.Net MVP! [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers] http://twitter.com/jcoehoorn Last edited by f'lar : October 29th, 2009 at 09:08 AM. |
|
#3
|
|||
|
|||
|
OK cool. Many thanks for that....
Unfortunately we have VS2003 at work. Tried it today and it didn't like Enumerable.Range and Function. Sub Populate_YearList() ddl_year.DataSource = Enumerable.Range(DateTime.Now.Year - 4, 15).Select(Function(i) String.Format("{0}/{1}", i, i-1999) ) ddl_year.DataBind() ddl_year.Items.FindByValue(DateTime.Now.Year).Selected = True End Sub If anyway can translate to 1.1 that would be great. Thanks. |
|
#4
|
||||
|
||||
|
1.1? Ugh. I'd want need to at least move up to 2005, which has iterator blocks and proper generics support.
That said, here you go: Code:
Sub Populate_YearList()
ddl_year.Items.Add("Include All")
'Year list can be extended
Dim Year As Integer = DateTime.Now.Year - 4
For i As Integer = 0 To 15
ddl_year.Items.Add( String.Format("{0}/{1:n2}", Year, Year - 1999) )
Year += 1
Next i
ddl_year.Items(4).Selected = True
End Sub
Not sure I got the "n2" format right, but you can look that up. Just search msdn for string.format. Last edited by f'lar : October 29th, 2009 at 09:16 AM. |
|
#5
|
|||
|
|||
|
Thanks f'lar. Sorry I haven't replied sooner. I will give it a try.
I appreciate you taking time out to help. Best regards. Jwebb001. Quote:
|
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Concatenate two Integers '2009/10' (VB) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|