|
There is no such thing as "global variables" using C# and, IIRC, isn't allowed by the CLI (though you can define global functions, i.e. methods not defined by a class).
Instead, you can use static fields or properties (properties are recommended for added safety since you can check the value being assigned before setting it):
public sealed class Options
{
private Options() {}
private static int option1;
private static string option2;
public static int Option1
{
get { return option1; }
set
{
if (value < 0) throw new ArgumentException();
option1 = value;
}
}
public static string Option2
{
get { return option2; }
set
{
if (value == null) throw new ArgumentNullException();
option2 = value;
}
}
} To set and get these, you simply refer to the static member:
string option1 = Options.Option2;
Options.Option1 = 10; If you're new to C#, you should also read the Visual C# Language[^] documentation in the Visual Studio .NET product documentation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
what Stewart wrote is very good idea. Here i am giving one more little solution.If you want to use some values or variables like connection string and something else that you need entire phase of your application is simply stored in .config file.
.config file (app.config) is an xml file that you can add using Project > Add New Item menu. there choose xml file and rename it as app.config.
in that xml file file you can see somthing like this
<?xml version="1.0" encoding="utf-8" ?>
just add below mentioned code
<configuration>
<appSettings>
<add key="sourcename" value="YourDataSourceName" />
<add key="ConnectionString" value="your connection string " />
</appSettings>
<system.windows.forms jitDebugging="true" />
</configuration>
so finally you will get this much in your app.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="sourcename" value="YourDataSourceName" />
<add key="ConnectionString" value="your connection string " />
</appSettings>
<system.windows.forms jitDebugging="true" />
</configuration>
If you notice the <appsettings> tag then you will find
<add key="sourcename" value="YourDataSourceName" /> here <add key> will give a friendly name to your required data or value. eg. ConnectionString
and <value> is the actual value that you want in your programm.
How you will apply this in your forms or .cs file
-------------------------------------------------------
if you want to access the .config values you have to add a namespace called
using System.Configuration;
retriving value from .config
string Requiredvariable=ConfigurationSettings.AppSettings["ConnectionString"].ToString();
now you can use Requiredvariable to create database connection. you don't want to give the lengthy connection string value again and again in all forms or .cds file. You can reuse the Requiredvariable.
|
|
|
|
|
Hi there ... I have a form with the Web browser control over it covering the whole form. I have a help provider too for the form but pressing F1 doesnt allow the help provider's help file to show . As i understand it, I need to use MSHTML and IDocShowUI interface and implement ShowHelp but all my efforts have failed. Can anyone send me the code to implement it in C#? You can contact me at fayazhsn@hotmail.com .... I need this asap!!
Thanks in advance!!
|
|
|
|
|
See Using MSHTML Advanced Hosting Interfaces[^] for an explanation of how to properly implement the IDocHostUIHandler . When the WebBrwoser control (AxWebBrowser as VS.NET and aximp.exe call it by default) has the focus, it's implementation is queried, which doesn't work like .NET controls would. The COM implementation is what is responding to events in this case.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello. thanks for your prompt reply. I already read that article and also the one which is a chapter from a book by Ted Faison. Ted explains that I need to create a mshtml object to use that. I dont know how to do that. The article you've mentioned also talks abt IDocHostUIHandler . I need info abt IDocHostShowUI & ShowHelp .... How can I show my custom help file (*.chm) when F1 is pressed?
|
|
|
|
|
You implement them and expose your implementation to MSHTML (rather, the aggregate WebBrowser control) the same way. The article I linked shows you how to build a typelib from which you can create an interop assembly. You must do that unless you want to create an RCW manually, which is entirely possible (that's how I typically do it when I need to interop with only a few unmanaged interfaces).
For more information on these interfaces, you should see the topic Programming and Reusing the Browser[^], specifically the section Advanced Hosting Reference[^].
Once you've created an RCW for the IDocHostShowUI interface, you must tell the WebBrowser control that you're it's parent. The article I linked also explains one way to do that. There are several ways, and without hacking the source for the AxWebBrowser control (if you use aximp.exe to create that interop assembly, you can opt to generate source code instead) that's the easiest way to do it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How can a open the *.sln file or *.csporj file made in Professional version of VS.NET with the Enterprise Architect.
Thank you
|
|
|
|
|
Same way you open any other solution or project file.
Especially since the solution was created in a lower level of VS.Net than what you're trying to open it in, there should be no problems.
Is it possible that the solution was created in .Net 1.1 and you're trying to open it with .Net 1.0? that would cause some minor difficulty. But Pro to Enterprise should be no problem at all.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
|
|
|
|
|
I have tried to update VS.NET Enterprise Architect online and i got the message that that my version was the last one. And with all these I can't open the *.sln or *.csproj file.
|
|
|
|
|
First things first, What's the error message?
Second, you CAN, in the Enterprise Architect version, open a project, or any other file, written by the Professional version. But, you CAN'T open a project written in any edition of VS.NET 2003 in VS.NET 2002.
So my questions will be a repeat of the first reply you got...
Which verion of Visual Studio.NET Professional Edition was the project written in, 2002 or 2003?
Which version of Visual Studio.NET Enterprise Edition are you trying to open the project in, 2002 or 2003?
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
The version of my VS.NET is 2002 and the files are downloaded from MS website, so I don't know with what version the files were written. So my question is how can I open the files?
|
|
|
|
|
OK. Last time! WHAT IS THE ERROR?
All you do is double click the .sln or .csproj file. That's it! There is no special trick to getting files to open. If the files were created in VS.NET 2003, you can download and compile this[^] little conversion tool so you can open the project files. THIS IN NO WAY SAYS THAT THE PROJECT IS GOING TO COMPILE WHEN YOU GET IT OPEN!
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
the error message told me that the files were made in the professional version of VS.NET and i couldn't open them with the version i had (Enterprise Architect). I'll try this utility you tell me about and i'll let you know what happens!
thanks a lot!
|
|
|
|
|
the project converter that Dave told me about works fine.
|
|
|
|
|
Hi,
I want the all the nodes in the tree to be expanded. How do I set this property?
Karteek
|
|
|
|
|
Simply call the ExpandAll() method of the TreeView control:
TreeView1.ExpandAll()
I have to ask, have you ever read the documentation on the TreeView and Datagrid controls? Or have even looked through the Intellisense dropdown when you hit "."?
And what's with the "regd" with every question you post?
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Agreed. This forum is for specific questions. You'll never learn anything if you don't actually try to research things - even basic research such as reading the class documentation - if you expect everyone to do the work for you.
I'm a bit confused by "regd" as well. It isn't necessary.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
Actually I have to complete the a project within the next two weeks. It is a big one and I donno much abt C# and .Net. So I post it so that I get the answer without wasting much time. regd I mean regarding. Sorry if I distrubed u with my posts.
Karteek
|
|
|
|
|
Putting "regarding", "re", or "regd" in a subject is pointless: obviously your post relates to whatever the subject says - that's what a subject is for.
It doesn't matter how much time you have to do a project. Not reading about what you're doing is counter-productive and no very smart. You should just jump into something - especially for an important project - without having any idea of what you're doing.
Besides, while your developing your project you should be reading about what you're doing. Not only will you learn while you develop your project, reading may help to make your project better by either providing you with alternative means to perform some task or to make your project more stable.
There's simply no valid excuse why someone can't read even something as simple and quick as class library documentation, at least for the classes the person is using. None.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
karteek wrote:
I donno much abt C# and .Net. So I post it so that I get the answer without wasting much time.
Actually, posting your question and waiting for an answer will take more time than reading the class documentation. It would have taken you about 2 minutes to find the answer to this one just reading the class documentation on the TreeView control.
...and you probably would have learned something else in the process too...
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
hello, i get this error-> System.Data.SqlClient.SqlException: Login failed for user 'xxx\ASPNET'
do some one knows how to solve it?
i am new to sql and i use msde.
thank u in advance
|
|
|
|
|
You basically have three options:
1) Get some SQL Server management tools, and set up login/user permissions in the SQL Server / Database for the ASPNET user.
2) Same as above, but create a separate SQL Login account, and don't use trusted connections to the database.
3) Add the ASPNET user to the Administrators group. This, however, is not at all secure, and can have very serious side-effects.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
|
|
|
|
|
It sounds like your connection string is setup using Integrated Security. Avoid this and use a dedicated username and password for SQL access. You'll also have to setup that username and password for the database using SQLManager.
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Can anybody please help me with dropdownlist.
it must be very simple ... I have this dropdown list which returns me only 1st record .. no matter what i select.
I m using drodownlist1.seletedItem.text to see the value and it's always showing me that i have selected 1st record.
Thanks
|
|
|
|
|
Can you post a bit more of your code so we can see exactly what you are doing?
Pankaj
Without struggle, there is no progress
|
|
|
|