Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I want to pass 2 objects as arguments to a Sub. When I declare the sub:

 

Public Sub Something (ByRef A As Object, ByRef B As Object)

 

it gives me an error, because I have Option Strict On. When I make it Off it works, but I know that Option Strict Off it's not a good programming tactic. Is there a way to use Option Strict On and to manage to pass the Objects as parameters to a fuction?

Posted

Sub
Dim Class1 as MyObject = New MyObject("Hello")
Dim Class2 as MyObject = New MyObject("Hello Again")

Something(CType(Class1, Object), CType(Class2, Object))
End Sub

Class MyObject
   Inherits Object
   Private S as String

   Public Sub New(Byval Value as String)
   S = Value
   End Sub

   Public ReadOnly Property MyValue() as String
       Get
           Return S
       End Get
   End Property
End Class

Sub Something(Byref A as Object, Byref B as Object)
Msgbox CType(A, MyObject).MyValue, , "Message from A"
Msgbox CType(B, MyObject).MyValue, , "Message from B"
End Sub

You need to use CType to convert an object into one of it's bases.

CType(Object, ClassToConvertTo)

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
  • *Gurus*
Posted

Everything derives from object, therefore there will be no problem with passing anything to an argument that is of that type, whether Option Strict is on or off.

 

If you got an error, it was probably something else.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted
Thanks a lot! It seems you know VB.NET pretty well. Now, if only you could answer to one more question that I have, I would be gratefull (look "Auto generate instances")

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...