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. Making a global variable

Making a global variable

Scheduled Pinned Locked Moved C#
questioncsharpdatabasetutorial
8 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.
  • B Offline
    B Offline
    Blekk
    wrote on last edited by
    #1

    Hi, first post on this site. I've just started C# really and have been using some of these tutorials and microsoft's video series. But, I got bored of the video series and started just playing around. I decided, I wanted to create a web browser, so I added the web browser object to my form and a textbox and button to type in the address and initiate the search respectively. Then I decided I wanted to add a bookmark feature, so I added a menuStrip and a Bookmark tab to it. I made it create a new instance of Bookmark.cs which I had created. On the Bookmark.cs form I added a listbox and button. The listbox displayed the data from a database that I had added to the project, I entered some example data and this all worked. But, when I click the button on the Bookmark.cs form, I want it to set the webBrowser1.Navigate() function to the SelectedValue of the listbox on Bookmark.cs. Yet, I cant because webBrowser1 isn't public. What I am really asking is, how do I make webBrowser1 public, so that I can access it from Bookmark.cs? Thanks, Any reply is appreciated.

    C B 2 Replies Last reply
    0
    • B Blekk

      Hi, first post on this site. I've just started C# really and have been using some of these tutorials and microsoft's video series. But, I got bored of the video series and started just playing around. I decided, I wanted to create a web browser, so I added the web browser object to my form and a textbox and button to type in the address and initiate the search respectively. Then I decided I wanted to add a bookmark feature, so I added a menuStrip and a Bookmark tab to it. I made it create a new instance of Bookmark.cs which I had created. On the Bookmark.cs form I added a listbox and button. The listbox displayed the data from a database that I had added to the project, I entered some example data and this all worked. But, when I click the button on the Bookmark.cs form, I want it to set the webBrowser1.Navigate() function to the SelectedValue of the listbox on Bookmark.cs. Yet, I cant because webBrowser1 isn't public. What I am really asking is, how do I make webBrowser1 public, so that I can access it from Bookmark.cs? Thanks, Any reply is appreciated.

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

      The problem is not that it's not public, it's that it's not static, so you need a copy of the form that contains the active control, for you to be able to access it.  This is the wrong way to do it anyhow, you don't want to make the control public, you want to either expose a method that calls the Navigate function, or, better yet, set up a delegate so that your bookmark form can call into your main form and do this for you.

      B 1 Reply Last reply
      0
      • C Christian Graus

        The problem is not that it's not public, it's that it's not static, so you need a copy of the form that contains the active control, for you to be able to access it.  This is the wrong way to do it anyhow, you don't want to make the control public, you want to either expose a method that calls the Navigate function, or, better yet, set up a delegate so that your bookmark form can call into your main form and do this for you.

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

        As I said, I'm a beginner at C# so could you please go into more detail on that because I don't know what you are talking about, except the static bit. What is the right way to do it? Any reply appreciated.

        A C 2 Replies Last reply
        0
        • B Blekk

          As I said, I'm a beginner at C# so could you please go into more detail on that because I don't know what you are talking about, except the static bit. What is the right way to do it? Any reply appreciated.

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          The way i usually do global variables is with a class, like this: internal class Globalvars { internal static int variable; Globalvars(){} } You just mark all the variables as internal static, and you call them like this: Globalvars.variable Just store variables there and access them from somewhere alse later on. You should be able to access them from anywhere. I don't know if this helps but.. there you go.

          1 Reply Last reply
          0
          • B Blekk

            As I said, I'm a beginner at C# so could you please go into more detail on that because I don't know what you are talking about, except the static bit. What is the right way to do it? Any reply appreciated.

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

            Well, the first step is to not create global variables if you can help it. www.codeproject.com/csharp/passdatadelegate.asp would be one example of how to use delegates.  Have the bookmark page define a delegate which is hooked up to a method on the main form, and passes through the URL to browse to.

            1 Reply Last reply
            0
            • B Blekk

              Hi, first post on this site. I've just started C# really and have been using some of these tutorials and microsoft's video series. But, I got bored of the video series and started just playing around. I decided, I wanted to create a web browser, so I added the web browser object to my form and a textbox and button to type in the address and initiate the search respectively. Then I decided I wanted to add a bookmark feature, so I added a menuStrip and a Bookmark tab to it. I made it create a new instance of Bookmark.cs which I had created. On the Bookmark.cs form I added a listbox and button. The listbox displayed the data from a database that I had added to the project, I entered some example data and this all worked. But, when I click the button on the Bookmark.cs form, I want it to set the webBrowser1.Navigate() function to the SelectedValue of the listbox on Bookmark.cs. Yet, I cant because webBrowser1 isn't public. What I am really asking is, how do I make webBrowser1 public, so that I can access it from Bookmark.cs? Thanks, Any reply is appreciated.

              B Offline
              B Offline
              beatles1692
              wrote on last edited by
              #6

              Hi I have a suggestion to make.How about writing a method in you web browser from that gets a bookmark and ask the web browser to nvaigate to it. for example: public void NavigateToBookmark(Bookmark bm) { this.webBrowser1.Navigate(bm.Url); } Anyway,I don't think that declaring the web browser control as public is a good idea,because if you do so,you make your bookmark dependent to a specific webbrowser and that's not good if you want to use the bookmark in some other code.Instead you can make your bookmark holds a reference to a webborwser(not to use a specific webbrowser) or you can pass a webbrowser object to your bookmark public class BookMark { WebBrowser browser; public BookMark(WebBrowser browser) { this.browser=browser; } public void Navigate() { this.browser.Navigate(this.url); } } Or //In BookMark class public void Navigate(WebBrowser browser) { browser.Navigate(this.url); } Regards

              B 2 Replies Last reply
              0
              • B beatles1692

                Hi I have a suggestion to make.How about writing a method in you web browser from that gets a bookmark and ask the web browser to nvaigate to it. for example: public void NavigateToBookmark(Bookmark bm) { this.webBrowser1.Navigate(bm.Url); } Anyway,I don't think that declaring the web browser control as public is a good idea,because if you do so,you make your bookmark dependent to a specific webbrowser and that's not good if you want to use the bookmark in some other code.Instead you can make your bookmark holds a reference to a webborwser(not to use a specific webbrowser) or you can pass a webbrowser object to your bookmark public class BookMark { WebBrowser browser; public BookMark(WebBrowser browser) { this.browser=browser; } public void Navigate() { this.browser.Navigate(this.url); } } Or //In BookMark class public void Navigate(WebBrowser browser) { browser.Navigate(this.url); } Regards

                B Offline
                B Offline
                Blekk
                wrote on last edited by
                #7

                Thanks, I think I will try this, also thanks for the help christian but that link doesn't work. Thanks all.

                1 Reply Last reply
                0
                • B beatles1692

                  Hi I have a suggestion to make.How about writing a method in you web browser from that gets a bookmark and ask the web browser to nvaigate to it. for example: public void NavigateToBookmark(Bookmark bm) { this.webBrowser1.Navigate(bm.Url); } Anyway,I don't think that declaring the web browser control as public is a good idea,because if you do so,you make your bookmark dependent to a specific webbrowser and that's not good if you want to use the bookmark in some other code.Instead you can make your bookmark holds a reference to a webborwser(not to use a specific webbrowser) or you can pass a webbrowser object to your bookmark public class BookMark { WebBrowser browser; public BookMark(WebBrowser browser) { this.browser=browser; } public void Navigate() { this.browser.Navigate(this.url); } } Or //In BookMark class public void Navigate(WebBrowser browser) { browser.Navigate(this.url); } Regards

                  B Offline
                  B Offline
                  Blekk
                  wrote on last edited by
                  #8

                  Ok, I don't know what to do now. I am creating a TextEditor (because I saw it in another post ;P). Anyway, I have created the main form, but when the program starts it displays another form called newDlg.cs It has buttons on, with labels saying "Create a Text File:" or "Create a CSV File:" etc. When the button is clicked, it opens the main form and assigns a variable called fileFormat to 1 or 2 (for Text File and CSV respectively). The fileFormat variable is then used to determine whether the file can be saved as a .txt or a .csv when the user clicks Save. I need to somehow make fileFormat global because it is used in newDlg.cs and Main.cs I tried the method of creating a globalVars.cs class but that didn't work. Anyone got any idea? 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