returning a structure from a form
My code:
VB Code:
Private Sub SelectStrategy()
Dim f As New Strategies.SelectStrategyfrm
f.ShowDialog()
If Not f.SelectedStrategy Is Nothing Then '<====no good!!! ERROR! DANGER WILL ROBINSON!
' the error is 'Is' requires operands that have
' reference types, but this operand has the value type 'Globals.StrategyStruct'.
MyOptions.CurrentStrategy = f.SelectedStrategy
End If
f.Dispose()
End Sub
My goal:
The form being created prompts the user for information. If the user selects something a public structure is populated and the form closes, no problem. If however, the user selects Cancel, the public structure is set to Nothing and the form closes.
VB Code:
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
SelectedStrategy = Nothing
Me.Close()
End Sub
But as you see in the first code segment, I have pointed out that I get an error.
I don't understand the error. If I can use
VB Code:
SelectedStrategy = Nothing
why can't I use
VB Code:
If Not f.SelectedStrategy Is Nothing Then
and how do I propery do what I am trying to do?
thanks
kevin
Re: returning a structure from a form
structures are "value types" and you cant use IS with them. It's like having an integer and trying to check to see if the integeger Is Nothing.... it just wont work :)
either make it a class instead of a structure, or write a function that'd check to see if the fields in the structure have their default values
Re: returning a structure from a form
ok... so I changed
VB Code:
Public Structure StrategyStruct
to
VB Code:
Public Class StrategyStruct
and it fixed the problem. I don't really understand why
but with most OOP stuff if I hack hard enough, eventually it works.
thanks MrPolite
kevin
Re: returning a structure from a form
Why not just create a dialog that returns a new class instance instead of populating one that already exists?
http://www.vbforums.com/showthread.php?t=331386
Re: returning a structure from a form
Quote:
Originally Posted by kebo
with most OOP stuff if I hack hard enough, eventually it works.
Trial and error is not good coding practice. You should see MSDN for the differences between structures and classes, there is an article about it.
Re: returning a structure from a form
Quote:
Originally Posted by wossname
I do, bad choice of word I supose.
Quote:
Originally Posted by wossname
Trial and error is not good coding practice. You should see MSDN for the differences between structures and classes, there is an article about it.
That was meant mostly tongue in cheek if you know what I mean (I couldn't find a tonue in cheek smilely) but I will look for the article.
thanks for the input