Is it posible to do this casting in VB?

A

active

Function CastIt (byval z as boolean) As ??
if z then
CastIt = DirectCast(ActiveMdiChild, MyForm1)
else
CastIt = DirectCast(ActiveMdiChild, MyForm2)
end if
End Function

Can I make a function in VB that returns the result of a DirectCast.

I think what I don't know is what to use for ?? above.

Is it possible to do this in VB?


Thanks

PS
I'd use it like this

ZZ= CastIt.Getsomething

Where both MyForm1 and MyForm2 have the property Getsomething

I find it hard to believe the compiler would let me do that but maybe you
see a different approach.

I could do it without a function
if z then
qq = DirectCast(ActiveMdiChild, MyForm1).Getsomething
else
qq = DirectCast(ActiveMdiChild, MyForm2).Getsomthing
end if

But I need to do it many times


Thanks
 
H

Herfried K. Wagner [MVP]

active said:
Function CastIt (byval z as boolean) As ??
if z then
CastIt = DirectCast(ActiveMdiChild, MyForm1)
else
CastIt = DirectCast(ActiveMdiChild, MyForm2)
end if
End Function

Can I make a function in VB that returns the result of a DirectCast.

I think what I don't know is what to use for ?? above.

Is it possible to do this in VB?

Not really, if the method is declared in both types separately and they
share 'Form' as common base class.

However, if you define the method in a base type (base class) or implement
an interface containing the method, you can use this type as the method's
return type.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

active said:
Function CastIt (byval z as boolean) As ??
if z then
CastIt = DirectCast(ActiveMdiChild, MyForm1)
else
CastIt = DirectCast(ActiveMdiChild, MyForm2)
end if
End Function

Can I make a function in VB that returns the result of a DirectCast.

I think what I don't know is what to use for ?? above.

Is it possible to do this in VB?


Thanks

PS
I'd use it like this

ZZ= CastIt.Getsomething

Where both MyForm1 and MyForm2 have the property Getsomething

I find it hard to believe the compiler would let me do that but maybe you
see a different approach.

I could do it without a function
if z then
qq = DirectCast(ActiveMdiChild, MyForm1).Getsomething
else
qq = DirectCast(ActiveMdiChild, MyForm2).Getsomthing
end if

But I need to do it many times


Thanks

You can't make a function return different data types.

What you can do is to make a base class that inherits Form and contains
the virtual method GetSomething, and let each form inherit from the base
class instead of Form and implement the GetSomething method.

Then you can cast your reference to the base class, and call the method.
 
C

Cor Ligthert [MVP]

Hi,

Function CastIt (byval z as MyForm1, y as MyForm2) As boolean
if type of z is MyForm1 then
CastIt = DirectCast(ActiveMdiChild, MyForm1)
return true
else
CastIt = DirectCast(ActiveMdiChild, MyForm2)
return false
end if
End Function

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Function CastIt (byval z as MyForm1, y as MyForm2) As boolean
if type of z is MyForm1 then
CastIt = DirectCast(ActiveMdiChild, MyForm1)
return true
else
CastIt = DirectCast(ActiveMdiChild, MyForm2)
return false
end if
End Function

What would be the task this procedure solves?
 
T

Tom Leylan

Don't mean to question your need for such a function but it seems unlikely
to be of much use. In your example the variable qq would have been declared
as a given form type right? The DirectCast information would simply be lost
if it is of type Form.

Perhaps posting a little more information would help, what's the goal?
 
C

Cor Ligthert [MVP]

Herfried,

I don't know but I can me immage, that I have 1 special and more general
formats for my MDI form. (Not so unusual), If I than have to do something
where a cast is needed I can do this.

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Excuse me for saying it, but that is pure nonsense.

:: As the return value of the method is Boolean, you can not assign a
form to it.

:: You a mixing the use of parameters with the use of global/member
variables.

:: The parameter y is never used at all.

:: The type of z is always MyForm1 so the if statement is pointless.

:: Casting to the specific form class serves no purpose at all, as the
return type of a method is always the same.

:: Assigning a value to the method name serves no purpose if you are
also using Return.
 
C

Chris Dunaway

Function CastIt (byval z as boolean) As ??
if z then
CastIt = DirectCast(ActiveMdiChild, MyForm1)
else
CastIt = DirectCast(ActiveMdiChild, MyForm2)
end if
End Function

Can I make a function in VB that returns the result of a DirectCast.

I think what I don't know is what to use for ?? above.

Is it possible to do this in VB?

Thanks

PS
I'd use it like this

ZZ= CastIt.Getsomething

Where both MyForm1 and MyForm2 have the property Getsomething

I find it hard to believe the compiler would let me do that but maybe you
see a different approach.

I could do it without a function
if z then
qq = DirectCast(ActiveMdiChild, MyForm1).Getsomething
else
qq = DirectCast(ActiveMdiChild, MyForm2).Getsomthing
end if

I'm not sure what you're attempting to accomplish. Does GetSomething
return a different type for each form?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
I'm not sure what you're attempting to accomplish. Does GetSomething
return a different type for each form?

Yes, that is what he would like to do, but i have already explained that
this is impossible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top