PDA

Click to See Complete Forum and Search --> : Quick Help with variables


Root Core
Feb 18th, 2006, 12:46 PM
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 :)

jmcilhinney
Feb 18th, 2006, 07:33 PM
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: 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. 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.

mendhak
Feb 20th, 2006, 04:22 AM
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.

jmcilhinney
Feb 20th, 2006, 06:00 AM
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.

Root Core
Feb 20th, 2006, 03:04 PM
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 :)

mendhak
Feb 21st, 2006, 05:47 AM
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.