a cast issue

S

Sam

Hi,
I don't understand that:

Me.DialogResult = IIf(m_UserWeb.AddUserWeb(Data), DialogResult.OK,
DialogResult.Abort)

(m_UserWeb.AddUserWeb(Data) returns a boolean)

gives : Option Strict On disallows implicit conversions from
'System.Object' to 'System.Windows.Forms.DialogResult'.

What should I do ?

Thx
 
C

Chris Dunaway

You have to cast the result to a DialogResult. IIf returns object but
it needs to be DialogResult:

Me.DialogResult = DirectCast(IIf(m_UserWeb.AddUserWeb(Data),
DialogResult.OK,
DialogResult.Abort), DialogResult)
 
C

Carlos J. Quintero [.NET MVP]

Hi Sam,

It's IIF who returns an Object:

Public Shared Function IIf(ByVal Expression As Boolean, ByVal TruePart As
Object, ByVal FalsePart As Object) As Object

So, use:

Me.DialogResult = CType(IIf(m_UserWeb.AddUserWeb(Data), DialogResult.OK,
DialogResult.Abort), DialogResult)

Or make your code crystal-clear ;-) :

If m_UserWeb.AddUserWeb(Data) Then
Me.DialogResult = DialogResult.OK
Else
Me.DialogResult = DialogResult.Abort)
End If


--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
C

Cor Ligthert

Sam,
lol
May I ask why?

I quite like it:)

Because it is bad as documentation, gives forever misunderstandings and is
not shorter than

me.DialogResult = false
if (m_UserWeb.AddUserWeb(Data)) then me.DialogResult = true

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Sam,
VB.IIf is a function that accepts two Object values & returns an Object
value, you need to cast the return value back to DialogResult to use it:

| Me.DialogResult = DirectCast(IIf(m_UserWeb.AddUserWeb(Data),
DialogResult.OK,
| DialogResult.Abort), DialogResult).

Because of the "awkwardness" of the above DirectCast I normally avoid IIf in
VB.NET 2003 & 2003

With VB.NET 2005 one can define a Generic version of IIf that does not need
the above DirectCast, plus it avoids any potential boxing issues.

' VS.NET 2005 syntax
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function

The generic IIf(Of T) function avoids the need (the "awkwardness") of the
DirectCast. I would consider using the Generic IIf as it may simplify some
expressions...

You can use IIf(Of T) as you would VB.IIf, the compiler is able to infer the
parameter for T.

Either:
Me.DialogResult = IIf(m_UserWeb.AddUserWeb(Data), _
DialogResult.OK, DialogResult.Abort)

Or:
Me.DialogResult = IIf(Of DialogResult)(m_UserWeb.AddUserWeb(Data), _
DialogResult.OK, DialogResult.Abort)


However! the "problem" with both the VB.IIf & my IIf(Of T) is that both
operands are evaluated which may have undesired side effects.

Hope this helps
Jay

| Hi,
| I don't understand that:
|
| Me.DialogResult = IIf(m_UserWeb.AddUserWeb(Data), DialogResult.OK,
| DialogResult.Abort)
|
| (m_UserWeb.AddUserWeb(Data) returns a boolean)
|
| gives : Option Strict On disallows implicit conversions from
| 'System.Object' to 'System.Windows.Forms.DialogResult'.
|
| What should I do ?
|
| Thx
|
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Because it is bad as documentation, gives forever misunderstandings and is
not shorter than

me.DialogResult = false
if (m_UserWeb.AddUserWeb(Data)) then me.DialogResult = true

.... or in this particular case:

\\\
Me.DialogResult = m_UserWeb.AddUserWeb(Data)
///

However, this doesn't mean that 'IIf' should not be used at all.
 
J

Jay B. Harlow [MVP - Outlook]

Herfried & Cor,
Actually the OP wants:

If m_UserWeb.AddUserWeb(Data) Then
Me.DialogResult = DialogResult.OK
Else
Me.DialogResult = DialogResult.Abort
End If

Of course you could make it a single line with (watch line wrapping):

If m_UserWeb.AddUserWeb(Data) Then Me.DialogResult = DialogResult.OK Else
Me.DialogResult = DialogResult.Abort

Both seem to be a lot of extra typing to me... Ergo I like my Generic IIf...

Hope this helps
Jay


| >> May I ask why?
| >>
| >> I quite like it:)
| >
| > Because it is bad as documentation, gives forever misunderstandings and
is
| > not shorter than
| >
| > me.DialogResult = false
| > if (m_UserWeb.AddUserWeb(Data)) then me.DialogResult = true
|
| ... or in this particular case:
|
| \\\
| Me.DialogResult = m_UserWeb.AddUserWeb(Data)
| ///
|
| However, this doesn't mean that 'IIf' should not be used at all.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 
H

Herfried K. Wagner [MVP]

Jay,

Jay B. Harlow said:
Actually the OP wants:

If m_UserWeb.AddUserWeb(Data) Then
Me.DialogResult = DialogResult.OK
Else
Me.DialogResult = DialogResult.Abort
End If

You are right, I only wanted to post a simplified version of Cor's code
which didn't work with the well-known 'DialogResult' ;-).
Of course you could make it a single line with (watch line wrapping):

If m_UserWeb.AddUserWeb(Data) Then Me.DialogResult = DialogResult.OK Else
Me.DialogResult = DialogResult.Abort

In this case I would either use a multi-line 'If' or your generic 'IIf' :).
 
C

Cor Ligthert

Jay,

I knew this and I did not like my sample either. However, I tried to make it
short.
For that is Herfried sample even better.

I don't like (and that is nice said) one line conditional statements and
that is in any language. I was even in doubt to send my sample because an
one-line "if" in VBNet is something I don't like either, I find those for
reading confusing.

Maybe it is a personal opinion, however that does not change it for me.

(The select case was as well possible by the way)

Cor
 
M

Mythran

Cor Ligthert said:
Jay,

I knew this and I did not like my sample either. However, I tried to make
it short.
For that is Herfried sample even better.

I don't like (and that is nice said) one line conditional statements and
that is in any language. I was even in doubt to send my sample because an
one-line "if" in VBNet is something I don't like either, I find those for
reading confusing.

Maybe it is a personal opinion, however that does not change it for me.

(The select case was as well possible by the way)

Cor

I agree with Cor on "I don't like ... one line conditional statements" for
the most part; however, I do like the following:

C#:

int i = someInteger;
string s = "i is " + (i < 0 ? "less than " : "greater than ") + " 0.";

Note: This was just an example only. I wonder it this gets optimized more
than:

string s = "i is {0} 0.";
if (i < 0) {
s = String.Format(s, "less than");
} else {
s = String.Format(s, "greater than");
}

or

string s = "i is ";
if (i < 0) {
s += "less than";
} else {
s += "greater than";
}
s += " 0.";

<shrug>

Sorry it's not in VB, but can't post a similar example in VB since VB has no
inline if (condition ? trueresult : falseresult). IIf is a method, not a
statement.

Mythran
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
I agree single line If statements can be confusing & will sometimes
"obscure" the logic. I use them sparingly, usually for Guard Clauses! Where
I'm throwing an exception or returning a value at the beginning of a method.
Something like:

Public Sub SomeMethod(value As String)
If value Is Nothing Then Throw New ArgumentNullException("value")
...
End Sub

I find a multi-line if above to be "too" verbose...

Hope this helps
Jay



| Jay,
|
| I knew this and I did not like my sample either. However, I tried to make
it
| short.
| For that is Herfried sample even better.
|
| I don't like (and that is nice said) one line conditional statements and
| that is in any language. I was even in doubt to send my sample because an
| one-line "if" in VBNet is something I don't like either, I find those for
| reading confusing.
|
| Maybe it is a personal opinion, however that does not change it for me.
|
| (The select case was as well possible by the way)
|
| Cor
|
|
 

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

Similar Threads


Top