How To Create an instance of an object from another instance?

  • Thread starter Thread starter Andro Tan
  • Start date Start date
A

Andro Tan

good day..

is there a way to create an instance of an object from another object?..hope
somebody can help me with this.

Private form as new MyForm

Load

Dim instance1 as MyForm = form
instance1.Show

Dim instance2 as MyForm = form
instance2.Show

i want to show two forms. but i doesn't do so..its because i don't think it
is necessary to go throught the initializecomponent again..which take really
a lot of time.

isn't there any way like for example dim form1 as myform =
createobjectfrom(form).

thanks in advance
 
Since these are ByReference objects whenever you assign
them to a new variable it will only assign a reference to
the original object and not a new object.

If you want to show two identical forms then you need to
put code like the following.

Dim Frm1 as new MyForm
Fmr1.show

Dim Frm2 as new MyForm
Frm2.show


Some objects have a clone method that will make a copy of
the object and not just copy the reference. I know Arrays
have this and you can use it like this.

'this code will work if option strict is on.
Dim str1(10) As String
Dim str2() As String
str2 = DirectCast(str1.Clone, String())

Hope this helps.

Matt
 
oh :( so microsoft haven't made any of that? what i really need is not clone
but the copy method of like for example the datatable object. because i
think it would be useless initializing all of the controls in the form, if i
could only do it once right?..hope somebody has a great idea for this..tnx
again for the help..

Jop Pascual said:
You might try using reflection to create a new instance of the object/form
that you want, and then show it..

Dim Fmr2 as Form
Fmr2 = [Assembly].GetExecutingAssembly(Fmr1.GetType().Fullname)
Fmr2.Show()

Of course, it won't be a clone of Fmr1. It's just another instance of it.

Matt Huntington said:
Since these are ByReference objects whenever you assign
them to a new variable it will only assign a reference to
the original object and not a new object.

If you want to show two identical forms then you need to
put code like the following.

Dim Frm1 as new MyForm
Fmr1.show

Dim Frm2 as new MyForm
Frm2.show


Some objects have a clone method that will make a copy of
the object and not just copy the reference. I know Arrays
have this and you can use it like this.

'this code will work if option strict is on.
Dim str1(10) As String
Dim str2() As String
str2 = DirectCast(str1.Clone, String())

Hope this helps.

Matt
 
Back
Top