Display Date From Array Indexes

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 630 Views - Last Post: 29 January 2013 - 08:23 AM Rate Topic: -----

#1 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Display Date From Array Indexes

Posted 28 January 2013 - 05:00 PM

Hi Everyone.

I have an array as displayed:

 Dim Date_Array(30, 11, 9998) As Integer 


What i want to do is display the date dependent on the index position displayed... for example:

Displaying information that is stored in array (25, 07, 2013)

This will then display the date "Thursday 25th July 2013" and the information stored in the array.

Hope you can help :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Display Date From Array Indexes

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Display Date From Array Indexes

Posted 28 January 2013 - 05:12 PM

View PostJordan.Hickin91, on 28 January 2013 - 06:00 PM, said:

I have an array as displayed:
 Dim Date_Array(30, 11, 9998) As Integer 

What i want to do is display the date dependent on the index position displayed... for example:
Displaying information that is stored in array (25, 07, 2013)
This will then display the date "Thursday 25th July 2013" and the information stored in the array.

Well, you can pull the day, month and year from a Date variable. Converting those to integers will give you the indices into the Integer array.

So what is it that's giving you problems? WHat have you tried so far?
Was This Post Helpful? 0
  • +
  • -

#3 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 06:20 PM

View Postlar3ry, on 28 January 2013 - 05:12 PM, said:

View PostJordan.Hickin91, on 28 January 2013 - 06:00 PM, said:

I have an array as displayed:
 Dim Date_Array(30, 11, 9998) As Integer 

What i want to do is display the date dependent on the index position displayed... for example:
Displaying information that is stored in array (25, 07, 2013)
This will then display the date "Thursday 25th July 2013" and the information stored in the array.

Well, you can pull the day, month and year from a Date variable. Converting those to integers will give you the indices into the Integer array.

So what is it that's giving you problems? WHat have you tried so far?


After reading your reply i think i was looking at it backwards... i was trying to change the indices to affect the date... when really i should have been changing the date to affect the indices :/ but now i have a problem with this line:

 Day_Box1.Text = Format(Date1, "dddd " & getday(Date.Today.DayOfWeek) & " MMMM yyyy") 


it is displaying this:

Thursday 2A12 January 2013

here is the getday code:

    Function getday(ByVal day As Integer)
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return day & "st"

            Case Is = 2 Or 22
                Return day & "nd"

            Case Is = 3 Or 23
                Return day & "rd"

            Case Else
                Return day & "th"

        End Select
    End Function


Any ideas? I can't seem to get it to display properly... it is probably something i need a fresh pair of eyes to notice?
Was This Post Helpful? 0
  • +
  • -

#4 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 06:30 PM

OK i got it to display the correct date its just the A12 that is displaying in stead of the "th" :/
Was This Post Helpful? 0
  • +
  • -

#5 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 06:36 PM

I think i found the problem but not sure how to fix it....

Function getday(ByVal day As String)
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return day & "st"

            Case Is = 2 Or 22
                Return day & "nd" '<THIS LINE HERE FOR EXAMPLE

            Case Is = 3 Or 23
                Return day & "rd"

            Case Else
                Return day & "th"

        End Select
    End Function



Is displaying as 22n22 (if i am not mistake when handling dates "d" will display today... so the function is returning daynday (day(22) first part of "nd" string n and second part of string (d) 22)

Any Ideas?
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Display Date From Array Indexes

Posted 28 January 2013 - 06:51 PM

View PostJordan.Hickin91, on 28 January 2013 - 07:30 PM, said:

OK i got it to display the correct date its just the A12 that is displaying in stead of the "th" ://>/>/>

A few things wrong. The first is that you want to refer to Date1 in your Function call, not Date. You also don't need the "Today", and you are asking for the day of the week, which is a number from 1-7. Your function call should be

          Day_Box1.Text = Format(Date1, "dddd " & getday(Date1.Day) & " MMMM yyyy")


which will return the day of the month, which is what you really want.

Edit: oops.. forgot to mention... a Function should always specify what it returns, so:

   Function getday(ByVal day As Integer) As String


This post has been edited by lar3ry: 28 January 2013 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 06:57 PM

View Postlar3ry, on 28 January 2013 - 06:51 PM, said:

View PostJordan.Hickin91, on 28 January 2013 - 07:30 PM, said:

OK i got it to display the correct date its just the A12 that is displaying in stead of the "th" ://>/>

A few things wrong. The first is that you want to refer to Date1 in your Function call, not Date. You also don't need the "Today", and you are asking for the day of the week, which is a number from 1-7. Your functioa call should be

          Day_Box1.Text = Format(Date1, "dddd " & getday(Date1.Day) & " MMMM yyyy")


which will return the day of the month, which is what you really want.


I have this now

Sub Update_Date()
        Day_Box1.Text = Format(Date1, "dddd " & getday(Date1.Day) & " MMMM yyyy")
        Day_Box2.Text = Format(Date2, "dddd " & getday(Date2.Day) & " MMMM yyyy"
    End Sub

    Function getday(ByVal day As String)
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return day & "st"

            Case Is = 2 Or 22
                Return day & "nd"

            Case Is = 3 Or 23
                Return day & "rd"

            Case Else
                Return day & "th"

        End Select
    End Function



and it is returning this:

Tuesday 29A12 January 2013

So something is amiss? here is the full code... bit messy atm:

Public Class Form1
    Dim Date_Array(30, 11, 9998) As String
    Dim Date1 As Date = Today
    Dim Date2 As Date = Today.AddDays(1)

    Sub Update_Date()
        Day_Box1.Text = Format(Date1, "dddd " & getday(Date1.Day) & " MMMM yyyy")
        Day_Box2.Text = Format(Date2, "dddd " & getday(Date2.Day) & " MMMM yyyy")
        If Date2 = Date1 Then
            Date2.AddDays(1)
            Update_Date()
        End If
    End Sub

    Function getday(ByVal day As String)
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return day & "st"

            Case Is = 2 Or 22
                Return day & "nd"

            Case Is = 3 Or 23
                Return day & "rd"

            Case Else
                Return day & "th"

        End Select
    End Function

    Private Sub Date_Control(Sender As Object, e As EventArgs) Handles Btn_DayBk.Click, Btn_DayFwd.Click, Btn_MnthBk.Click, Btn_MnthFwd.Click
        Select Case Sender.Name

            Case Is = "Btn_DayBk"
                Date1 = Date1.AddDays(-1)
                Date2 = Date2.AddDays(-1)
                Update_Date()
                MsgBox("Moved 1 Day Backwards")

            Case Is = "Btn_DayFwd"
                Date1 = Date1.AddDays(1)
                Date2 = Date2.AddDays(1)
                Update_Date()
                MsgBox("Moved 1 Day Forwards")

            Case Is = "Btn_MnthBk"
                Date1 = Date1.AddMonths(-1)
                Date2 = Date2.AddMonths(-1)
                Update_Date()
                MsgBox("Moved 1 Month Backwards")

            Case Is = "Btn_MnthFwd"
                Date1 = Date1.AddMonths(1)
                Date2 = Date2.AddMonths(1)
                Update_Date()
                MsgBox("Moved 1 Month Forwards")

        End Select

    End Sub

    Private Sub Btn_CustDate_Click(sender As Object, e As EventArgs) Handles Btn_CustDate.Click
        Dim Dat1 As String() = Split(TextBox1.Text, "/")
        Dim X As Integer = Dat1(0)
        Dim Y As Integer = Dat1(1)
        Dim Z As Integer = Dat1(2)

        MsgBox(Date_Array(X, Y, Z))
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Update_Date()
    End Sub


Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Display Date From Array Indexes

Posted 28 January 2013 - 07:02 PM

The day parameter of your function should be an integer.

You would be doing yourself a great service if you add the following two lines above the Class statemment in every VB.Net program you write:

Option Strict On
Option Explicit On



This post has been edited by lar3ry: 28 January 2013 - 08:06 PM

Was This Post Helpful? 2
  • +
  • -

#9 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 07:11 PM

View Postlar3ry, on 28 January 2013 - 07:02 PM, said:

The day parameter of your function should be an integer.

You would be doing yourself a great service if you add the following two lines above the Class statemment in every VB.Net program you write:

Option Strict On
Option Explicit On]



Well That brought up some interesting results. now I have 3-4 errors across 2 lines:
Option Strict On
Option Explicit On

Public Class Form1
    Dim Date_Array(30, 11, 9998) As String
    Dim Date1 As Date = Today
    Dim Date2 As Date = Today.AddDays(1)

    Sub Update_Date()
        Day_Box1.Text = Format(Date1, "dddd " & getday(Date1.Day) & " MMMM yyyy") 'Error: prohibits operands

        Day_Box2.Text = Format(Date2, "dddd " & getday(Date2.Day) & " MMMM yyyy")'Error: prohibits operands
        If Date2 = Date1 Then
            Date2.AddDays(1)
            Update_Date()
        End If
    End Sub

    Function getday(ByVal day As Integer) 'Error Here: Requires an "AS" clause ://>
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return day & "st"

            Case Is = 2 Or 22
                Return day & "nd"

            Case Is = 3 Or 23
                Return day & "rd"

            Case Else
                Return day & "th"

        End Select
    End Function

    Private Sub Date_Control(Sender As Object, e As EventArgs) Handles Btn_DayBk.Click, Btn_DayFwd.Click, Btn_MnthBk.Click, Btn_MnthFwd.Click
        Select Case Sender.Name 'Error here: Disallows Late Bindings

            Case Is = "Btn_DayBk"
                Date1 = Date1.AddDays(-1)
                Date2 = Date2.AddDays(-1)
                Update_Date()
                MsgBox("Moved 1 Day Backwards")

            Case Is = "Btn_DayFwd"
                Date1 = Date1.AddDays(1)
                Date2 = Date2.AddDays(1)
                Update_Date()
                MsgBox("Moved 1 Day Forwards")

            Case Is = "Btn_MnthBk"
                Date1 = Date1.AddMonths(-1)
                Date2 = Date2.AddMonths(-1)
                Update_Date()
                MsgBox("Moved 1 Month Backwards")

            Case Is = "Btn_MnthFwd"
                Date1 = Date1.AddMonths(1)
                Date2 = Date2.AddMonths(1)
                Update_Date()
                MsgBox("Moved 1 Month Forwards")

        End Select

    End Sub

    Private Sub Btn_CustDate_Click(sender As Object, e As EventArgs) Handles Btn_CustDate.Click
        Dim Dat1 As String() = Split(TextBox1.Text, "/")
        Dim X As Integer = CInt(Dat1(0))
        Dim Y As Integer = CInt(Dat1(1))
        Dim Z As Integer = CInt(Dat1(2))

        MsgBox(Date_Array(X, Y, Z))
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Update_Date()
    End Sub
End Class



Any help?
Was This Post Helpful? 0
  • +
  • -

#10 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 07:23 PM

I got it to this:
Option Strict On
Option Explicit On

Public Class Form1
    Dim Date_Array(30, 11, 9998) As String
    Dim Date1 As Date = Today
    Dim Date2 As Date = Today.AddDays(1)

    Sub Update_Date()
        Day_Box1.Text = Format(Date1, "dddd " & CInt(getday()) & " MMMM yyyy")
        Day_Box2.Text = Format(Date2, "dddd " & CInt(getday()) & " MMMM yyyy")
        If Date2 = Date1 Then
            Date2.AddDays(1)
            Update_Date()
        End If
    End Sub

    Function getday() As Integer
        Select Case getday 'not sure how to do this part... as there could be two dates?

            Case Is = 1 Or 21 Or 31
                Return CInt(getday & "st")

            Case Is = 2 Or 22
                Return CInt(getday & "nd")

            Case Is = 3 Or 23
                Return CInt(getday & "rd")

            Case Else
                Return CInt(getday & "th")

        End Select
    End Function

    Private Sub Date_Control(Sender As Object, e As EventArgs) Handles Btn_DayBk.Click, Btn_DayFwd.Click, Btn_MnthBk.Click, Btn_MnthFwd.Click
        Select Case Sender.Name ' not sure what the problem is here

            Case Is = "Btn_DayBk"
                Date1 = Date1.AddDays(-1)
                Date2 = Date2.AddDays(-1)
                Update_Date()
                MsgBox("Moved 1 Day Backwards")

            Case Is = "Btn_DayFwd"
                Date1 = Date1.AddDays(1)
                Date2 = Date2.AddDays(1)
                Update_Date()
                MsgBox("Moved 1 Day Forwards")

            Case Is = "Btn_MnthBk"
                Date1 = Date1.AddMonths(-1)
                Date2 = Date2.AddMonths(-1)
                Update_Date()
                MsgBox("Moved 1 Month Backwards")

            Case Is = "Btn_MnthFwd"
                Date1 = Date1.AddMonths(1)
                Date2 = Date2.AddMonths(1)
                Update_Date()
                MsgBox("Moved 1 Month Forwards")

        End Select

    End Sub

    Private Sub Btn_CustDate_Click(sender As Object, e As EventArgs) Handles Btn_CustDate.Click
        Dim Dat1 As String() = Split(TextBox1.Text, "/")
        Dim X As Integer = CInt(Dat1(0))
        Dim Y As Integer = CInt(Dat1(1))
        Dim Z As Integer = CInt(Dat1(2))

        MsgBox(Date_Array(X, Y, Z))
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Update_Date()
    End Sub
End Class



Scrap that.. done the problem with the getday.. just the sender problem
Was This Post Helpful? 0
  • +
  • -

#11 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 28 January 2013 - 07:30 PM

ok it is still displaying A12 on my date :/ and I have no idea how to fix the sender.name error

and I changed Cint to Cstr through the getday function... due to a conversion error..... but still showing the unusal A12 in date
Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Display Date From Array Indexes

Posted 28 January 2013 - 08:18 PM

You are changing all the wrong things. Take a little time and think about what I've said...

Your getday() should look like this:
    Function getday(ByVal day As Integer) As String
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return CStr(day) & "st"

            Case Is = 2 Or 22
                Return CStr(day) & "nd"

            Case Is = 3 Or 23
                Return CStr(day) & "rd"

            Case Else
                Return CStr(day) & "th"
        End Select
    End Function


The parameter should remain an integer (ByVal day as Integer)
The return value is a String ( As String )
You need the integer (day) for the Select Case
You need to convert the integer to a string to concatenate it with the suffix and to return the String.
"st", "nd", and "th" are already strings.

And you don't need to convert the return from getday() to an integer. You need it as a string to fill the TextBox, and it comes back from getday() as a string.
Was This Post Helpful? 0
  • +
  • -

#13 Jordan.Hickin91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 13

Re: Display Date From Array Indexes

Posted 29 January 2013 - 07:39 AM

Option Strict On
Option Explicit On

Public Class Form1
    Dim Date_Array(30, 11, 9998) As String
    Dim Date1 As Date = Today
    Dim Date2 As Date = Today.AddDays(1)

    Sub Update_Date()
        Day_Box1.Text = Format(Date1, "dddd " & CStr(getday(Date1.Day)) & " MMMM yyyy")
        Day_Box2.Text = Format(Date2, "dddd " & CStr(getday(Date2.Day)) & " MMMM yyyy")
        If Date2 = Date1 Then
            Date2.AddDays(1)
            Update_Date()
        End If
    End Sub

    Function getday(ByVal day As Integer) As String
        Select Case day

            Case Is = 1 Or 21 Or 31
                Return CStr(day) & "st"

            Case Is = 2 Or 22
                Return CStr(day) & "nd"

            Case Is = 3 Or 23
                Return CStr(day) & "rd"

            Case Else
                Return CStr(day) & "th"

        End Select
    End Function


    Private Sub Date_Control(Sender As Object, e As EventArgs) Handles Btn_DayBk.Click, Btn_DayFwd.Click, Btn_MnthBk.Click, Btn_MnthFwd.Click
        Select Case Sender 'Error Here, Late Binding ????

            Case Is Btn_DayBk
                Date1 = Date1.AddDays(-1)
                Date2 = Date2.AddDays(-1)
                Update_Date()
                MsgBox("Moved 1 Day Backwards")

            Case Is = "Btn_DayFwd"
                Date1 = Date1.AddDays(1)
                Date2 = Date2.AddDays(1)
                Update_Date()
                MsgBox("Moved 1 Day Forwards")

            Case Is = "Btn_MnthBk"
                Date1 = Date1.AddMonths(-1)
                Date2 = Date2.AddMonths(-1)
                Update_Date()
                MsgBox("Moved 1 Month Backwards")

            Case Is = "Btn_MnthFwd"
                Date1 = Date1.AddMonths(1)
                Date2 = Date2.AddMonths(1)
                Update_Date()
                MsgBox("Moved 1 Month Forwards")

        End Select

    End Sub

    Private Sub Btn_CustDate_Click(sender As Object, e As EventArgs) Handles Btn_CustDate.Click
        Dim Dat1 As String() = Split(TextBox1.Text, "/")
        Dim X As Integer = CInt(Dat1(0))
        Dim Y As Integer = CInt(Dat1(1))
        Dim Z As Integer = CInt(Dat1(2))

        MsgBox(Date_Array(X, Y, Z))
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Update_Date()
    End Sub
End Class



That is all I have now... Date is still displaying wrong and still lost with the sender error :/
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Display Date From Array Indexes

Posted 29 January 2013 - 07:50 AM

What is the type of the parameter named sender?

Private Sub Date_Control(Sender As Object, e As EventArgs) Handles

It's Object and since object the base type for all other types. Sender could be anything. Since it could be anything it doesn't know if it has .Name at compile-time.

So you need to check to see if the sender is a control first.
If Not (TypeOf sender Is Control) Then End Sub
Dim c = DirectCast(sender,Control)


If it then directly cast to one.

This post has been edited by AdamSpeight2008: 29 January 2013 - 07:53 AM

Was This Post Helpful? 1
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,792
  • Joined: 12-December 12

Re: Display Date From Array Indexes

Posted 29 January 2013 - 07:55 AM

You are embedding, for example, "th" in your Format string-specifier. So, for example, your code equates to:

Format(Date1, "dddd 2thMMMM yyyy")
and the letters t and h are then used to format the date. MSDN

One way to handle this is to split it into three parts:

someString = Format(Date1, "dddd ")
someString = someString & CStr(getday(Date1.Day))
someString = someString & Format(Date1, " MMMM yyyy")

This post has been edited by andrewsw: 29 January 2013 - 08:06 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2