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. local/global variables?

local/global variables?

Scheduled Pinned Locked Moved C#
helpquestion
6 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.
  • H Offline
    H Offline
    Heks
    wrote on last edited by
    #1

    =
    f.Name; x += 1; } } ------------------------------------------------------------- if ( sExt == "zip") { OpenZip(sFull); ShowImage(num); } How can I access the zip instance from the ShowImage() function? :-O Thx for the help.:)

    G 1 Reply Last reply
    0
    • H Heks

      =
      f.Name; x += 1; } } ------------------------------------------------------------- if ( sExt == "zip") { OpenZip(sFull); ShowImage(num); } How can I access the zip instance from the ShowImage() function? :-O Thx for the help.:)

      G Offline
      G Offline
      Gary Thom
      wrote on last edited by
      #2

      you could try using the singleton pattern. Have an external singleton class hold the instance of open zip file, access the instance from both functions.

      class ZipHolder
      {
         private ZipHolder()
         {
         }
      
         private ZipArchive zip = null;
      
         public ZipHolder Instance = new ZipHolder();
      
         public void Open(string name)
         {
           zip = new ZipArchive(name);
           ....
         }
      
         public ZipArchive Zip
         {
             get { return zip; }
         }
      }
      

      you can then access the Open thus: ZipHolder.Instance.Open("myzip.zip"); and the open zip file by ZipHolder.Instance.Zip. Gary

      H 1 Reply Last reply
      0
      • G Gary Thom

        you could try using the singleton pattern. Have an external singleton class hold the instance of open zip file, access the instance from both functions.

        class ZipHolder
        {
           private ZipHolder()
           {
           }
        
           private ZipArchive zip = null;
        
           public ZipHolder Instance = new ZipHolder();
        
           public void Open(string name)
           {
             zip = new ZipArchive(name);
             ....
           }
        
           public ZipArchive Zip
           {
               get { return zip; }
           }
        }
        

        you can then access the Open thus: ZipHolder.Instance.Open("myzip.zip"); and the open zip file by ZipHolder.Instance.Zip. Gary

        H Offline
        H Offline
        Heks
        wrote on last edited by
        #3

        Thanks for the help :D However...when I try using the code I get this error: 'mrmanga.ZipHolder.ZipHolder()' is inaccessible due to its protection level Any idea? --- edit --- Also, when i try making ZipHolder() public, I get this: An unhandled exception of type 'System.StackOverflowException' occurred in mrmanga.exe

        G 1 Reply Last reply
        0
        • H Heks

          Thanks for the help :D However...when I try using the code I get this error: 'mrmanga.ZipHolder.ZipHolder()' is inaccessible due to its protection level Any idea? --- edit --- Also, when i try making ZipHolder() public, I get this: An unhandled exception of type 'System.StackOverflowException' occurred in mrmanga.exe

          G Offline
          G Offline
          Gary Thom
          wrote on last edited by
          #4

          Hi, Sorry I wasn't clear, and I think it lost some of the information I added in a tag. Ok. To open the zip file ZipHolder.Instance.Open("zipfile.zip"); To access the instance of ZipArchive (held inside the ZipHolder)

          public void ShowImage(int theNum)
          {
              AbstractFile imageFile = ZipHolder.Instance.Zip.GetFile(zipFiles[num]);
              if (imageFile.Exists)
              {
                  using( Stream stImage = imageFile.OpenRead() )
                  {
                       pbMain.Image = Image.FromStream(stImage);
                  }
              }
          }
          

          etc.. Gary

          H 1 Reply Last reply
          0
          • G Gary Thom

            Hi, Sorry I wasn't clear, and I think it lost some of the information I added in a tag. Ok. To open the zip file ZipHolder.Instance.Open("zipfile.zip"); To access the instance of ZipArchive (held inside the ZipHolder)

            public void ShowImage(int theNum)
            {
                AbstractFile imageFile = ZipHolder.Instance.Zip.GetFile(zipFiles[num]);
                if (imageFile.Exists)
                {
                    using( Stream stImage = imageFile.OpenRead() )
                    {
                         pbMain.Image = Image.FromStream(stImage);
                    }
                }
            }
            

            etc.. Gary

            H Offline
            H Offline
            Heks
            wrote on last edited by
            #5

            :D! Ahh worked like a charm! Took me a little modding (had to throw in a static, and change string to abstractfile) but sure enough, it works :) I can't thank you enough. Thanks ^^

            G 1 Reply Last reply
            0
            • H Heks

              :D! Ahh worked like a charm! Took me a little modding (had to throw in a static, and change string to abstractfile) but sure enough, it works :) I can't thank you enough. Thanks ^^

              G Offline
              G Offline
              Gary Thom
              wrote on last edited by
              #6

              :doh: I write these every day and still forgot the static in the example :-O Gary

              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