VB redeclare as different type - vb.net

Can I change the variable type by declare it again in the code. Like...
Dim x As New DEV_CLASS
If environment = "UAT" Then
Dim x As New UAT_CLASS
End If
x.something1
x.something2
x.something3

As #TyCobb pointed out, use an interface
Dim x As MyInterface
If environment = "UAT" Then
x = New UAT_CLASS
Else
x = New DEV_CLASS
'DirectCast(x, DEV_CLASS).SomeOtherDevMethod()
End If
x.Method1()
x.Method2()
Class and interface definition:
Public Interface MyInterface
Sub Method1()
Sub Method2()
End Interface
Public Class DEV_CLASS
Implements MyInterface
Public Sub Method1() Implements MyInterface.Method1
End Sub
Public Sub Method2() Implements MyInterface.Method2
End Sub
Public Sub SomeOtherDevMethod()
End Sub
End Class
Public Class UAT_CLASS
Implements MyInterface
Public Sub Method1() Implements MyInterface.Method1
End Sub
Public Sub Method2() Implements MyInterface.Method2
End Sub
End Class

Related

how to dont call contruvtor Inherits in vb

I want to call Dim objFkkiNinteiJokyoRpt As New A(objCsv) in class C. But class A Inherits Common. If now call contructor will An error occurred.Because type Object C.CsvGenerator diference type Object in common. I thnk now stop call to contructor common (or any other way) but i dont know how to do. Helf me please. Sorry because my english so bad
Public Class A Inherits Common
Public _objCsv As C.CsvGenerator
Friend Sub New(ByVal objCsv As C.CsvGenerator)
_objCsv = objCsvGenerator
End Sub
Public Sub New()
End Sub
End Sub
Public Sub New(ByVal objRSReportObj As Object)
MyBase.New(objRSReportObj)
InitializeReport()
End Sub
End Class
Public Class C
Private Function SelectCSV
Dim objCsv As New CsvGenerator("")
Dim objFkkiNinteiJokyoRpt As New A(objCsv)
End Function
Friend Class CsvGenerator
Inherits cmShare.cmObject
End Class
End Class
Public Class Common
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal objRSReport As Object)
MyBase.New(objRSReport)
End Sub
End Class

Call derrived class constructor without arguments from main class constructor with arguments

I have a abstract base class and a derrived class. The base class has some members that I want to set based on the derrived class type. But I also have a common constructor for all derrived classes that accept an argument. That common constructor with an argument is repeating code I would like to get rid of.
Public MustInherit Class B
Protected member1 As String
Protected member2 As String
End Class
Public Class D1 : Inherits B
Public Sub New()
Me.member1 = "D1" ' individual code here
End Sub
Public Sub New(arg As String)
Me.New()
Me.meber2 = arg ' repeating code here, should be moved to base class
End Sub
End Class
Public Class D2 : Inherits B
Public Sub New()
Me.member1 = "D2" ' individual code here
End Sub
Public Sub New(arg As String)
Me.New()
Me.meber2 = arg ' repeating code here, should be moved to base class
End Sub
End Class
How can I refactor this, so that the derrived constructor without arguments is called, even when invoking the constructor with the argument in the base class?
Your code wouldn't even compile since the member are Private. You could have a Protected method that populates the member.
Public MustInherit Class B
Private member1 As String
Private member2 As String
Protected Sub SetMembers(ByVal m1 As String, ByVal m2 As String)
member1 = m1
member2 = m2
End Sub
End Class
Public Class D1 : Inherits B
Public Sub New()
Me.New("")
End Sub
Public Sub New(arg As String)
SetMembers("D1", arg)
End Sub
End Class
Public Class D2 : Inherits B
Public Sub New()
Me.New("")
End Sub
Public Sub New(arg As String)
SetMembers("D2", arg)
End Sub
End Class
You could change SetMembers to a protected New and call it with MyBase.New(..., ...) if you want.
Why not add a constructor to the base class?
Public MustInherit Class B
Public Sub New(arg As String)
Me.member2 = arg
End Sub
Protected member1 As String
Protected member2 As String
End Class
Public Class D1 : Inherits B
Public Sub New(arg As String)
MyBase.New(arg)
Me.member1 = "D1"
End Sub
End Class
Public Class D2 : Inherits B
Public Sub New(arg As String)
MyBase.New(arg)
Me.member1 = "D2"
End Sub
End Class
It doesn't really reduce the duplication, per se, since each derived class still has the line that calls the base constructor, passing it the value, but it does allow you to centrally maintain what it does with that value, and it does force all derived classes to do it. That way you can never create a derived class where you forget to provide that value.
In fact, if you want to require all derived classes to provide a default value for member1, then you could add that as a parameter to the base constructor as well:
Public MustInherit Class B
Public Sub New(member1 As String, member2 As String)
Me.member1 = member1
Me.member2 = member2
End Sub
Private member1 As String
Private member2 As String
End Class
Public Class D1 : Inherits B
Public Sub New(arg As String)
MyBase.New("D1", arg)
End Sub
End Class
Public Class D2 : Inherits B
Public Sub New(arg As String)
MyBase.New("D2", arg)
End Sub
End Class

Returning the self type in Mustinherit class - VB.NET

I have an abstract class as
Public MustInherit Class GenericClass
Public Sub New(Byval x as Integer)
' Some code here
End Sub
End Class
I inherit this class to another class as follows:
Public Class SpecificClass
Inherits GenericClass
Public Sub New(Byval x as Integer)
MyBase.New(x)
End Sub
End Class
I want to add a Shared Function e.g. magicFunction in such a way that when I use it, it should return an object of type SpecificClass. What should I do?
I want something like this but it is not allowed in VB.NET
Public MustInherit Class GenericClass
Public Sub New(Byval x as Integer)
' Some code here
End Sub
Public Shared Function magicFunction(Byval y as Integer) as GenericClass
Dim z as Integer
' Some code here that will alter the value of z
Return New GenericClass(z) ' Not allowed in VB.NET -- MustInherit class cannot have new
End Sub
End Class
Calling the magicFunction of the inheriting SpecificClass should return an object of SpecificClass like this:
Public Class ABC
Public Function myAwesomeFunction as SpecificClass
Dim objSpecificClass as SpecificClass
objSpecificClass = SpecificClass.magicFunction(someInteger)
Return objSpecificClass
End Sub
End Class
Any help would be appreciated
This may help:
Public MustInherit Class GenericClass(Of T As {GenericClass(Of T)})
Public Sub New(ByVal x As Integer)
' Some code here
End Sub
Public Shared Function magicFunction(ByVal y As Integer) As GenericClass(Of T)
Dim z As Integer
' Some code here that will alter the value of z
Return Activator.CreateInstance(GetType(T), z)
End Function
End Class
Public Class SpecificClass1
Inherits GenericClass(Of SpecificClass1)
Public Sub New(ByVal x As Integer)
MyBase.New(x)
End Sub
End Class
Public Class SpecificClass2
Inherits GenericClass(Of SpecificClass2)
Public Sub New(ByVal x As Integer)
MyBase.New(x)
End Sub
End Class
Usage:
Dim a As SpecificClass1 = SpecificClass1.magicFunction(1)
Dim b As SpecificClass2 = SpecificClass2.magicFunction(2)

VB generics with constraints -- type casting and inheritance?

Take this scenario:
Public Interface IMyClass
End Interface
Public mustinherit class MyBaseClass : implements IMyClass
End Class
public class MyClass : inherits MyBaseClass
End Class
public class MyModel(of t as IMyClass)
private Dim _parameter as t
Public Sub New(byval parameter As t)
_parameter As t
End Sub
End class
In my controller, I can do this with no problem:
Dim _myclass as IMyClass = new MyClass()
Can I do something similar with this:
Dim _myModel as MyModel(of IMyClass) = new MyModel(of MyClass)
???
My initial thought was wrong, as I thought the conversion could be done automatically, but it appears it is not done. Any way to achieve the same thing within .NET?
EDIT
I updated the MyModel class to show more of what I was doing. I want to constrain the instance I create, but then do what would be a narrowing conversion with traditional, non-generics code. Basically, my partial Razor views would require the explicit model, and those views end up rendering another view that will take that model and display it. Because the models all implement or inherit a class that implements IMyClass, all the methods should exist on all of the instances and should be callable but the types are not interchangable.
Let’s modify MyModel slightly, shall we?
Public Class MyModel(Of T As IMyClass)
Private _parameter As T
Public Sub Something(parameter As T)
_parameter = parameter
End Sub
End class
Public Class MyClassA : Inherits MyBaseClass
End Class
Public Class MyClassB : Inherits MyBaseClass
End Class
Dim _myModel As MyModel(Of IMyClass) = New MyModel(Of MyClassA)()
_myModel.Something(New MyClassB()) ' Boom!
If the assignment were allowed the last line would pose a problem: MyMode(Of MyClassA)._parameter has type MyClassA but the last line would assign an object of the (unrelated) type MyClassB. This is illegal and so VB forbids it.
Do you need multiple varieties of MyModel, or are you just attempting to require that the stored object be constrained to IMyClass?
Simplest approach (that might not do everything you need):
Public Interface IMyClass
Sub DoIt()
End Interface
Public Class MyModel
Private ReadOnly _parameter As IMyClass
Public Sub New(parameter As IMyClass)
_parameter = parameter
End Sub
Public Sub DoItToIt()
_parameter.DoIt()
End Sub
End Class
Public Class MyClassA
Implements IMyClass
Public Sub DoIt() Implements IMyClass.DoIt
End Sub
End Class
Public Class Tests
Public Sub Main()
Dim model1 As MyModel = New MyModel(New MyClassA)
model1.DoItToIt()
End Sub
End Class
Next step up in complexity is to define an interface IHasMyClass for classes that contain an IMyClass. This supports manipulations based on the allowed type, and the actual type, of the contained object:
Public Interface IMyClass
Sub DoIt()
End Interface
Public Interface IHasMyClass
Function GetIt() As IMyClass
Function GetItsType() As Type
Function GetAllowedType() As Type
End Interface
Public Class MyModel(Of T As IMyClass)
Implements IHasMyClass
Private ReadOnly _parameter As IMyClass
Public Sub New(parameter As IMyClass)
_parameter = parameter
End Sub
Public Sub DoItToIt()
_parameter.DoIt()
End Sub
Public Function GetItAsT() As T
Return _parameter
End Function
Public Function GetIt() As IMyClass Implements IHasMyClass.GetIt
Return _parameter
End Function
Public Function GetItsType() As Type Implements IHasMyClass.GetItsType
Return _parameter.GetType()
End Function
Public Function GetAllowedType() As Type Implements IHasMyClass.GetAllowedType
Return GetType(T)
End Function
End Class
Public Class MyClassA
Implements IMyClass
Public Sub DoIt() Implements IMyClass.DoIt
End Sub
End Class
Public Class Tests
Public Sub Main()
' Allow any IMyClass
Dim model1 As MyModel(Of IMyClass) = New MyModel(Of IMyClass)(New MyClassA)
model1.DoItToIt()
Dim it As IMyClass = model1.GetIt()
Dim allowedT As Type = model1.GetAllowedType()
' Restrict to MyClassA
Dim modelA As MyModel(Of MyClassA) = New MyModel(Of MyClassA)(New MyClassA)
modelA.DoItToIt()
Dim itA1 As IMyClass = modelA.GetIt()
Dim itA2 As MyClassA = modelA.GetItAsT()
Dim allowedTA As Type = modelA.GetAllowedType()
End Sub
End Class
In Tests(), notice that we now need to declare whether we are creating a MyModel that accepts ANY IMyClass MyModel(Of IMyClass), or one that requires a specific sub-class MyModel(Of MyClassA).
If we want to manipulate MyModels, that may be either of the above types, we use the common interface:
Dim model As IHasMyClass
model = model1
...
model = modelA
Or in your case, to support all the functionality of MyModel, rename IHasMyClass as IMyModel, and add the various MyModel functions, but instead of T, use IMyClass:
Public Interface IMyModel
Function GetIt() As IMyClass
Function GetItsType() As Type
Function GetAllowedType() As Type
Sub DoItToIt()
Function CompareIt(other As IMyClass) As Integer
End Interface
And make appropriate changes/additions to IMyClass and MyModel.
Then it becomes possible to do:
Dim model As IMyModel = modelA
If model.CompareIt(model1.GetIt()) > 0 ...

Organizing VB.Net Mehods

Say I have a class with several methods within it. I want to organize the methods into groupings that can be accessed without constructing a new object each time. The purpose is to group the methods of the class into logical buckets
For instance:
Dim myclass as MyCustomClass
myclass.Shipping.Get_List()
myclass.Production.Get_List()
What is the best way to do this? I tried nested classes, but VB.NET won't let me access the methods as shown above.
so this is how i would do what you want
this is not the best design of the world but it would work
I would suggest you to move the actual get_list and other kind of method / property into the specific class while keeping the common one in the parent class, which in this case is test
but then, I have no idea what your code look like so from that point on, it's your choice
Module Module1
Sub Main()
Dim test As New test
test.Production.Get_List()
test.Shipping.Get_List()
End Sub
End Module
Public Class Shipping
Private parent As test
Public Sub New(ByRef parent As test)
Me.parent = parent
End Sub
Public Function Get_List() As List(Of Integer)
Return parent.GetShipping_List
End Function
End Class
Public Class Production
Private parent As test
Public Sub New(ByRef parent As test)
Me.parent = parent
End Sub
Public Function Get_List() As List(Of Integer)
Return parent.GetProduction_List
End Function
End Class
Public Class test
Public Property Production As Production
Public Property Shipping As Shipping
Public Function GetShipping_List() As List(Of Integer)
Return Nothing
End Function
Public Function GetProduction_List() As List(Of Integer)
Return Nothing
End Function
Public Sub New()
Production = New Production(Me)
Shipping = New Shipping(Me)
End Sub
End Class
With caution that you more than likely should re-evaluate your architecture, you could implement your pattern like this:
Public Class MyCustomClass
Private _shippingList As List(Of String)
Private _productionList As List(Of String)
Public Production As ProductionClass
Public Shipping As ShippingClass
Public Sub New()
Production = New ProductionClass(Me)
Shipping = New ShippingClass(Me)
End Sub
Public Class ShippingClass
Private _owner As MyCustomClass
Public Sub New(owner As MyCustomClass)
_owner = owner
End Sub
Public Function Get_List()
Return _owner._productionList
End Function
End Class
Public Class ProductionClass
Private _owner As MyCustomClass
Public Sub New(owner As MyCustomClass)
_owner = owner
End Sub
Public Function Get_List()
Return _owner._productionList
End Function
End Class
End Class
However, if your true intent is simply organizing the methods in a more accessible and logical manner, I would suggest considering:
Public Class MyCustomClass
Public Sub ShippingListGet()
End Sub
Public Sub ShippingListAddTo()
End Sub
Public Sub ShippingThatDO()
End Sub
Public Sub ShippingThisDo()
End Sub
Public Sub ProductionListGet()
End Sub
Public Sub ProductionListAddTo()
End Sub
Public Sub ProductionThisDo()
End Sub
Public Sub ProductionThatDo()
End Sub
End Class
Keep in mind, some people consider that difficult to read. I personally prefer organization along those lines so when the methods are sorted alphabetically they group logically.
I have found the solution I was looking for using interfaces
Public Interface ICompany
Function Company_List() As DataTable
End Interface
Public Class MainClass
Public Company As ICompany = New CompanyClass
Public Sub New()
MyBase.New()
End Sub
Private Class CompanyClass
Public Sub New()
MyBase.New()
End Sub
Public Function Company_List() As DataTable
My code....
End Function
End Class
End Class