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. String to Object Instance Problem

String to Object Instance Problem

Scheduled Pinned Locked Moved C#
help
20 Posts 4 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.
  • A atoi_powered

    Hi, I wanted to use these statements to convert a string to an object instance but it gave me this error: ------------------------------- An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll Additional information: Value cannot be null. ------------------------------- I'm fully sure that I wrote the string phrase right but I don't know for what purpose this error comes. Here's the code:

    Type t = Type.GetType("pictureBox99." + "BackgroundImage");
    PictureBox picture = Activator.CreateInstance(t) as PictureBox;
    picture.BackgroundImage = Resource1.island;

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #3

    There's absolutely no need to use Activator.CreateInstance in your code sample. You already know that you are trying to create a PictureBox, so just use new.

    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

    A 1 Reply Last reply
    0
    • P Pete OHanlon

      There's absolutely no need to use Activator.CreateInstance in your code sample. You already know that you are trying to create a PictureBox, so just use new.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      A Offline
      A Offline
      atoi_powered
      wrote on last edited by
      #4

      Yes, I know that the object will be a pictureBox but for some matters in my software's development I need to go through this.

      P 1 Reply Last reply
      0
      • L Lost User

        Debug it and see if "t" is null or not.

        Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

        A Offline
        A Offline
        atoi_powered
        wrote on last edited by
        #5

        Suppose it is null, what happens next? Or clearly, what should I do to stop getting errors?

        1 Reply Last reply
        0
        • A atoi_powered

          Yes, I know that the object will be a pictureBox but for some matters in my software's development I need to go through this.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #6

          Well, the problem in your code is that you are retrieving the type of BackgroundImage - which is Image, and then attempting to cast that into a PictureBox. You can't do this - hence the reason that the value of picture is null. In your sample, you really need to do this:

          PictureBox picture = new PictureBox();
          picture.BackgroundImage = Resource1.island;

          You will also need to place this control on the screen (and add it to the Controls collection of its parent).

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            Well, the problem in your code is that you are retrieving the type of BackgroundImage - which is Image, and then attempting to cast that into a PictureBox. You can't do this - hence the reason that the value of picture is null. In your sample, you really need to do this:

            PictureBox picture = new PictureBox();
            picture.BackgroundImage = Resource1.island;

            You will also need to place this control on the screen (and add it to the Controls collection of its parent).

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            A Offline
            A Offline
            atoi_powered
            wrote on last edited by
            #7

            Here's the real problem: I have 81 pictureBoxes and I want to change their BackgroundImage at once. I'm looking for a solution to stay away from writing 81 statements. I need a code snippet to do so.

            P 1 Reply Last reply
            0
            • A atoi_powered

              Here's the real problem: I have 81 pictureBoxes and I want to change their BackgroundImage at once. I'm looking for a solution to stay away from writing 81 statements. I need a code snippet to do so.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #8

              Are you trying to convert them into the same image?

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              A 1 Reply Last reply
              0
              • P Pete OHanlon

                Are you trying to convert them into the same image?

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                A Offline
                A Offline
                atoi_powered
                wrote on last edited by
                #9

                Yes. I'm trying to reach something like this:

                for (Counter = 1; Counter <= 81; Counter++)
                {
                picturebox|COUNTER|.BackgroundImage = Resource1.Img1;
                }

                P 1 Reply Last reply
                0
                • A atoi_powered

                  Yes. I'm trying to reach something like this:

                  for (Counter = 1; Counter <= 81; Counter++)
                  {
                  picturebox|COUNTER|.BackgroundImage = Resource1.Img1;
                  }

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #10

                  Off the top of my head, I would possibly use something like this:

                  private void ChangePictureBoxes(Control parentControl, Image img)
                  {
                  foreach (Control control in parentControl.Children)
                  {
                  PictureBox picture = control as PictureBox;
                  if (picture != null)
                  {
                  picture.BackgroundImage = img;
                  continue;
                  }
                  // Loop through the children - just in case the pictureboxes
                  // are in other control containers.
                  ChangePictureBoxes(control);
                  }
                  }

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                  A B 2 Replies Last reply
                  0
                  • P Pete OHanlon

                    Off the top of my head, I would possibly use something like this:

                    private void ChangePictureBoxes(Control parentControl, Image img)
                    {
                    foreach (Control control in parentControl.Children)
                    {
                    PictureBox picture = control as PictureBox;
                    if (picture != null)
                    {
                    picture.BackgroundImage = img;
                    continue;
                    }
                    // Loop through the children - just in case the pictureboxes
                    // are in other control containers.
                    ChangePictureBoxes(control);
                    }
                    }

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    A Offline
                    A Offline
                    atoi_powered
                    wrote on last edited by
                    #11

                    The Visual Studio says " ~Control does not contain a definition for Children"!?

                    P 1 Reply Last reply
                    0
                    • A atoi_powered

                      Hi, I wanted to use these statements to convert a string to an object instance but it gave me this error: ------------------------------- An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll Additional information: Value cannot be null. ------------------------------- I'm fully sure that I wrote the string phrase right but I don't know for what purpose this error comes. Here's the code:

                      Type t = Type.GetType("pictureBox99." + "BackgroundImage");
                      PictureBox picture = Activator.CreateInstance(t) as PictureBox;
                      picture.BackgroundImage = Resource1.island;

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #12

                      Okay, I've read this whole thread and you're horribly confused about classes and instances. Your user name indicates you come from non-class-based procedural languages, and perhaps you need to keep the Object Oriented chapter of your C# book open while you learn! The failed code that you are trying here is trying to find a type, and create a new instance of that type. Activator.CreateInstance(sometype) is logically equivalent to new sometype(), and in fact the snippet you posted can be written exactly as

                      PictureBox picture = new pictureBox99.BackgroundImage();
                      picture.BackgroundImage = Resource1.island;

                      Hopefully you can see why that doesn't work! You are not trying to create a copy of something whose final type is not known at compile time, which is about the only time I've needed to use reflection based instantiation. No, what you want to do is find all the instances and do the same thing to them:

                      foreach(Control c in Controls){
                      PictureBox picture = c as PictureBox;
                      if(picture != null)
                      picture.BackgroundImage = Resource1.island;
                      }

                      or with Linq:

                      this.Controls.Select(c => c as PictureBox).Where(c => c != null).ToList().ForEach(pb => pb.BackgroundImage = Resource1.island);

                      (the ToList being necessary because ForEach isn't defined except on Lists).

                      A 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        Off the top of my head, I would possibly use something like this:

                        private void ChangePictureBoxes(Control parentControl, Image img)
                        {
                        foreach (Control control in parentControl.Children)
                        {
                        PictureBox picture = control as PictureBox;
                        if (picture != null)
                        {
                        picture.BackgroundImage = img;
                        continue;
                        }
                        // Loop through the children - just in case the pictureboxes
                        // are in other control containers.
                        ChangePictureBoxes(control);
                        }
                        }

                        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #13

                        It's Controls[^], not Children, in .Net. But yes, this is roughly what I just posted.

                        P 1 Reply Last reply
                        0
                        • B BobJanova

                          Okay, I've read this whole thread and you're horribly confused about classes and instances. Your user name indicates you come from non-class-based procedural languages, and perhaps you need to keep the Object Oriented chapter of your C# book open while you learn! The failed code that you are trying here is trying to find a type, and create a new instance of that type. Activator.CreateInstance(sometype) is logically equivalent to new sometype(), and in fact the snippet you posted can be written exactly as

                          PictureBox picture = new pictureBox99.BackgroundImage();
                          picture.BackgroundImage = Resource1.island;

                          Hopefully you can see why that doesn't work! You are not trying to create a copy of something whose final type is not known at compile time, which is about the only time I've needed to use reflection based instantiation. No, what you want to do is find all the instances and do the same thing to them:

                          foreach(Control c in Controls){
                          PictureBox picture = c as PictureBox;
                          if(picture != null)
                          picture.BackgroundImage = Resource1.island;
                          }

                          or with Linq:

                          this.Controls.Select(c => c as PictureBox).Where(c => c != null).ToList().ForEach(pb => pb.BackgroundImage = Resource1.island);

                          (the ToList being necessary because ForEach isn't defined except on Lists).

                          A Offline
                          A Offline
                          atoi_powered
                          wrote on last edited by
                          #14

                          look here:

                          private void ResetPictureBoxes()
                          {
                          pictureBox9.BackgroundImage = Resource1.sea;
                          pictureBox40.BackgroundImage = Resource1.sea;
                          pictureBox1.BackgroundImage = Resource1.sea;
                          pictureBox100.BackgroundImage = Resource1.sea;
                          pictureBox101.BackgroundImage = Resource1.sea;
                          pictureBox102.BackgroundImage = Resource1.sea;
                          pictureBox103.BackgroundImage = Resource1.sea;
                          pictureBox104.BackgroundImage = Resource1.sea;
                          pictureBox105.BackgroundImage = Resource1.sea;
                          pictureBox13.BackgroundImage = Resource1.sea;
                          pictureBox14.BackgroundImage = Resource1.sea;
                          pictureBox15.BackgroundImage = Resource1.sea;
                          pictureBox16.BackgroundImage = Resource1.sea;
                          pictureBox17.BackgroundImage = Resource1.sea;
                          pictureBox18.BackgroundImage = Resource1.sea;
                          pictureBox19.BackgroundImage = Resource1.sea;
                          pictureBox2.BackgroundImage = Resource1.sea;
                          pictureBox20.BackgroundImage = Resource1.sea;
                          pictureBox21.BackgroundImage = Resource1.sea;
                          // AND SO ON
                          }

                          I try your code snippet, too but it didn't work.

                          private void ResetPictureBoxes()
                          {
                          foreach (Control c in this.Controls)
                          {

                                      PictureBox picture = c as PictureBox;
                                      if (picture != null)
                                          picture.BackgroundImage = Resource1.island;
                                  }
                              }
                          
                          B 1 Reply Last reply
                          0
                          • B BobJanova

                            It's Controls[^], not Children, in .Net. But yes, this is roughly what I just posted.

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #15

                            Thanks - I just knocked this up in the CP editor.

                            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                            1 Reply Last reply
                            0
                            • A atoi_powered

                              The Visual Studio says " ~Control does not contain a definition for Children"!?

                              P Offline
                              P Offline
                              Pete OHanlon
                              wrote on last edited by
                              #16

                              Replace Children with Controls. I just knocked this snippet up in the CP editor.

                              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                              A 1 Reply Last reply
                              0
                              • P Pete OHanlon

                                Replace Children with Controls. I just knocked this snippet up in the CP editor.

                                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                A Offline
                                A Offline
                                atoi_powered
                                wrote on last edited by
                                #17

                                Mission Done! Thanks a lot :) :)

                                P 1 Reply Last reply
                                0
                                • A atoi_powered

                                  Mission Done! Thanks a lot :) :)

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #18

                                  No problem. The key to this trick is the fact that your control could be nested inside any one of a number of control containers - this recursive method is a handy way to iterate over a form and find ALL instances of a particular type.

                                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                  1 Reply Last reply
                                  0
                                  • A atoi_powered

                                    look here:

                                    private void ResetPictureBoxes()
                                    {
                                    pictureBox9.BackgroundImage = Resource1.sea;
                                    pictureBox40.BackgroundImage = Resource1.sea;
                                    pictureBox1.BackgroundImage = Resource1.sea;
                                    pictureBox100.BackgroundImage = Resource1.sea;
                                    pictureBox101.BackgroundImage = Resource1.sea;
                                    pictureBox102.BackgroundImage = Resource1.sea;
                                    pictureBox103.BackgroundImage = Resource1.sea;
                                    pictureBox104.BackgroundImage = Resource1.sea;
                                    pictureBox105.BackgroundImage = Resource1.sea;
                                    pictureBox13.BackgroundImage = Resource1.sea;
                                    pictureBox14.BackgroundImage = Resource1.sea;
                                    pictureBox15.BackgroundImage = Resource1.sea;
                                    pictureBox16.BackgroundImage = Resource1.sea;
                                    pictureBox17.BackgroundImage = Resource1.sea;
                                    pictureBox18.BackgroundImage = Resource1.sea;
                                    pictureBox19.BackgroundImage = Resource1.sea;
                                    pictureBox2.BackgroundImage = Resource1.sea;
                                    pictureBox20.BackgroundImage = Resource1.sea;
                                    pictureBox21.BackgroundImage = Resource1.sea;
                                    // AND SO ON
                                    }

                                    I try your code snippet, too but it didn't work.

                                    private void ResetPictureBoxes()
                                    {
                                    foreach (Control c in this.Controls)
                                    {

                                                PictureBox picture = c as PictureBox;
                                                if (picture != null)
                                                    picture.BackgroundImage = Resource1.island;
                                            }
                                        }
                                    
                                    B Offline
                                    B Offline
                                    BobJanova
                                    wrote on last edited by
                                    #19

                                    Define 'didn't work'. Are all your picture boxes children of the same control? If so, you should use 'parentOfPBs.Controls' not 'this.Controls'. If not, you will have to traverse the whole control tree; I suggest you do this once and stash them in a list as it can be quite slow on a big form. You can use a standard recursive tree traversal similar to

                                    public static List<T> GetControlsInTree<T>(Control parent) where T: Control {
                                    List<T> result = new List<T>
                                    AddControlsInTree(parent, result);
                                    return result;
                                    }

                                    static void AddControlsInTree<T>(Control parent, List<T> list) where T: Control {
                                    foreach (Control c in parent.Controls){
                                    T t = c as T;
                                    if (t != null) list.Add(t);
                                    AddControlsInTree(c, list);
                                    }
                                    }

                                    You can stash the list of picture boxes after form initialisation (after you call InitializeComponent in the constructor, for example):

                                    List<PictureBox> pictures = GetControlsInTree<PictureBox>(this);

                                    Then you can iterate over them at will:

                                    private void ResetPictureBoxes()
                                    foreach(PictureBox picture in pictures)
                                    picture.BackgroundImage = Resource1.island;
                                    }

                                    A 1 Reply Last reply
                                    0
                                    • B BobJanova

                                      Define 'didn't work'. Are all your picture boxes children of the same control? If so, you should use 'parentOfPBs.Controls' not 'this.Controls'. If not, you will have to traverse the whole control tree; I suggest you do this once and stash them in a list as it can be quite slow on a big form. You can use a standard recursive tree traversal similar to

                                      public static List<T> GetControlsInTree<T>(Control parent) where T: Control {
                                      List<T> result = new List<T>
                                      AddControlsInTree(parent, result);
                                      return result;
                                      }

                                      static void AddControlsInTree<T>(Control parent, List<T> list) where T: Control {
                                      foreach (Control c in parent.Controls){
                                      T t = c as T;
                                      if (t != null) list.Add(t);
                                      AddControlsInTree(c, list);
                                      }
                                      }

                                      You can stash the list of picture boxes after form initialisation (after you call InitializeComponent in the constructor, for example):

                                      List<PictureBox> pictures = GetControlsInTree<PictureBox>(this);

                                      Then you can iterate over them at will:

                                      private void ResetPictureBoxes()
                                      foreach(PictureBox picture in pictures)
                                      picture.BackgroundImage = Resource1.island;
                                      }

                                      A Offline
                                      A Offline
                                      atoi_powered
                                      wrote on last edited by
                                      #20

                                      Thanks :)

                                      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