I've created a request class. Here is an abbreviated version of it:
Public Class Request(Of T)
Private _Account As String
Public Property Account() As String
Get
Return _Account
End Get
Set(ByVal value As String)
_Account = value
End Set
End Property
Private _InnerRequest As T
Public Property InnerRequest() As T
Get
Return Me._InnerRequest
End Get
Set(ByVal value As T)
Me._InnerRequest = value
End Set
End Property
End Class
And then I have two other classes that I intend to use with this one - again, abbreviated
Public Class Individual
Public FirstName As String
Public LastName As String
Friend Sub New()
End Sub
End Class
And
Public Class Commercial
Public EntityName As String
Friend Sub New()
End Sub
End Class
Again, both of these are pretty abbreviated. The issue comes in when I attempt to use the properties of individual or commercial:
Dim Req As New Request(Of Individual)()
Req.InnerRequest.FirstName = "Herman" <-- Null Ref Exception
So... how do I get my inner request null ref exception kicked? I tried simply using Me._InnerRequest = New T in the New sub of Request, but no dice. Is there a way to handle this?
Req.InnerRequest must be set to an object instance of Individual first.
Req.InnerRequest = new Individual()
Req.InnerRequest.FirstName = "Herman"
Or create an instance for InnerRequest with the following modifications
Public Class Request(Of T As {New}) 'Classes of type T must have a public new constructor defined
::
Private _InnerRequest As New T() 'Creates a new class of type T when an instance is created of Request
And make the constructors of the other classes Public instead of Friend.
Than you can directly do
Dim Req As New Request(Of Individual)()
Req.InnerRequest.FirstName = "Herman"
#Barry already answered what the main problem is, but here's an alternate syntax if you prefer object initializers:
Req.InnerRequest = new Individual() With { FirstName = "Herman" }
Or, if you prefer, you could overload the constructor for your Individual class:
Dim individual As New Individual("Herman")
Req.InnerRequest = individual
With the Individual class looking like:
Public Class Individual
Public FirstName As String
Public LastName As String
Friend Sub New()
End Sub
Friend Sub New(firstName As String)
Me.FirstName = firstName
End Sub
End Class
You probably should consider restricting the T to some Entity class:
Public Class Request(Of T As Entity)
From which both Individual and Commercial will inherit:
Public Class Individual : Inherits Entity
Then maybe declare an overridable property Name of type String on this Entity class (which can be abstract/MustInherit), this should provide some flexibility. Otherwise you'd be having a hard time consuming your design pattern.
Related
I have: ClassUsser, ClassClient, ClassPerson.
ClassUsser
Public Property Name as String
ClassClient
Public Property Name as String
ClassPerson
Public Property KindPerson as Integer
Public Property Person as (ClassClient or ClassPerson)
I want delare Property Person as diferent type of class based on value of KindPerson.
Enum KindsPerson
{ User=1,
Client=2}
First, I would set value of KindPerson, and after, through set/get methods, obtain the Person
I don't Know If It si posible in VB.Net. It exists any way to do this?
Thanks
You could use the Factory Pattern:
Module Module1
Sub Main()
Dim p1 = Person.GetPerson(Person.PersonKind_ENUM.Client, "Adam")
Dim p2 = Person.GetPerson(Person.PersonKind_ENUM.User, "Barbara")
Dim personList As New List(Of Person)
personList.Add(p1)
personList.Add(p2)
For Each p In personList
Console.WriteLine($"{p.GetType.Name}: {p.name}")
Next
Console.ReadKey()
End Sub
Public Class User
Inherits Person
End Class
Public Class Client
Inherits Person
End Class
Public MustInherit Class Person
Property Name As String
Public Enum PersonKind_ENUM
User
Client
End Enum
Public Shared Function GetPerson(personKind As PersonKind_ENUM, name As String) As Person
Select Case personKind
Case PersonKind_ENUM.Client
Return New Client() With {.Name = name}
Case PersonKind_ENUM.User
Return New User() With {.Name = name}
Case Else
Throw New InvalidOperationException
End Select
End Function
End Class
End Module
So this is how I would design my Lazy class (From this SO):
Public NotInheritable Class MySingleton
Private Shared ReadOnly _instance As New Lazy(Of MySingleton)(Function() New _
MySingleton(), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance() As MySingleton
Get
Return _instance.Value
End Get
End Property
Private _MyString As String
Public Property MyString As String
Get
Return _MyString
End Get
Set(value As String)
_MyString = value
End Set
End Property
End Class
To access the _MyString value, I do the following:
Dim MyString = MySingleton.Instance.MyString
In fact, I always have to type the ".Instance."
Does it have any disadvantage if I design the Property the following way:
Public Property MyString As String
Get
Return instance._MyString
End Get
Set(value As String)
instance._MyString = value
End Set
End Property
So I can access it without always writing the ".Instance."
Dim MyString = MySingleton.MyString
Yes you can (of course with Public Shared Property), but you are losing some of the benefits from singleton over static classes.
Lets say you have another class MyWorker
Public Class MyWorker
Public Sub Work(instance as MySingleton)
Dim value as String = instance.MyString
' Do something ...
End Sub
End Class
I would not do this. This may not look like a big issue, but on the long run you have a tight coupling in your code base and a hard time mocking your class for unit testing, one of the reasons for using singeltons over static classes in the first place.
I often use this approach, when accessing Singelton values mutiple times:
Dim instance as MySingelton = MySingelton.Value
If instance.MyString = "something" Then
instance.MyString = "something else"
End If
much cleaner approach.
I cant seem to get my HTML.DropDownListFor() to work properly with my structures.
CategoriesEditViewModel
Public Class CategoriesEditViewModel
Public Property Categories As List(Of cihCategoryOrgDef)
Public Property Category As cihCategoryOrgDef
Public Property lists As cihLists = New cihLists()
Public Property SelectedItem As String
Public Sub New(catId as Guid)
SelectedItem = codId.ToString
lists.loadClubWaivers(ZenCommon.CurrentOrgId)
End Sub
End Class
cihLists
Private orgWaiverList As List(of cihWaiver)
Public Sub loadClubWaivers(orgId as Guid)
'Go to database and populate orgWaiverList
End Sub
Public ReadOnly Property organizationClubWaivers() As List(Of cihWaiver)
Get
Return orgWaiverList
End Get
End Property
cihWaiver
Public Class cihWaiver
Public waiverId As Guid = Guid.Empty
Public waiverName As String = ""
Public waiverText As String = ""
End Class
Edit View Page
#Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverText", Model.lists.organizationClubWaivers))
The error i get is 'Library.cihWaiver' does not contain a property with the name 'waiverId'.
but the cihWaiver class clearly has an item for 'waiverId'. I haven't done MVC stuff in a while, so maybe i'm going about this all wrong.
The answer was as simple as changing my cihWaiver Class to this:
Public Class cihWaiver
Public Property waiverId As Guid = Guid.Empty
Public Property waiverName As String = ""
Public waiverText As String = ""
End Class
cane someone explain to me why the keyword Property is so important?
Let's say I have the following class structure (simplified from my real-world problem):
Public Class PC_People_Container
Private _people_list As New List(Of PL_Person)
Public Sub New()
End Sub
Public Sub Add(ByVal item As PL_Person)
_people_list.Add(item)
End Sub
Public Property PeopleList As List(Of PL_Person)
Get
Return _people_list
End Get
Set(ByVal value As List(Of PL_Person))
_people_list = value
End Set
End Property
End Class
Public Class PL_Person
Private _Name As String
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Contacts As ContactObject
Public Property Contacts As ContactObject
Get
Return _Contacts
End Get
Set(ByVal value As ContactObject)
_Contacts = value
End Set
End Property
Public Sub New()
End Sub
End Class
Public Class ContactObject
Public Property PhoneNumber As String
Public Property EmailAddress As String
Public Sub New()
End Sub
End Class
If I were to serialize this, I'd get the default assigned node names in my XML. That means my root is named PC_People_Container and each person in the list is marked up as PL_Person. I know I can change the root node using <XmlRoot(ElementName:="PeopleContainer")>. The trouble is doing that for the subclasses. I can't use the <XmlRoot> tag on PL_Person class because there can't be two root elements, and IntelliSense throws a fit when I try to use the <XmlElement> tag on a class like I would on a property. Is it even possible to control what those subclasses are named when they're serialized as child nodes?
PL_Person and ContactObject are not subclasses as you call them, they are merely property types.
This makes your question confusing because it suggests you may have a problem with inheritance (subclasses are classes that inherit from some base class) when in fact you just want your property elements to be named differently.
You should decorate your properties (not classes) with <XmlElement> to specify custom name:
<XmlElement("Persons", GetType(PL_Person))>
Public Property PeopleList As List(Of PL_Person)
As an afterthought, I would definitely not recommend calling your classes using such an awkward convention. In .NET, you should not use any prefixes or underscores in class names. Just call it Person.
Maybe I just don't know what to search for, but I'm going a little bonkers here trying to figure out how to create a collection of inherited classes. The base class, I will never use.
Basically, I have 3 components:
A base class call ImageFormat
Child classes of ImageForm
Code in Sub Main() to loop create a
collection and loop through it.
So it does it, #3. The problem is that it always gets the last item added to the collection and uses it's values only.
Here's my Base Class:
Public MustInherit Class ImageFormat
Protected Shared _extentions As String()
Protected Shared _targettype As String
Protected Shared _name As String
Public ReadOnly Property Extentions As String()
Get
Return _extentions
End Get
End Property
Public ReadOnly Property TargetType As String
Get
Return _targettype
End Get
End Property
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
End Class
And here are the child classes:
Class WindowsEnhancedMetafile
Inherits ImageFormat
Sub New()
_extentions = {"EMF"}
_targettype = "jpg"
_name = "Windows Enhanced Metafile"
End Sub
End Class
Class WindowsBitmap
Inherits ImageFormat
Sub New()
_extentions = {"BMP", "DIB", "RLE", "BMZ"}
_targettype = "jpg"
_name = "Windows Bitmap"
End Sub
End Class
Class WindowsMetafile
Inherits ImageFormat
Sub New()
_extentions = {"WMF"}
_targettype = "jpg"
_name = "Windows Metafile"
End Sub
End Class
(don't know if these child classes need to different, like just instantied from ImageFormat type or Singleton patterns - would appreciate anything thoughts you have on this)
Then, my routine is:
Sub Main()
Dim imgFormats As New List(Of ImageFormat)
imgFormats.Add(New WindowsBitmap)
imgFormats.Add(New WindowsMetafile)
imgFormats.Add(New WindowsEnhancedMetafile)
Dim name As String = String.Empty
For Each imgFormat In imgFormats
name = imgFormat.Name
Console.WriteLine(name)
Next
Console.ReadLine()
End Sub
This returns Windows Enhanced Metafile three times at the Console. What am I doing wrong here?
The three properties:
Protected Shared _extentions As String()
Protected Shared _targettype As String
Protected Shared _name As String
Are marked as Shared - they belong to the class not the object.
Each time you assign a new value to _name it overrides the old value, thus why you get the same name printed each time.
It should be:
Protected _extentions As String()
Protected _targettype As String
Protected _name As String
Well, your _name et al are Shared, which means they are class-level variables. When you're adding WindowsEnhancedMetafile, it happens to overwrite these fields with WMF-specific information. If you changed your code to:
imgFormats.Add(New WindowsMetafile)
imgFormats.Add(New WindowsEnhancedMetafile)
imgFormats.Add(New WindowsBitmap)
you would've had "Windows Bitmap" printed three times.
All you have to do is to change your field declarations to
Protected _extentions As String()
Protected _targettype As String
Protected _name As String