Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. cannot convert from 'string' to 'AccountManagement.IAccount'

cannot convert from 'string' to 'AccountManagement.IAccount'

Scheduled Pinned Locked Moved C#
helpregexxmlquestion
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Latheesan
    wrote on last edited by
    #1

    Hi, I made a new form on my project that allows my user to search using specific search criteria. Once the record is found, i need to be able to open the Account Management form, so i have a code that looks like this: private void doEditAccount(IAccount account) { AccountManageForm form = new AccountManageForm(account); form.ShowDialog(); } private void searchBtn1_Click(object sender, EventArgs e) { try { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); iterator = nav.Select(expr); if (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); string tmpAccID = (nav2.GetAttribute("ID", "")); nav2.MoveToFirstChild(); string tmpName = (nav2.Value); nav2.MoveToNext(); string tmpLastName = (nav2.Value); nav2.MoveToNext(); string tmpBalance = (nav2.Value); nav2.MoveToNext(); string tmpOverDraftLimit = (nav2.Value); nav2.MoveToNext(); string tmpFullAddress = (nav2.Value); doEditAccount(tmpName); this.Close(); } else { MessageBox.Show("Sorry, I could not find any account for\r\nAccount ID : " + accountIDInput.Text + "", "Friendly Bank"); } } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } } When i try to build the solution to test this out, im getting this following error(s):

    Error 1 - The best overloaded method match for 'FriendlyBank.AdvSearchForm.doEditAccount(AccountManagement.IAccount)' has some invalid arguments

    Error 2 - cannot convert from 'string' to 'AccountManagement.IAccount'

    Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ :(

    C C 2 Replies Last reply
    0
    • L Latheesan

      Hi, I made a new form on my project that allows my user to search using specific search criteria. Once the record is found, i need to be able to open the Account Management form, so i have a code that looks like this: private void doEditAccount(IAccount account) { AccountManageForm form = new AccountManageForm(account); form.ShowDialog(); } private void searchBtn1_Click(object sender, EventArgs e) { try { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); iterator = nav.Select(expr); if (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); string tmpAccID = (nav2.GetAttribute("ID", "")); nav2.MoveToFirstChild(); string tmpName = (nav2.Value); nav2.MoveToNext(); string tmpLastName = (nav2.Value); nav2.MoveToNext(); string tmpBalance = (nav2.Value); nav2.MoveToNext(); string tmpOverDraftLimit = (nav2.Value); nav2.MoveToNext(); string tmpFullAddress = (nav2.Value); doEditAccount(tmpName); this.Close(); } else { MessageBox.Show("Sorry, I could not find any account for\r\nAccount ID : " + accountIDInput.Text + "", "Friendly Bank"); } } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } } When i try to build the solution to test this out, im getting this following error(s):

      Error 1 - The best overloaded method match for 'FriendlyBank.AdvSearchForm.doEditAccount(AccountManagement.IAccount)' has some invalid arguments

      Error 2 - cannot convert from 'string' to 'AccountManagement.IAccount'

      Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ :(

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Latheesan wrote:

      Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ

      Latheesan wrote:

      string tmpName = (nav2.Value);

      tmpName is defined as a string.

      Latheesan wrote:

      private void doEditAccount(IAccount account)

      The method doEditAccount requires as a paramters an object of a type that implements the IAccount interface, string does not implement that interface. You need to create the relevant object and pass that to the method.


      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      L 1 Reply Last reply
      0
      • L Latheesan

        Hi, I made a new form on my project that allows my user to search using specific search criteria. Once the record is found, i need to be able to open the Account Management form, so i have a code that looks like this: private void doEditAccount(IAccount account) { AccountManageForm form = new AccountManageForm(account); form.ShowDialog(); } private void searchBtn1_Click(object sender, EventArgs e) { try { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); iterator = nav.Select(expr); if (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); string tmpAccID = (nav2.GetAttribute("ID", "")); nav2.MoveToFirstChild(); string tmpName = (nav2.Value); nav2.MoveToNext(); string tmpLastName = (nav2.Value); nav2.MoveToNext(); string tmpBalance = (nav2.Value); nav2.MoveToNext(); string tmpOverDraftLimit = (nav2.Value); nav2.MoveToNext(); string tmpFullAddress = (nav2.Value); doEditAccount(tmpName); this.Close(); } else { MessageBox.Show("Sorry, I could not find any account for\r\nAccount ID : " + accountIDInput.Text + "", "Friendly Bank"); } } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } } When i try to build the solution to test this out, im getting this following error(s):

        Error 1 - The best overloaded method match for 'FriendlyBank.AdvSearchForm.doEditAccount(AccountManagement.IAccount)' has some invalid arguments

        Error 2 - cannot convert from 'string' to 'AccountManagement.IAccount'

        Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ :(

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        In addition to what colin said, I assume your IAccount instance probably has a property that you can access to pass the information you're wanting to pass where a string is expected.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        L 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Latheesan wrote:

          Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ

          Latheesan wrote:

          string tmpName = (nav2.Value);

          tmpName is defined as a string.

          Latheesan wrote:

          private void doEditAccount(IAccount account)

          The method doEditAccount requires as a paramters an object of a type that implements the IAccount interface, string does not implement that interface. You need to create the relevant object and pass that to the method.


          Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

          L Offline
          L Offline
          Latheesan
          wrote on last edited by
          #4

          Thank you very much for that prompt reply. I looked at my orignal code inside my MainForm which already had this doEditAccount method which is working fine, i could not see anything different from what i am trying to do on my search form. Have a quick look at the working code (if you have time) - http://nopaste.php-q.net/294607[^] The orignal working form does not need any parameter passed into the doEditAccount yet it works fine :s

          C 1 Reply Last reply
          0
          • L Latheesan

            Thank you very much for that prompt reply. I looked at my orignal code inside my MainForm which already had this doEditAccount method which is working fine, i could not see anything different from what i am trying to do on my search form. Have a quick look at the working code (if you have time) - http://nopaste.php-q.net/294607[^] The orignal working form does not need any parameter passed into the doEditAccount yet it works fine :s

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            I would guess this is what you are missing:

                        IAccount account = doFindAccount(nameTextBox.Text);
            

            Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

            L 1 Reply Last reply
            0
            • C Colin Angus Mackay

              I would guess this is what you are missing:

                          IAccount account = doFindAccount(nameTextBox.Text);
              

              Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

              L Offline
              L Offline
              Latheesan
              wrote on last edited by
              #6

              Thanks for your reply again, i tried to put it in, but it said the doFindAccount method is not found. Even if i managed to get the method working, the problem is that, the doFindAccount searches for account in the hashtable. When im searching for the first time, this account name would not be in the hashtable. So, i looked at my main form and found this code that adds new account: IAccount account = new Account(nameTextBox.Text, ""); account.SetAccNumber(nameTextBox.Text); doEditAccount(account); bank.AddAccount(account); So, from that, i see, the code adds the account to the hashtable and then it does the editing part by opening the acc manage form. So, i tried to do this in my search form like this: IAccount account = new Account(tmpName, ""); doEditAccount(account); bank.AddAccount(account); this.Close(); Now the previous error is gone and have a new one:

              The name 'bank' does not exist in the current context

              The AdvSearchForm is VERY similar to the MainForm, yet how can the form cannot find the instance 'bank' ?

              C 1 Reply Last reply
              0
              • C Christian Graus

                In addition to what colin said, I assume your IAccount instance probably has a property that you can access to pass the information you're wanting to pass where a string is expected.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                L Offline
                L Offline
                Latheesan
                wrote on last edited by
                #7

                Thanks allot sir for your reply, but im a bit confused by what you said, since i've never programmed before till few months ago, im still getting used to using Visual Studio C# Express software and learning C# This is for my programming course (1st year) and it's alrady doing my head.

                C 1 Reply Last reply
                0
                • L Latheesan

                  Thanks for your reply again, i tried to put it in, but it said the doFindAccount method is not found. Even if i managed to get the method working, the problem is that, the doFindAccount searches for account in the hashtable. When im searching for the first time, this account name would not be in the hashtable. So, i looked at my main form and found this code that adds new account: IAccount account = new Account(nameTextBox.Text, ""); account.SetAccNumber(nameTextBox.Text); doEditAccount(account); bank.AddAccount(account); So, from that, i see, the code adds the account to the hashtable and then it does the editing part by opening the acc manage form. So, i tried to do this in my search form like this: IAccount account = new Account(tmpName, ""); doEditAccount(account); bank.AddAccount(account); this.Close(); Now the previous error is gone and have a new one:

                  The name 'bank' does not exist in the current context

                  The AdvSearchForm is VERY similar to the MainForm, yet how can the form cannot find the instance 'bank' ?

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Latheesan wrote:

                  The AdvSearchForm is VERY similar to the MainForm, yet how can the form cannot find the instance 'bank' ?

                  Because bank is a field on the MainForm, not the form you are working on. The MainForm takes the bank as a parameter in the constructor and stored it in a field (sometimes known as a member variable). Perhaps you should do the same with this form.


                  Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                  L 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Latheesan wrote:

                    The AdvSearchForm is VERY similar to the MainForm, yet how can the form cannot find the instance 'bank' ?

                    Because bank is a field on the MainForm, not the form you are working on. The MainForm takes the bank as a parameter in the constructor and stored it in a field (sometimes known as a member variable). Perhaps you should do the same with this form.


                    Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                    L Offline
                    L Offline
                    Latheesan
                    wrote on last edited by
                    #9

                    Great, brilliant suggestion. I added the following line towards the begining of the partial class: IBank bank; editted the search method like this: IAccount account = new Account(tmpName, ""); doEditAccount(account); bank.AddAccount(account) And i was able to successfully build the solution. However, when i use the search form and enter an account ID and click search, it opens the account management form correctly, but when i close the account management form, i get this exception error message: http://i9.tinypic.com/4poqqab.png[^]

                    L C 2 Replies Last reply
                    0
                    • L Latheesan

                      Great, brilliant suggestion. I added the following line towards the begining of the partial class: IBank bank; editted the search method like this: IAccount account = new Account(tmpName, ""); doEditAccount(account); bank.AddAccount(account) And i was able to successfully build the solution. However, when i use the search form and enter an account ID and click search, it opens the account management form correctly, but when i close the account management form, i get this exception error message: http://i9.tinypic.com/4poqqab.png[^]

                      L Offline
                      L Offline
                      Latheesan
                      wrote on last edited by
                      #10

                      Nevermind, i fixed it by removing this line: bank.AddAccount(account); from the search method and it seems to be working. I know this is terrible, trial-and-error method is not a good way to learn new programming language, but im short on time. Im just glad its working and i can move onto the next piece of work.

                      1 Reply Last reply
                      0
                      • L Latheesan

                        Great, brilliant suggestion. I added the following line towards the begining of the partial class: IBank bank; editted the search method like this: IAccount account = new Account(tmpName, ""); doEditAccount(account); bank.AddAccount(account) And i was able to successfully build the solution. However, when i use the search form and enter an account ID and click search, it opens the account management form correctly, but when i close the account management form, i get this exception error message: http://i9.tinypic.com/4poqqab.png[^]

                        C Offline
                        C Offline
                        Colin Angus Mackay
                        wrote on last edited by
                        #11

                        But you didn't pass a bank object to the form. Either do this in the constructor, or by setting up a property to receive it. The constructor option is usually preferable if it will always need it, or the object is mandatory.


                        Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                        L 1 Reply Last reply
                        0
                        • L Latheesan

                          Thanks allot sir for your reply, but im a bit confused by what you said, since i've never programmed before till few months ago, im still getting used to using Visual Studio C# Express software and learning C# This is for my programming course (1st year) and it's alrady doing my head.

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #12

                          well, I'd recommend buying a book and working through it. We're glad to help ( you're asking us to help and trying to do it yourself, and that's never a problem ), but a trial and error approach isn't going to help you understand the core principles that you're struggling to grasp now. The main issues here as far as I can see: 1 - a variable has a type. If you try to pass an object of the wrong type, you're putting a square peg in a round hole. It's a 'thing', but it's not the right 'thing'. 2 - A class is an object designed to wrap specific functionalty. Once you define a class, you can have many instances of a class, just like knowing what a car is, I can own 3. If I have three cars, they are all independant objects, changing the tires on one car won't change the other ones. And, objects that exist within a class, are not visible outside that class, unless they are public, and then they need to be addressed via the class instance. If I want something that's in my car, I have to look inside the car, not on the ground. I hope that makes *some* sort of sense, but seriously, read through the texts you've been given, or consider buying a book. And, do some really basic to the point of useless projects that exist entirely to help you understand concepts like types and class scope.

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                          1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            But you didn't pass a bank object to the form. Either do this in the constructor, or by setting up a property to receive it. The constructor option is usually preferable if it will always need it, or the object is mandatory.


                            Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                            L Offline
                            L Offline
                            Latheesan
                            wrote on last edited by
                            #13

                            But you didn't pass a bank object to the form.

                            I didn't need to, so i ended up removing the line IBank bank;

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups