如何使用默认构造函数显示字符串格式 [英] How to display string format with the default constructor

查看:117
本文介绍了如何使用默认构造函数显示字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的任务是编写一个程序,使用默认的构造函数和参数化控件从列表框中显示月/日"格式.

我已经处理了参数化过程之一,但是我在使用默认构造函数时遇到了麻烦.

这是Rectangle.vb
的代码

Hello, My assignment is to write a program that displays the format "month/day" from the listboxes using the default constructor and the parameterized controls.

I took care of the parameteriezed procedure one, but I''m having trouble with the default constructor.

Here is the code for Rectangle.vb

Public Class FormattedDate
    Private _strMonth As String
    Private _strDay As String

    Public Sub New()
        _strMonth = String.Empty
        _strDay = String.Empty
    End Sub
    Public Sub New(ByVal Month As String,
                   ByVal Day As String)
        _strMonth = Month
        _strDay = Day

    End Sub

    Public Function GetDate() As String
        Return _strMonth & "/" & _strDay
    End Function

    Public Property Month() As String
        Get
            Return _strMonth
        End Get
        Set(ByVal value As String)
            Dim intMonth As Integer
            Integer.TryParse(value, intMonth)
            If intMonth < 1 OrElse intMonth > 12 Then
                _strMonth = String.Empty
            Else
                _strMonth = value
            End If
        End Set
    End Property

    Public Property Day() As String
        Get
            Return _strDay
        End Get
        Set(ByVal value As String)
            _strDay = value
        End Set
    End Property

End Class



这是参数化过程的代码



Here''s the code for the parameterized procedure

Private Sub btnParameterized_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnParameterized.Click
        Dim myDate As New FormattedDate

        myDate = New FormattedDate(lstMonth.SelectedItem.ToString, lstDay.SelectedItem.ToString)

        lblHireDate.Text = myDate.GetDate

    End Sub



这是默认构造函数过程的代码,我很难获取列表框的月份和日期以链接到函数过程.



Here''s the code for the default constructor procedure, and I''m having trouble to get the list box month and day to link to the function procedure.

Private Sub btnDefault_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.Click

       Dim formatDate As New FormattedDate()
       Dim Month As String
       Dim Day As String

       Month = lstMonth.SelectedItem.ToString
       Day = lstDay.SelectedItem.ToString
       lblHireDate.Text = formatDate.GetDate

   End Sub



我究竟做错了什么?我想念什么?
谢谢.



What am I doing wrong? What am I missing?
thanks.

推荐答案

好吧,您永远都不会分配日期和月份

Well you are never assigning Day and Month

formatDate.Month = Month
formatDate.Day = Day





Private Sub btnDefault_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.Click
    Dim formatDate As New FormattedDate()
    Dim Month As String
    Dim Day As String
 
    Month = lstMonth.SelectedItem.ToString
    Day = lstDay.SelectedItem.ToString
    formatDate.Day = Day
    formatDate.Month = Month
    lblHireDate.Text = formatDate.GetDate
End Sub


这篇关于如何使用默认构造函数显示字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆