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

Posted on 2007-07-31
Last Modified: 2010-04-23
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

Question by:DigitalDan3
    LVL 38

    Expert Comment

    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

    Author Comment

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

    LVL 38

    Expert Comment

    >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.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    What is the end goal here?...

    ~HOW~ are you going to utilize this "feature" if it could be done?
    LVL 27

    Assisted Solution

    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
    LVL 38

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now