By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

a cast issue

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

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

What should I do ?


Never more use IIF

Cor
Nov 21 '05 #2

P: n/a
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)

Nov 21 '05 #3

P: n/a
Sam
lol
May I ask why?

I quite like it:)

Nov 21 '05 #4

P: n/a
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

"Sam" <sa**************@voila.fr> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
Sam
yes, it is shorter :)) I'm just teasing.
Ok Thanks I won't use it then.

Nov 21 '05 #7

P: n/a
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

"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
| 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
|
Nov 21 '05 #8

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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/>

Nov 21 '05 #9

P: n/a
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| "Cor Ligthert" <no************@planet.nl> schrieb:
| >> 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/>
|
Nov 21 '05 #10

P: n/a
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
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' :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #11

P: n/a
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
Nov 21 '05 #12

P: n/a

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #13

P: n/a
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
| 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
|
|
Nov 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.