Error 27 Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.

Hi Guys

How would I best resolve this, VS 2K5 is complaing about the values in the case statements and not able to test for object Identity, I don't want to test for object identity I'm looking for values, I know I could add the ToString option after optCurrency.Value.ToString and then put both the "2" and "3" in quotes but that does't seem right either, any thoughts

Select Case Me.optCurrency.Value
Case 2
Me.optCurrency.Value = CurrencyType.Euros
'load combo boxCall LoadFundId(CurrencyType.Euros)
Case 3
Me.optCurrency.Value = CurrencyType.Sterling
'load combo box
Call LoadFundId(CurrencyType.Sterling)
End Select

 

Thanks



Answer this question

Error 27 Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.

  • hakan65

     

    Hi

     

    Thanks

     

     


  • danny y

    Glad I coud help. . .

    Yeah it looks like alot of code. . . thats because it is vb  . On the other hand alot of it is IDE generated, too.

    What this is doing is making your form as "dumb" as possible. The Class file is the 'model'. The form's load method sets up the 'view'. the events specify the 'control.'

    The important thing to note is that the goal was not to replace the select/case statement.

    The select case statement produced a "smell" indicating that perhaps the model was not really representative of the problem domain.

    By designing a more robust class hierarchy, the need for the select/case was removed. At the same time we gained a more flexible model. 

    The elimination of the select/case is just a side effect!



  • Easy_Care

    Hi Blair

    Thanks for the reply, I agree select case statements are bad practice a hang up from vb 5/6 I guess. I will look into these "type" class patterns and see if that makes for better coding.

    do you know of any specific information in MSDN I can look up

     

    Duncan


  • demercurio

    before you say 'but you make it so complicated' I say guess again. . .

    what happens when you decide to add a new currency type

    total code rewrite using the select case. . . just add a couple of lines of code using 'TypeCode' class pattern.

    Plus the code reads better. . . and is faster!



  • Daniel Holt

    here is an extremely good investigation of the pattern:

     Right Click and Save this PDF



  • Ken D.

    Hi Blair

    I am a little confused by this abstract classing, I can see how it works, just not in this case, perhaps I should have provided more information.

    Select Case Me.optCurrency.Value
    Case 2
    Me.optCurrency.Value = CurrencyType.Euros
    'load combo box
    Call LoadFundId(CurrencyType.Euros)
    Case 3
    Me.optCurrency.Value = CurrencyType.Sterling
    'load combo box
    Call LoadFundId(CurrencyType.Sterling)
    End Select

    the me.optCurrency was a group of radio buttons, each having a value, depending on which one has been selected, this calls loadFundId(currencyType.XXXX) and populates a dropdown. Me is the current form (I guess it would "this" in c#)

    So not sure whether this needs to be an abstract class, so to get over the issue being thrown by VS2K5 I've changed to an if statement rather than a select case.

    I will certianly look into abstract class in more detail, as I can see how they would benefit an application, thanks for your help

    Duncan


  • waterhunter

    Hi Blair

    Well I didn't expect that, wow thanks for that, my first reaction is that is a lot of code just to replace a select case statement, but the more I look at it the more cleaner and readable it becomes, and makes for a better application in the long run, so I will look at implmenting this into the app (got probably 4 or 5 areas to clean up).

    I have also decided to step my learning of c#, been reading some interesting articles about where Microsoft is taking VB and C#, so might be better to start now.

    Thanks once again

    Duncan


  • Ed Reid

    also. . . you could override the Derived CurrencyType.ToString such as:

    Public Class USDollarCurrency
      Inherits CurrencyType
      Public Overrides Function GetFundList() As IList(Of CurrencyFund)
        Dim result As New List(Of CurrencyFund)
        result.Add(New CurrencyFund(Guid.NewGuid, "Goldman Sachs Global Currency Fund-Dollar Plus", "14.56", "Goldman Sachs"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds US Dollar Cash Fund", "11.929", "Fidelity International"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds II US Dollar Currency Fund", "30.207", "Fidelity International"))
        Return result
      End Function

      Public Overrides ReadOnly Property Code() As CurrencyCode
        Get
          Return CurrencyCode.UsDollar
        End Get
      End Property

      Public Overrides Function ToString() As String
        Return "US Dollar ($)"
      End Function

    End Class

    and change the assignbutton function:

    Private Sub AssignButton(ByVal abutton As RadioButton, ByVal aCurrency As CurrencyType)
        abutton.Text = aCurrency.ToString()
        abutton.Tag = aCurrency
      End Sub

    and the viewer:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CurrentCurrencyFund Is Nothing Then Return
        With CurrentCurrencyFund
          MessageBox.Show(String.Format("{1}{0}{2}{0}{3}", _
                System.Environment.NewLine, .FundName, .FundManager, .Value), _currentCurrencyType.ToString())
        End With
      End Sub


  • Andres Yepes

    what is optCurrency an integer or a currency type

    on another note. . .  I can't stand select case statements. . . typically bad style.

    I will assume that Me in your example is a class called CurrencyUser

    the right way is to make Currency an abstract class with an abstract method LoadFundId(ByVal aUser as CurrencyUser)

    inherit from Currency for all your different types.

    Each CurrencyUser has a Currency field.

    CurrencyUser user just calls Me.Currency.LoadFundID(Me)

    done!



  • nhaas

  • Wes Williamson

    create a new windows app.

    add a class module call CurrencyType and add this code:

    Public MustInherit Class CurrencyType
      Public Enum CurrencyCode As Integer
        Euro
        Sterling
        UsDollar
      End Enum
      Public MustOverride Function GetFundList() As IList(Of CurrencyFund)
      Public MustOverride ReadOnly Property Code() As CurrencyCode
    End Class

    Public Class EuroCurrency
      Inherits CurrencyType
      Public Overrides Function GetFundList() As IList(Of CurrencyFund)
        Dim result As New List(Of CurrencyFund)
        result.Add(New CurrencyFund(Guid.NewGuid, "Goldman Sachs Global Currency Fund - Euro Plus ", "11.73", "Goldman Sachs"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds Euro Cash Fund  ", "11.064", "Fidelity International"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds II Euro Currency Fund  ", "19.428", "Fidelity International"))
        result.Add(New CurrencyFund(Guid.NewGuid, "STANLIB Euro Currency Fund of Funds", "73.68", "Standard Bank"))
        Return result
      End Function

      Public Overrides ReadOnly Property Code() As CurrencyCode
        Get
          Return CurrencyCode.Euro
        End Get
      End Property
    End Class

    Public Class SterlingCurrency
      Inherits CurrencyType
      Public Overrides Function GetFundList() As IList(Of CurrencyFund)
        Dim result As New List(Of CurrencyFund)
        result.Add(New CurrencyFund(Guid.NewGuid, "Goldman Sachs Sterling Liquid Reserves Fund", "4.16", "Goldman Sachs"))
        result.Add(New CurrencyFund(Guid.NewGuid, "STANLIB Offshore Sterling Fund", "13.797", "Standard Bank"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds II - Sterling Currency Fund", "19.0290", "Fidelity International"))
        Return result
      End Function

      Public Overrides ReadOnly Property Code() As CurrencyCode
        Get
          Return CurrencyCode.Sterling
        End Get
      End Property
    End Class

    Public Class USDollarCurrency
      Inherits CurrencyType
      Public Overrides Function GetFundList() As IList(Of CurrencyFund)
        Dim result As New List(Of CurrencyFund)
        result.Add(New CurrencyFund(Guid.NewGuid, "Goldman Sachs Global Currency Fund-Dollar Plus", "14.56", "Goldman Sachs"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds US Dollar Cash Fund", "11.929", "Fidelity International"))
        result.Add(New CurrencyFund(Guid.NewGuid, "Fidelity Funds II US Dollar Currency Fund", "30.207", "Fidelity International"))
        Return result
      End Function

      Public Overrides ReadOnly Property Code() As CurrencyCode
        Get
          Return CurrencyCode.UsDollar
        End Get
      End Property
    End Class


    Public Class CurrencyFund
      Private _id As Guid
      Private _fundName As String
      Private _value As Double
      Private _fundManager As String

      Public ReadOnly Property ID() As System.Guid
        Get
          Return _id
        End Get
      End Property

      Public ReadOnly Property FundManager() As String
        Get
          Return _fundManager
        End Get
      End Property

      Public ReadOnly Property FundName() As String
        Get
          Return _fundName
        End Get
      End Property

      Public ReadOnly Property Item() As CurrencyFund
        Get
          Return Me
        End Get
      End Property

      Public Property Value() As Double
        Get
          Return _value
        End Get
        Set(ByVal value As Double)
          _value = value
        End Set
      End Property

      Public Sub New(ByVal aId As Guid, ByVal aName As String, ByVal initialValue As Double, ByVal fundManager As String)
        _id = aId
        _fundName = aName
        _value = initialValue
        _fundManager = fundManager
      End Sub
    End Class

    in your main form. . . drop three radio buttons a combobox and a button. . .

    put this code in it:

    Public Class Form1

      Private _currentCurrencyType As CurrencyType

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "View"
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.DisplayMember = "FundName"
        ComboBox1.ValueMember = "Item"
        AssignButton(RadioButton1, New EuroCurrency)
        AssignButton(RadioButton2, New SterlingCurrency)
        AssignButton(RadioButton3, New USDollarCurrency)
        RadioButton1.Checked = True
      End Sub

      Private Sub AssignButton(ByVal abutton As RadioButton, ByVal aCurrency As CurrencyType)
        abutton.Text = aCurrency.Code.ToString()
        abutton.Tag = aCurrency
      End Sub

      Private Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
        If Not (TypeOf sender Is RadioButton) Then Return
        Dim button As RadioButton = CType(sender, RadioButton)
        If Not button.Checked Then
          _currentCurrencyType = Nothing
          Return
        End If
        _currentCurrencyType = CType(button.Tag, CurrencyType)
        ComboBox1.DataSource = _currentCurrencyType.GetFundList()
      End Sub

      Private ReadOnly Property CurrentCurrencyFund() As CurrencyFund
        Get
          Return CType(ComboBox1.SelectedItem, CurrencyFund)
        End Get
      End Property

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CurrentCurrencyFund Is Nothing Then Return
        With CurrentCurrencyFund
          MessageBox.Show(String.Format("{1}{0}{2}{0}{3}", _
                System.Environment.NewLine, .FundName, .FundManager, .Value), _currentCurrencyType.Code.ToString)
        End With
      End Sub
    End Class

    compile and run. . .

    code can be found here: http://www.obj-tec.com/MSDNForums/TestOptsForDuncan/testopts.zip



  • 10wattmindtrip

    Hi

    point taken about the language, yes vb should have been changed long before vb.net, too much of a hand holding thing I feel. and to a lesser extent they are still but thats another topic.

    anyway thanks for the advice I will look at the information you have provided and hopefully that will put me straight.

    Once again thanks

    Duncan


  • Archana Kamath

    now you ready. . .lets say you want to add the foo currency type. . . In CurrencyTypes.vb, change this:

    Public MustInherit Class CurrencyType
      Public Enum CurrencyCode As Integer
        Euro
        Sterling
        UsDollar
      End Enum
      Public MustOverride Function GetFundList() As IList(Of CurrencyFund)
      Public MustOverride ReadOnly Property Code() As CurrencyCode
    End Class

    to

    Public MustInherit Class CurrencyType
      Public Enum CurrencyCode As Integer
        Euro
        Sterling
        UsDollar
        Foo
      End Enum
      Public MustOverride Function GetFundList() As IList(Of CurrencyFund)
      Public MustOverride ReadOnly Property Code() As CurrencyCode
    End Class

    and add this class:

    Public Class FooCurrency
      Inherits CurrencyType
      Public Overrides Function GetFundList() As IList(Of CurrencyFund)
        Dim result As New List(Of CurrencyFund)
        result.Add(New CurrencyFund(Guid.NewGuid, "Foobar Foo Currency Fund", "100", "FooBar Holdings"))
        Return result
      End Function

      Public Overrides ReadOnly Property Code() As CurrencyCode
        Get
          Return CurrencyCode.Foo
        End Get
      End Property
    End Class

    and simply add a new radiobutton and an assignment in the FormLoad:

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "View"
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        AssignButton(RadioButton1, New EuroCurrency)
        AssignButton(RadioButton2, New SterlingCurrency)
        AssignButton(RadioButton3, New USDollarCurrency)
        AssignButton(RadioButton4, New FooCurrency)
        RadioButton1.Checked = True
      End Sub


  • AndyMc

    Hi

    I guess you're using c#, I am learning c# but have a better knowledge of VB so logical choice for me. I didn't think the type of language used mattered such much these days as everything is compiled into MSIL.


  • Error 27 Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.