Converting Objects

Posted on 2009-04-23
Medium Priority
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
  • 3
  • 2
LVL 60

Expert Comment

by:Kevin Cross
ID: 24219838
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 86

Expert Comment

by:Mike Tomlinson
ID: 24219867
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

ID: 24221051
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 24221138
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

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

Author Closing Comment

ID: 31574001

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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