Solved

Shared Private Public etc in a Visual Basic Class

Posted on 2008-06-18
3
625 Views
Last Modified: 2010-05-18
Hi
I'm modifying some code and haven't used classes too much so please bare with me :)

In this class I see for example that in the region "Variables" these are Private.
1) Is that ok? Are they also called 'fields'?

2) the Properties are Public. Is that ok?

3) Some Functions are Private Shared, Public Shared or Public.

Shouldn't they ALL just be either  Private Shared, Public Shared or, Public.

4) Is Shared the same as Static in C#?

Thanks for the input
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Text
Imports CMT.DataAccessLayer
 
 
Namespace BusinessLogicLayer
 
    Public Class KnowledgeBaseArticle
        Inherits content
#Region "Variables"
        Private _knowledgeBaseArticleid As Integer
        Private _category As Integer
 
#End Region
#Region "Constructors"
        Public Sub New()
        End Sub 'New
 
        Public Sub New(ByVal KnowledgeBaseArticleID As Integer)
            _knowledgeBaseArticleid = KnowledgeBaseArticleID
        End Sub 'New
#End Region
 
#Region "Public Propertys"
        Public Property KnowledgeBaseArticleID() As Integer
            Get
                Return _knowledgeBaseArticleid
            End Get
            Set(ByVal Value As Integer)
                _knowledgeBaseArticleid = Value
            End Set
        End Property
 
        Public Property Category() As Integer
            Get
                Return _category
            End Get
            Set(ByVal value As Integer)
                _category = value
            End Set
        End Property
 
        Public ReadOnly Property MainImage() As ReferenceFile
            Get
                For Each objImage As ReferenceFile In Me.Images
                    If objImage.Primary Then
                        Return objImage
 
                    End If
                Next
 
                If Me.Images.Count > 0 Then
                    Return Me.Images(0)
 
                End If
 
                Return Nothing
            End Get
        End Property
 
 
#End Region
#Region "Functions"
 
        Private Shared Function CreateList(ByVal ds As DataSet)
            Try
                Dim objKnowledgeBaseArticles As New List(Of KnowledgeBaseArticle)
                Dim r As DataRow
                For Each r In ds.Tables(0).Rows
                    objKnowledgeBaseArticles.Add(ItemForDataRow(r))
                Next r
                Return objKnowledgeBaseArticles
            Catch ex As Exception
                Throw
            End Try
        End Function
 
        Private Shared Function ItemForDataRow(ByVal r As DataRow) As KnowledgeBaseArticle
            Dim objKnowledgeBaseArticle As New KnowledgeBaseArticle
 
            '' Required properties - throw failure if any of these are null
 
 
            objKnowledgeBaseArticle.KnowledgeBaseArticleID = Convert.ToInt32(r("knowledgeBaseArticle_rid"))
 
            objKnowledgeBaseArticle.ContentSummary = Convert.ToString(r("content_summary"))
            objKnowledgeBaseArticle.ContentTitle = Convert.ToString(r("content_title"))
            objKnowledgeBaseArticle.PublishDate = Convert.ToDateTime(r("content_publish_date"))
            objKnowledgeBaseArticle.CommitedFlag = Convert.ToBoolean(r("commited_flag"))
            objKnowledgeBaseArticle.Category = Convert.ToString(r("knowledgeBaseArticle_category"))
 
            Dim objfiles As New ReferenceFile
            'objKnowledgeBaseArticle.Images = objfiles.GetFilesByRef(Convert.ToInt32(r("content_rid")), "CONT", ReferenceFile.FileTypes.Image)
            objKnowledgeBaseArticle.Documents = objfiles.GetFilesByRef(Convert.ToInt32(r("content_rid")), "CONT", ReferenceFile.FileTypes.Document)
 
 
 
            Try
 
            Catch ex As Exception
 
            End Try
 
            Return objKnowledgeBaseArticle
 
        End Function
 
        Public Overloads Shared Function GetKnowledgeBaseArticles() As List(Of KnowledgeBaseArticle)
            Try
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_GetAllKnowledgeBaseArticles")
                Return CreateList(ds)
            Catch ex As Exception
                Throw
            End Try
        End Function
 
        Public Overloads Shared Function SearchKnowledgeBaseArticles(ByRef myKeywords As String, ByRef myCategory As Integer, ByRef myDate As String) As List(Of KnowledgeBaseArticle)
            Try
                Dim ds As DataSet
                Dim myPublishDate As DateTime
 
                If Not myDate Is Nothing AndAlso myDate.Length > 0 AndAlso DateTime.TryParse(myDate, myPublishDate) Then
                    myPublishDate = FormatDateTime(myPublishDate, DateFormat.ShortDate)
 
                    ds = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_Search", myKeywords, myCategory, myPublishDate)
                Else
                    ds = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_Search", myKeywords, myCategory, Nothing)
                End If
                Return CreateList(ds)
            Catch ex As Exception
                Throw
            End Try
        End Function
 
 
        Public Overloads Shared Function GetUnExpiredKnowledgeBaseArticles() As List(Of KnowledgeBaseArticle)
            Try
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_GetAllUnexpiredKnowledgeBaseArticles")
                Return CreateList(ds)
            Catch ex As Exception
                Throw
            End Try
        End Function
        Public Overloads Shared Function GetExpiredKnowledgeBaseArticles() As List(Of KnowledgeBaseArticle)
            Try
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_GetAllExpiredKnowledgeBaseArticles")
                Return CreateList(ds)
            Catch ex As Exception
                Throw
            End Try
        End Function
        'Public Shared Function GetTopXNewsArticles(ByVal _topx As Integer) As List(Of NewsArticles)
        '    Try
 
        '        Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_News_GetTopXArticles", _topx)
        '        Dim objNewsArticles As New List(Of NewsArticles)
        '        Dim r As DataRow
        '        For Each r In ds.Tables(0).Rows
        '            Dim objNews As New NewsArticles
        '            objNews.NewsID = Convert.ToInt32(r("news_rid"))
        '            objNews.ContentTitle = Convert.ToString(r("content_title"))
        '            objNews.ContentSummary = Convert.ToString(r("content_summary"))
        '            objNews.CommitedFlag = Convert.ToBoolean(r("commited_flag"))
        '            objNews.Destination = Convert.ToInt32(r("destination"))
 
        '            objNewsArticles.Add(objNews)
        '        Next r
        '        Return objNewsArticles
 
        '    Catch ex As Exception
        '        Throw
        '    End Try
        'End Function
 
        Public Shared Function GetKnowledgeBaseArticle(ByVal id As Integer) As List(Of KnowledgeBaseArticle)
            Try
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_GetKnowledgeBaseArticle", id)
                Return CreateList(ds)
            Catch ex As Exception
                Throw
            End Try
        End Function '
 
        Public Shared Function GetKnowledgeBaseArticleForFE(ByVal id As Integer) As List(Of KnowledgeBaseArticle)
            Try
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_GetKnowledgeBaseArticleForFE", id)
                Return CreateList(ds)
            Catch ex As Exception
                Throw
            End Try
        End Function '
 
        Public Function Load() As Boolean
            Try
 
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_GetKnowledgeBaseArticle", _knowledgeBaseArticleid)
                If ds.Tables(0).Rows.Count < 1 Then
                    Return False
                End If
                KnowledgeBaseArticleID = Convert.ToInt32(ds.Tables(0).Rows(0)("knowledgeBaseArticle_rid"))
                ContentID = Convert.ToInt32(ds.Tables(0).Rows(0)("content_rid"))
                ContentTitle = Convert.ToString(ds.Tables(0).Rows(0)("content_title"))
                ContentSummary = Convert.ToString(ds.Tables(0).Rows(0)("content_summary"))
                ContentFullText = Convert.ToString(ds.Tables(0).Rows(0)("content_text"))
                PublishDate = Convert.ToDateTime(ds.Tables(0).Rows(0)("content_publish_date"))
 
                CommitedFlag = Convert.ToBoolean(ds.Tables(0).Rows(0)("commited_flag"))
                Category = Convert.ToString(ds.Tables(0).Rows(0)("knowledgeBaseArticle_category"))
 
 
                Dim objfiles As New ReferenceFile
                'Images = objfiles.GetFilesByRef(ContentID, "CONT", ReferenceFile.FileTypes.Image)
                Documents = objfiles.GetFilesByRef(ContentID, "CONT", ReferenceFile.FileTypes.Document)
 
 
                Return True
            Catch ex As Exception
                Throw
            End Try
        End Function 'Load
 
 
 
 
        Public Sub DeleteKnowledgeBaseArticle(ByVal knowledgeBaseArticleID As Integer, ByVal Username As String)
            Try
                Dim objKnowledgeBaseArticle As New KnowledgeBaseArticle(knowledgeBaseArticleID)
                objKnowledgeBaseArticle.Load()
                MyBase.Delete(objKnowledgeBaseArticle.ContentID, Username)
                SqlHelper.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_Delete", knowledgeBaseArticleID, Username)
            Catch ex As Exception
                Throw
            End Try
 
        End Sub 'Remove
 
        Public Overloads Function Save(ByVal UserName As String) As Boolean
            If _knowledgeBaseArticleid = 0 Then
                Return Insert(UserName)
            Else
                If _knowledgeBaseArticleid > 0 Then
                    Return Update(UserName)
                Else
                    _knowledgeBaseArticleid = 0
                    Return False
                End If
            End If
        End Function 'Save
 
 
        Private Function Insert(ByVal UserName As String) As Boolean
 
            Try
                Dim contentid As Integer
                contentid = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_Content_Add", ContentTitle, ContentSummary, ContentFullText, PublishDate, DBNull.Value, Me.CommitedFlag, UserName, 3))
                _knowledgeBaseArticleid = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_Add", contentid, UserName, Category))
                Dim tmpFile As ReferenceFile
                Dim tmpColl As New Referencefilecollection
                'If Not IsNothing(Images) Then
                '    'if Images.Count > 0 Then
                '    For Each tmpFile In Images
                '        tmpColl.Add(tmpFile)
                '    Next
                'End If
                If Not IsNothing(Documents) Then
                    For Each tmpFile In Documents
                        tmpColl.Add(tmpFile)
                    Next
                End If
                Dim objSaveFiles As New ReferenceFile
                objSaveFiles.SaveCollection(tmpColl, contentid, 1, UserName)
                Return _knowledgeBaseArticleid > 0
            Catch ex As Exception
                Throw
            End Try
        End Function 'Insert
        Private Function Update(ByVal UserName As String) As Boolean
 
            Try
 
                SqlHelper.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_Content_Update", ContentID, ContentTitle, ContentSummary, ContentFullText, PublishDate, DBNull.Value, CommitedFlag, UserName, 3)
                SqlHelper.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_KnowledgeBaseArticles_Update", _knowledgeBaseArticleid, UserName, _category)
                Dim tmpFile As ReferenceFile
                Dim tmpColl As New Referencefilecollection
                'If Not IsNothing(Images) Then
                '    'if Images.Count > 0 Then
                '    For Each tmpFile In Images
                '        tmpColl.Add(tmpFile)
                '    Next
                'End If
                If Not IsNothing(Documents) Then
                    For Each tmpFile In Documents
                        tmpColl.Add(tmpFile)
                    Next
                End If
                Dim objSaveFiles As New ReferenceFile
                objSaveFiles.SaveCollection(tmpColl, ContentID, 1, UserName)
                Return _knowledgeBaseArticleid > 0
            Catch ex As Exception
                Throw
            End Try
 
        End Function 'Update
#End Region
 
        Public Shared Function GetCategories() As DataSet
            Try
                Dim ds As DataSet = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_Categories_GetAllCategories")
                Return ds
            Catch ex As Exception
                Throw
            End Try
        End Function '
 
        Public Shared Function GetCategoryName(ByRef categoryID As Integer) As String
            Try
                Dim strName As String = SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_Categories_GetCategoryName", categoryID)
                Return strName
            Catch ex As Exception
                Throw
            End Try
        End Function
 
    End Class
 
 
 
End Namespace

Open in new window

0
Comment
Question by:amoran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 20 total points
ID: 21812058
Hi

>>In this class I see for example that in the region "Variables" these are Private.
>>1) Is that ok? Are they also called 'fields'?
Yes thats ok, it's best to keep your member variables private to make sure only the classes code can directly manipulate them (preserving encapsulation), these are also known as fields

>>2) the Properties are Public. Is that ok?
Again that's okay, the properties act as a means for getting/setting the value of a variable without direct access, this lets you for example add extra code to validate assignment (in the set block)

>>3) Some Functions are Private Shared, Public Shared or Public.
>>Shouldn't they ALL just be either  Private Shared, Public Shared or, Public.
You only make those functions public that you are allowing external code to access, those that are to be used internally only you should mark as private, i generally make helper methods (ones that do a bit of checking or intermdiate processing). Non shared functions can act on your member variable so if you have three instances of the same class, each one can have an entirely different state. Shared functions can only act on Shared (static) member variables, they can not change the state of the instance directly - you'll get a compile error if you try.

>>4) Is Shared the same as Static in C#?
Yes ! :)
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question