Results 1 to 4 of 4

Thread: Sub Classes?

  1. #1

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Sub Classes?

    I don't know how to do this, but this is what I want to do in code:

    Code:
    Dim oWeek as new FiscalWeek(iWeekNumber, iYearNumber)
    
    iWeek = oWeek.CurrentWeek.WeekNumber
    iYear = oWeek.CurrentWeek.YearNumber
    iNextWeek = oWeek.NextWeek.WeekNumber
    iLastWeek = oWeek.NextWeek.YearNumber
    I can "kinda" do it by making a class inside a class:

    Code:
    Public Class FiscalWeek
    
    
      Private Shared iNextWeek As Integer
      Private Shared iLastWeek As Integer
      Private Shared iCurrentWeek As Integer
      Private Shared iNextYear As Integer
      Private Shared iLastYear As Integer
    
      Public CurrentWeek As New cCurrentWeek()
      Public NextWeek As New cNextWeek()
    
      Class cCurrentWeek
    
        Public ReadOnly Property WeekNumber() As Integer
    
          Get
            Return iCurrentWeek
          End Get
    
        End Property
    
        Public ReadOnly Property YearNumber()
    
          Get
            Return iCurrentYear
          End Get
    
        End Property
    
      End Class
    
      Public Class cNextWeek
    
        Public ReadOnly Property WeekNumber() As Integer
    
          Get
            Return iNextWeek
          End Get
    
        End Property
    
        Public ReadOnly Property YearNumber()
    
          Get
            Return iNextYear
          End Get
    
        End Property
    This was working pretty much, but I've found when I created two seperate instances of oWeek the Current week gets changed in both when I change it in one. it dosn't make sense to me, but I must be doing this wrong, what is the propper way to have a "nested property"?
    Last edited by Dmyze; Jan 13th, 2003 at 05:35 PM.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Shared will share the same data for all instances which is not what you want. Try something along this line:
    VB Code:
    1. Public Class FiscalWeek
    2.  
    3.     Private _CurrentWeek As New SubWeek()
    4.     Private _NextWeek As New SubWeek()
    5.  
    6.     Public Property CurrentWeek() As SubWeek
    7.         Get
    8.             Return _CurrentWeek
    9.         End Get
    10.         Set(ByVal Value As SubWeek)
    11.             _CurrentWeek = Value
    12.         End Set
    13.     End Property
    14.  
    15.     Public Property NextWeek() As SubWeek
    16.         Get
    17.             Return _NextWeek
    18.         End Get
    19.         Set(ByVal Value As SubWeek)
    20.             _NextWeek = Value
    21.         End Set
    22.     End Property
    23.  
    24. End Class
    25.  
    26. Public Class SubWeek
    27.  
    28.     Private _WeekNumber As Integer
    29.     Private _YearNumber As Integer
    30.  
    31.     Public ReadOnly Property WeekNumber() As Integer
    32.         Get
    33.             Return _WeekNumber
    34.         End Get
    35.     End Property
    36.  
    37.     Public ReadOnly Property YearNumber()
    38.         Get
    39.             Return _YearNumber
    40.         End Get
    41.     End Property
    42.  
    43.     Public Sub New()
    44.  
    45.     End Sub
    46.  
    47.     Public Sub New(ByVal wkNum As Integer, ByVal yrNum As Integer)
    48.         _WeekNumber = wkNum
    49.         _YearNumber = yrNum
    50.     End Sub
    51.  
    52. End Class

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    if i understand what you are trying to do try this code for the class

    Code:
    Public Class returnDates
    
        Dim currentWeek As Integer
        Dim currentYear As Integer
    
        Public Sub New(ByVal tempWeek As Integer, ByVal tempYear As Integer)
            currentWeek = tempWeek
            currentYear = tempYear
        End Sub
    
        Public Function returnCurrentWeek() As Integer
            Return currentWeek
        End Function
    
        Public Function returnCurrentYear() As Integer
            Return currentYear
        End Function
    
        Public Function returnNextWeek() As Integer
            Return currentWeek + 1
        End Function
    
        Public Function returnNextYear() As Integer
            Return currentYear + 1
        End Function
    End Class
    and then use this code to create class and call functions

    Code:
    Dim a As New returnDates(1, 2003)
            Dim b As New returnDates(10, 2010)
    
            Dim temp As Integer
    
            temp = a.returnCurrentWeek
            temp = a.returnCurrentYear
            temp = a.returnNextWeek
            temp = a.returnNextYear
    
            temp = b.returnCurrentWeek
            temp = b.returnCurrentYear
            temp = b.returnNextWeek
            temp = b.returnNextYear

    hope i got it and this helps!

    Nick

  4. #4

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    That is where I started, but I have so many properties that I want to add to the weeks like, start date, end date, weeknumber, year number, that it seems to me that it would make sense to have it grouped together.

    ie:

    oWeek.CurrentWeek.YearNumber
    oWeek.CurrentWeek.WeekNumber
    oWeek.CurrentWeek.StartDate
    oWeek.CurrentWeek.EndDate

    and then that repeted for LastWeek, NextWeek, and LastYear.

    I did figure out how to get it thead safe, it does everything i want it to do except when you referance it oWeek. you get the oWeek.cCurrentWeek and I'd like that to be invisible. If I make the sub class private, it won't let me use the "Public CurrentWeek As cCurrentWeek"


    Code:
    Public Class FiscalWeek
    
      Public CurrentWeek As cCurrentWeek
      Public NextWeek As cNextWeek
      Public LastWeek As cLastWeek
      Public LastYear As cLastYear
    
      Public Sub New(ByVal WeekNumber As Integer, ByVal YearNumber As Integer, ByVal sSQLSP As String)
    
        ...
    
        CurrentWeek = New cCurrentWeek(WeekNumber, YearNumber, cn)
        ...
    
      End Sub 
    
    
    
     Class cCurrentWeek
    
        Private m_Week As Integer
        Private m_Year As Integer
        Private m_Start As Date
        Private m_End As Date
    
        Sub New(ByVal iWeek As Integer, ByVal iYear As Integer, ByVal cn As SqlConnection)
          m_Week = iWeek
          m_Year = iYear
          FindDates(iWeek, iYear, cn)
    
        End Sub
    
        Private Sub FindDates(ByVal iWeek As Integer, ByVal iYear As Integer, ByVal cn As SqlConnection)
    	..
    
        End Sub
    
    
    
        Public ReadOnly Property WeekNumber() As Integer
    
          Get
            Return m_Week
          End Get
    
        End Property
    
        Public ReadOnly Property YearNumber()
    
          Get
            Return m_Year
          End Get
    
        End Property
    
        Public ReadOnly Property StartDate() As Date
    
          Get
            Return m_Start
          End Get
    
        End Property
    
        Public ReadOnly Property EndDate() As Date
    
          Get
            Return m_End
          End Get
    
        End Property
    
      End Class
    End Class
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width