Converting Objects

Posted on 2009-04-23
Last Modified: 2012-05-06
Object used to convert, now it doesn't???
What should I do?
Private Function ReturnValue(ByVal oName As Object, _

            ByVal r As Integer, Optional ByVal c As Integer = -1) As Object

        Dim o As Object = Nothing

        Dim fGrd As FarPoint.Win.Spread.FpSpread = CType(oName, FarPoint.Win.Spread.FpSpread)

        With fGrd.ActiveSheet

            If c = -1 Then

                c = 2

            End If

            o = .GetValue(r, c)

        End With

        Return o

    End Function

Open in new window

Question by:Sheritlw
    LVL 59

    Expert Comment

    by:Kevin Cross
    I would think you would have to validate that oName is of type FarPoint.Win.Spread.FpSpread before doing a type conversion or else this should fail as you may have incompatible objects.  When you say this use to convert, where you in another version of VB or what has changed?  Could the value of oName be Nothing now or some invalid object due to logic error.  Try debugging and seeing what is oName is at point it is trying to be converted.  If you have an error message, please post that.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Object doesn't automatically convert to anything as it is the "base object" for everything.

    Why are you using Object here anyways?...It seems that you ALREADY know that "oName" is of type FarPoint.Win.Spread.FpSpread...otherwise it would be UNSAFE to convert it to that type without first checking its Type or enclosing it in a Try/Catch block.

    Furthermore, if the Type is KNOWN then the Type of GetValue() should also be known and you can specify the proper return Type for the ReturnValue() function.

    Unless something "trickier" is going on in the bigger scope of things that you aren't showing in the question?...

    Author Comment

    I end up with more than 50 grids on this one form.  Each tab containes a similar layout and controls are named the same but the index number of the tab is appended to the name giving it a unique name.
    Now they have 6 tabs that they can go back and forth from and click save whenever.  When they click save I need to find out what tab(s) grids have changes and update/save to db.
    Don't know if this is the most efficient way and I am sure I will be re-working this program as I learn this .Net stuff.
    LVL 85

    Accepted Solution

    Ok... it sounds like the code I gave you in your other question should help:
    (It allows you to find a control based on a String name)

    Here is another example where Label1 thru Label5 are found using a dynamically built string:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 1 To 5
                Dim ctl As Control = GetControlByName("Label" & i)
                If Not IsNothing(ctl) Then
                    If TypeOf ctl Is Label Then
                        Dim lbl As Label = DirectCast(ctl, Label)
                        lbl.Text = "Label #" & i
                    End If
                End If
        End Sub
        Private Function GetControlByName(ByVal ControlName As String) As Control
            Dim matches() As Control = Me.Controls.Find(ControlName, True)
            If matches.Length > 0 Then
                Return matches(0)
                Return Nothing
            End If
        End Function
    End Class

    Open in new window


    Author Comment

    That is great... Thank you so much.  You truly are a genius.
    Thank you again.

    Author Closing Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now