Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 934213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:57:45+00:00 2026-05-15T20:57:45+00:00

I need to cast, in the better way, two objects of two types of

  • 0

I need to cast, in the better way, two objects of two types of two custom-classes (in VB.Net):

The code:

Public Class pluto
    Public Sub New(ByVal campoPippoPass As String)
        _campoPippo = campoPippoPass
    End Sub

    Private _campoPippo As String = ""

    Public Property campoPippo() As String
        Get
            Return Me._campoPippo
        End Get
        Set(ByVal value As String)
            If Not Object.Equals(Me._campoPippo, value) Then
                Me._campoPippo = value
            End If
        End Set
    End Property
End Class

Public Class pippo
    Public Sub New(ByVal campoPippoPass As String)
        _campoPippo = campoPippoPass
    End Sub

    Private _campoPippo As String = ""

    Public Property campoPippo() As String
        Get
            Return Me._campoPippo
        End Get
        Set(ByVal value As String)
            If Not Object.Equals(Me._campoPippo, value) Then
                Me._campoPippo = value
            End If
        End Set
    End Property
End Class

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim a As New pippo("ciao")
    ' here I have the error 'invalid cast'
    Dim c As pluto = CType(a, pluto)
    MsgBox(c.campoPippo)
End Sub

How can I convert “c” into an “a” type object? Is there another way than writing the following?

Dim c As New pluto(a.campoPippo)

In the case of a more complex class, it could be more easy if there was a function for the conversion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:57:45+00:00Added an answer on May 15, 2026 at 8:57 pm

    Firstly, I’m assuming the line: Dim c As pluto = CType(b, pluto) is a mistype and should actually be Dim c As pluto = CType(a, pluto)?

    You can’t cast one class to another unless they’re related. You might need to explain what you’re trying to do otherwise my answer would be, why are you creating the different classes pluto and pippo if they seem to be identical? Just create the one class and create two objects of it.

    If you do need separate classes, maybe they are related in some way and you could make pippo inherit from pluto? Or make both of them implement the same interface.

    In general I’d also suggest that it might be worth translating your class/variable names to English since that might make it easier for people to understand what you’re trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I have a class Customer (no base class). I need to cast from
How can I make class, that can be cast to DateTime. But I need
I need to cast a generic VBA form control object into a ComboBox object
I need to cast: IQueryable<PARENT> to IQueryable<Child> . IQueryable<PARENT> query = GetParents(); IQueryable<CHILD> result
I need to cast to a specific Type at Runtime. How it works, and
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
How to cast an object (of type Object) into its real type? I need
Is it possible to cast a NSInteger to a NSNumber object? I need to
Need some regular expressions help. So far I have my code working to allow
I have two date fields that I need to join on. The first is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.