473,480 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possible to convert an Object Type into another?

29 New Member
Hi
I'm trying to do this:

Expand|Select|Wrap|Line Numbers
  1. Dim MyForm As New Object
  2.  
  3.    or
  4.  
  5. Dim MyForm As System.Windows.Forms.Form
  6.  
  7. Select Case Form.Activeform.Name
  8.   Case My.Forms.Form1.Name
  9.      MyForm = My.Forms.Form1
  10.   Case My Forms.Form2.Name
  11.      MyForm = My.Forms.Form2
  12. End Select
  13.  
The problem comes because MyForm is still an Object or Form type.
So I need to convert MyForm Type whether into Form1 or Form2, in order to be access to properties and methods of one of those class.

I tried with Ctype, DirectCast, TryCast, Convert.ChangeType... etc., and MyForm always keep its Object or Form Type.

Doing a unboxing neither works to me. For instance:

CType(MyForm, Form2).property.xxxx.focus()

That works but not to what I really need. I want to convert MyForm or whatever it was into a Form1 or Fomr2 Class.

Thanks.
Apr 30 '15 #1
1 1542
Seth Schrock
2,965 Recognized Expert Specialist
I'm not sure what you are looking for, but I think that you are wanting to be able to reference a form based on its string name and have access to all its controls, properties and methods. If this is the case, then you would need to paste this function into your class:
Expand|Select|Wrap|Line Numbers
  1. 'from http://vbcity.com/forums/t/32930.aspx
  2. Function GetFormByName(ByVal FormName As String) As Form
  3.     'first try: in case the full namespace has been provided (as it should ;-) )
  4.     Dim T As Type = Type.GetType(FormName, False)
  5.     'if not found prepend default namespace
  6.     If T Is Nothing Then
  7.         Dim Fullname As String = Application.ProductName & "." & FormName
  8.         T = Type.GetType(Fullname, True, True)
  9.     End If
  10.     Return CType(Activator.CreateInstance(T), Form)
  11. End Function
You would then use your Form.Activeform.Name as the argument when calling this function.
May 5 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2513
by: Ram | last post by:
Hi, I have a container of base pointers and I want to generate a container of derived class pointers from it by casting each pointer of base class to a pointer of derived class. Is it possible...
2
2054
by: Jon the Blind | last post by:
I'm looking for a way to retrieve data via System.Reflection.FieldInfo.GetValue (which returns an Object) and convert it to XML using XmlConvert.ToString. XmlConvert accepts only specified data...
3
9946
by: windandwaves | last post by:
Hi Folk I am trying to delete an object in another database, but from reading the google newsgroup archives, it seems that this is not possible. Does anyone know any easy work-arounds? Can you...
7
11917
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
5
6761
by: tienlx | last post by:
Hi all, I'm have a problem that i don't know how to convert object to char in C# . Does anybody know ? Thanks.
2
2097
by: Kimelia Schiles | last post by:
Hi, I would like to know how to convert the value with return type of object to a byte array. For example: byte photo = ds.Tables.Rows; Thanks and have a nice day
1
9892
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
1
3020
by: sebascomeau | last post by:
Hi, I don't know if this is possible but a just want to know if it is. I want to create a new object on the fly from another object with the same properties but all properties was another type...
2
1361
by: tatinccr | last post by:
Hi guys, I have a collection in a class, and I have another class and I need to reference this collection in another class. How can I do that? This is my code: namespace CashRegister.Reports {...
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.