[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Creating a variable that is the same type as another variable.

How can I create a variable that is the same type of another variable

Dim x as Integer = 1

Dim y as System.Type.gettype(x) = 2

VB.Net 2005

3 Solutions
Variable typing is done at compilation.... The only way around it is to use a late bound variable type, like Object.  

Dim x As Integer = 1
Dim y As Object = x

Dim p As String = "Hello"
Dim q As Object = p
DigitalDan3Author Commented:
There is no way through reflection or generics to accomplish this.

>There is no way through reflection or generics to accomplish this.

Because we're talking about variable typing, no there isn't.  Generics are early bound.  Reflection can tell you information about your type, but can't be used to alter a variable type.  Using a type like Object is the only way to change your variable type at runtime.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Mike TomlinsonMiddle School Assistant TeacherCommented:
What is the end goal here?...

~HOW~ are you going to utilize this "feature" if it could be done?
If you need it to be flexible, you can declare them both as Objects, and then just test for the expected type(s).

As an example:

        Dim x As Object
        Dim y As Object

        x = 5
        y = 10

        'You could test for Int16, Int32, String, or whatever
        '    If x.GetType.Equals(GetType(System.Int32)) = True Then...
        '    Or test them against each other...
        If x.GetType.Equals(y.GetType) = True Then

            MsgBox("Value of x: " & x & vbCrLf & _
                    "Value of y: " & y & vbCrLf & _
                    "perform some math: " & (x + y))

        End If
Looking at your previous question, I'm wondering if what you really want is inheritance or an interface.

The reason you can assign an integer or a string to object is because they both inherit from that type.  But that type is too generic to be very useful.  

Inheritance lets you create new types that are based on another type and override certain members to change their behaviour.  An interface defines an object's signature, so that you can create multiple classes with the same methods and properties and have different behaviour at run time.  An interface sounds closest to what you are talking about, because it is typically used in a horizontal rather than vertical relationship between class types.  

For example, I have a reporting application that implements different types of reports.  Each report has it's own set of classes that implement a common interface.  

Public Class ReportData1
    Implements IReportData
End Class

Public Class ReportData2
    Implements IReportData
End Class

I can load them into a variable typed IReportData, based on a classname:

Private Function GetReportFromAssembly(ByVal ReportAssembly As String, ByVal ReportClassName As String, ByVal RepAssemblyPath As String) As ReportBase.IReportData
        Dim oCurrentAssembly As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
        Dim oTemplateAssembly As [Assembly]
        Dim oReport As Object
        Dim sTemplateNameAndPath As String = System.IO.Path.Combine(RepAssemblyPath, ReportAssembly)
        If System.IO.File.Exists(sTemplateNameAndPath) Then
            oTemplateAssembly = Assembly.LoadFrom(sTemplateNameAndPath)
            oReport = Activator.CreateInstance(oTemplateAssembly.GetType(ReportClassName, True))
            Return CType(oReport, ReportBase.IReportData)
        End If

End Function

Now when I call the methods of that variable, I'm getting whatever code is set up in the class it belongs to.  The interface enforces type safety, but allows me to create new "versions" of that type, with their own behaviour.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now