Results 1 to 6 of 6

Thread: Quick Help with variables

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    23

    Quick Help with variables

    How can you make variables global or public? When I use a variable that I declared up in the form's class (public partial class frmMain : Form) it says "strHost does not exist in the current context" the cases match so it can't be a problem with the variable name not matching. Does anyone know what I need to do to the variable? On a side note just another quick question how do you make parameters in a method optional like this vb it's option Referer as string = "" but how would you do that in a C# code?
    Thanks in advance for any help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Quick Help with variables

    There is no such thing as optional method arguments in C#, which is one reason that it's a bad idea to use them in libraries written in VB. You should overload the method name:
    Code:
            public void SomeMethod()
            {
                this.SomeMethod(string.Empty);
            }
    
            public void SomeMethod(string str)
            {
                MessageBox.Show(str);
            }
    As for global variables, you can create a single class, declare it sealed, give it a single private constructor and declare all its members static. This means that you cannot instantiate the class, you cannot inherit the class and you can access all it's members without an instance.
    Code:
        sealed class Globals
        {
            private Globals()
            {
                // Do nothing as the class should not be instantiated.
            }
    
            private static int myVar;
    
            public static int MyProperty
            {
                get { return myVar; }
                set { myVar = value; }
            }	
        }
    This is exactly the same beahviour as a VB module except that with a class you will need to qualify the member name with the class name in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Quick Help with variables

    Quote Originally Posted by jmcilhinney
    There is no such thing as optional method arguments in C#, which is one reason that it's a bad idea to use them in libraries written in VB.
    That's too bad.

    Is it a bad idea though, the .NET framework does support it, so I don't think it should be a problem using it in VB.NET class libraries.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Quick Help with variables

    Quote Originally Posted by mendhak
    That's too bad.

    Is it a bad idea though, the .NET framework does support it, so I don't think it should be a problem using it in VB.NET class libraries.
    If you write a method in VB.NET with optional parameters and then call that method from C#, the fact that the parameters are optional is lost and you must provide a value for each and every parameter. If you use overloading in VB.NET then you get the same behaviour regardless of the language from which the call is made. This isn't really an issue with applications but it may be with libraries.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    23

    Re: Quick Help with variables

    Wow thats great. Too bad for optional parameters though I like to use those in some functions in VB and VB .NET. Im just a beginners in C# Im much better at VB though so some of the things you guys talked about I didn't understand but thanks anyways. Problem solved

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Quick Help with variables

    Don't be afraid to ask, even if it's in the middle of a thread, about a concept you haven't heard of. I know if there's something new to me, I'd definitely ask about it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width