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. object initialization

object initialization

Scheduled Pinned Locked Moved C#
question
5 Posts 2 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.
  • B Offline
    B Offline
    bidisha_tina
    wrote on last edited by
    #1

    I have a class myclass{public int a;} and a string str="a". I want to set myclassobject.a=1 as myclassobject.str=1.How is it possible? } bidisha

    C 1 Reply Last reply
    0
    • B bidisha_tina

      I have a class myclass{public int a;} and a string str="a". I want to set myclassobject.a=1 as myclassobject.str=1.How is it possible? } bidisha

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

      Your class is:

      class myclass
      {
      public int a;
      }

      There is no definition of str in your class. You cannot set myclassobject.str = 1; What is it you actually want to do? To what purpose do you want to set str=1 in a class that contains no definition of str? Also, on a matter of coding guides, you should not make fields public like this. You should encapsulate them in a property. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

      B 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Your class is:

        class myclass
        {
        public int a;
        }

        There is no definition of str in your class. You cannot set myclassobject.str = 1; What is it you actually want to do? To what purpose do you want to set str=1 in a class that contains no definition of str? Also, on a matter of coding guides, you should not make fields public like this. You should encapsulate them in a property. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

        B Offline
        B Offline
        bidisha_tina
        wrote on last edited by
        #3

        Actually I have a xsd file from which I autogenerate a .cs file containing all class definations.I need to change my xsd often,so the members of the class often changes.And I want to initialise the members at run time whose name I can specify through a string str.suppose I set str="call".Now I want to set myobject.call=value.Please help. bidisha

        C 1 Reply Last reply
        0
        • B bidisha_tina

          Actually I have a xsd file from which I autogenerate a .cs file containing all class definations.I need to change my xsd often,so the members of the class often changes.And I want to initialise the members at run time whose name I can specify through a string str.suppose I set str="call".Now I want to set myobject.call=value.Please help. bidisha

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

          Then you need to have a look at some of the stuff in the System.Reflection namespace. Start with typeof(MyClass) which returns a Type object and from there you can enumerate all sorts of things like the properties in a class, you can then invoke them in order to set the value of the property and so on. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          B 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Then you need to have a look at some of the stuff in the System.Reflection namespace. Start with typeof(MyClass) which returns a Type object and from there you can enumerate all sorts of things like the properties in a class, you can then invoke them in order to set the value of the property and so on. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

            B Offline
            B Offline
            bidisha_tina
            wrote on last edited by
            #5

            I have used FieldInfo.setValue to set the fields where FieldInfo.Name=my_specified_name. I have no problem with setting the field value with primitive datatype such as string or int.But some field are of user defined datatype.For example, I have a field as Call mycall; where public class Call { string calltime; string callvalue; } I cant reach to mycall.calltime using FieldInfo. bidisha

            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