locked
Operator '+' is not defined for types 'Client' and 'String' RRS feed

  • Question

  • User394746963 posted

    I am working on a site that I would like to pass information from one page to another. I'm new to asp.net but not other languages, I'm just trying to learn some stuff. For some reason I keep getting the above error. The code I am using looks real similar to the code in the Murach book I am looking at, but I still get the error. Any help would be great.


    //Code

    Partial Class Customer
        Inherits System.Web.UI.Page

        Private SelectedClient As Client

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                ddlName.DataBind()
            End If
            SelectedClient = Me.GetSelectedClient()
            lblAddress.Text = SelectedClient.Address
            lblAddress2.Text = SelectedClient.Address2
            lblPhone.Text = SelectedClient.Phone
            lblEmail.Text = SelectedClient.Email
            lblIncidents.Text = SelectedClient.Incidents
        End Sub

        Private Function GetSelectedClient() As Client
            Dim dvTable As Data.DataView = CType(AccessDataSource2.Select(DataSourceSelectArguments.Empty), Data.DataView)
            dvTable.RowFilter = "ID = '" & ddlName.SelectedValue & "'"
            Dim drvRow As Data.DataRowView = dvTable(0)
            Dim Client As New Client

            Client.ID = drvRow("ID").ToString
            Client.Name = drvRow("Name").ToString
            Client.Address = drvRow("Address").ToString
            Client.Address2 = drvRow("Address2").ToString
            Client.Phone = drvRow("Phone").ToString
            Client.Email = drvRow("Email").ToString
            Client.Incidents = drvRow("Incidents").ToString
            Return Client
        End Function
        Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            If Page.IsValid Then
                Dim List As New List
                List.Client = SelectedClient
                List.Phone = SelectedClient
                List.Email = SelectedClient
                Me.AddToList(List)
                response.redirect("ContactList.aspx")
            End If
        End Sub
        Private Sub AddToList(ByVal List As List)
            Dim ContactList As SortedList = GetContact()
            Dim sName As String = SelectedClient.Name
            Dim sPhone As String = SelectedClient.Phone
            Dim sEmail As String = SelectedClient.Email
            If ContactList.ContainsKey(sName) Then
                List = CType(ContactList(sName), List)
                List.Phone += CType(lblPhone.Text, String)
                List.Email += CType(lblEmail.Text, String)
            Else
                ContactList.Add(sName, List)
            End If
        End Sub

        Private Function GetContact() As SortedList
            If Session("ContactList") Is Nothing Then
                Session.Add("ContactList", New SortedList)
            End If
            Return CType(Session("ContactList"), SortedList)
        End Function
        
    End Class

    Monday, October 19, 2009 11:17 PM

Answers

  • User-952121411 posted

    The '+=' notation is just the shorthand methodology for adding or concatenating (2) values together.  Take a look below:

    Dim MyValue As Integer = 1
    
    'The (2) lines below are the same
    MyValue +=1
    
    MyValue = MyValue + 1


    Both lines above produce a value = 2.

    A couple of notes about your code:

    1. The Ctype(  ,String) code is not really needed, because the '.Text' properties are already in the type of 'System.String'.  If anything you should add .Trim() at the end to remove spaces if any exist.
    2. The code as written (even though it does not work) states that you want to update the '.Phone' property on the object 'List' by adding to the existing value.  Are you sure you want to really do this?  If the 1st value added to that property is 555-123-4567 and the second value added is 555-987-1234, then the properies value is going to end up being 555-123-4567555-987-1234.  That does not seem right to me that you want to keep adding or concatenating property values together.  I would think you want the following code: list.phone = lblPhone.text.trim()  However, I do not know the reqirements and maybe you are trying to add values together, and then parse them back apart later.  If you need multiple object instances all with unique values, a List(of MyObject) is a better alternative.
    3. If you really need to add property values together, like your code is attampting, you could try the longhand version (below) to see if it works:
    list.phone = list.phone & lblPhone.text.trim()


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2009 2:14 PM

All replies

  • User2019981500 posted


    Hi Great Man,
    looking to your code,i didn't find any mistake but one thing is sure that this type of error gets displayed only when
    you have set "Option Explicit=true" for Vb.Net Compiler.
    what exacly is impact of above statement ?

    Just go through your code and see if you are using "&" or "+" for concatenation,if so just check whether you are concatenating strings only? like i can see your code contains  statements like

    dvTable.RowFilter = "ID = '" & ddlName.SelectedValue & "'"

    it is ok as long as you are working in loosely typed environment or language like vb.net.but c# will never accept it or vb.net too if option explicit is set to true. now only suggestion from myside is that if you are doing any contcatenation job in your program then do it like below

    dvTable.RowFilter = "ID = '" & ddlName.SelectedValue.ToString() & "'"

    because  in above statement i am making sure that i concatenate strings only rather than your statement is concatenating object with string.
    I hope this can give you some vision about your mistake

    Regards

    Tuesday, October 20, 2009 12:02 AM
  • User-952121411 posted

    Hello-

    The easiest way to debug this and determine the line causing the issue is to configure how exceptions are thrown in VS.NET.  In the menus at the top of the IDE goto 'Debug -> Exceptions...' (or press Ctrl + Alt + E for the keyboard shortcut) to access the 'Exceptions' dialog box.

    Go ahead and make sure all of the check boxes listed under the 'Thrown' column.  Then go ahead and run your application.

    What this will do is stop execution on the actual problematic line of code.  The best thing to do, so we can help better is to paste the pertinent code associated with the problematic line, and highlight which line is causing the problem.

    Now if this error is occuring at build, you can easily double click the error in the 'Error List' tab that sits at the bottom of the IDE and double-click on the error.  It will take you straight to the problem line.

    The error you are receiving should not be too difficult to analyze, but the code you posted seems to be missing the line where this error occurs.  The only place you are even using a '+' symbol is in the following (2) lines, which don't look immdeatly to have any issue:

    List.Phone += CType(lblPhone.Text, String)
                List.Email += CType(lblEmail.Text, String)

    Please post back with the problematic code once you have tried the steps above.

    Tuesday, October 20, 2009 9:32 AM
  • User394746963 posted

    The line of code that is causing the problem is the one you specified.


    List.Phone += CType(lblPhone.Text, String)
    List.Email += CType(lblEmail.Text, String)


    both of these lines cause an error.

    Tuesday, October 20, 2009 12:42 PM
  • User394746963 posted

    Hello,

    Thanks for any help you are able to offer. I'm sure by looking at the code you can tell, but I have two forms i'm trying to pass specific information from one form to the other. the list.phone += CType(lblPhone.text, string) is supposed to be making a string with the information that I want placed in the list. For some reason this is not working. Though when I remove the two lines of code everything works fine. Not sure whats going on, I was just doing as the book suggested.

    Tuesday, October 20, 2009 1:07 PM
  • User-952121411 posted

    The '+=' notation is just the shorthand methodology for adding or concatenating (2) values together.  Take a look below:

    Dim MyValue As Integer = 1
    
    'The (2) lines below are the same
    MyValue +=1
    
    MyValue = MyValue + 1


    Both lines above produce a value = 2.

    A couple of notes about your code:

    1. The Ctype(  ,String) code is not really needed, because the '.Text' properties are already in the type of 'System.String'.  If anything you should add .Trim() at the end to remove spaces if any exist.
    2. The code as written (even though it does not work) states that you want to update the '.Phone' property on the object 'List' by adding to the existing value.  Are you sure you want to really do this?  If the 1st value added to that property is 555-123-4567 and the second value added is 555-987-1234, then the properies value is going to end up being 555-123-4567555-987-1234.  That does not seem right to me that you want to keep adding or concatenating property values together.  I would think you want the following code: list.phone = lblPhone.text.trim()  However, I do not know the reqirements and maybe you are trying to add values together, and then parse them back apart later.  If you need multiple object instances all with unique values, a List(of MyObject) is a better alternative.
    3. If you really need to add property values together, like your code is attampting, you could try the longhand version (below) to see if it works:
    list.phone = list.phone & lblPhone.text.trim()


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2009 2:14 PM
  • User394746963 posted

    Thank you for your help. I am unsure about why the book was doing that. When I removed those 2 lines of code, the program worked the way I wanted it to. I appreciate you explaining what the code itself was doing and after learning that I now know that it is not what I was wanting. Thanks again.

    Tuesday, October 20, 2009 7:32 PM