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. Problem with function argument

Problem with function argument

Scheduled Pinned Locked Moved C#
helpbusiness
5 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.
  • F Offline
    F Offline
    faisal khanani
    wrote on last edited by
    #1

    i am working on a project in which i'm dealing with business layer and data layer where business layer has a reference of data layer. when business layer call a function of data layer sending ref object variable as parameter function signature in data layer is: "public bool add(ref object objagent)" this ref object variable is actually an instance of business layer class, but when i proceed with function assigning values like: string stragentname; stragentname=objagent.agentname; upon complilation it gives me error:"object doesnot contain a definition for agentname"

    M 1 Reply Last reply
    0
    • F faisal khanani

      i am working on a project in which i'm dealing with business layer and data layer where business layer has a reference of data layer. when business layer call a function of data layer sending ref object variable as parameter function signature in data layer is: "public bool add(ref object objagent)" this ref object variable is actually an instance of business layer class, but when i proceed with function assigning values like: string stragentname; stragentname=objagent.agentname; upon complilation it gives me error:"object doesnot contain a definition for agentname"

      M Offline
      M Offline
      Matt Gerrans
      wrote on last edited by
      #2

      That's because you are passing the thing as an object, instead of whatever type it really is. Your method signature should probably be something like this:

      public bool Add( Agent agent )
      {
      string agentName = agent.agentName;
      ...
      }

      From what you've shown, there is no need to have the ref keyword in there. Also, you don't need Hungarian notation with C#. Matt Gerrans

      F 1 Reply Last reply
      0
      • M Matt Gerrans

        That's because you are passing the thing as an object, instead of whatever type it really is. Your method signature should probably be something like this:

        public bool Add( Agent agent )
        {
        string agentName = agent.agentName;
        ...
        }

        From what you've shown, there is no need to have the ref keyword in there. Also, you don't need Hungarian notation with C#. Matt Gerrans

        F Offline
        F Offline
        faisal khanani
        wrote on last edited by
        #3

        thanks Matt but i can not declare Agent type in this function, because it is defined in data layer that do not have reference of business layer. therefore, i have to declare object type. :)

        G 1 Reply Last reply
        0
        • F faisal khanani

          thanks Matt but i can not declare Agent type in this function, because it is defined in data layer that do not have reference of business layer. therefore, i have to declare object type. :)

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          If you can't reference the class, then you can't do anything with it (unless you use reflection). If you don't want the data layer to know the business class, then make an interface with the properties you want to expose, and make the business class implement it, and reference the interface in the data layer. --- b { font-weight: normal; }

          M 1 Reply Last reply
          0
          • G Guffa

            If you can't reference the class, then you can't do anything with it (unless you use reflection). If you don't want the data layer to know the business class, then make an interface with the properties you want to expose, and make the business class implement it, and reference the interface in the data layer. --- b { font-weight: normal; }

            M Offline
            M Offline
            Matt Gerrans
            wrote on last edited by
            #5

            Guffa's suggestion is probably the way you should go. Another quick-and-dirty option, if Add() only needs a few things, is to use parameters. For example:

            Add( string agentName, int agentId );

            Another one that's even more of a hack is to pass an attribute/value Hashtable. Matt Gerrans

            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