Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old March 29th, 2004, 05:43 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Dear Imar tnx again for ur response again,
So I made a componenct included a sqlConnection Obj.
then I define it in my form like
Code:
protected Component1 DabConn = new Component1();
Now in run time I can see the value of DabConn.sqlConnection1.ConnectionString but in design time I can not set it.

Im waiting, Tnx.

Always:),
Hovik Melkomian.
Reply With Quote
  #12 (permalink)  
Old March 29th, 2004, 05:51 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Hovik ,

You're still not really providing enough information. What kind of project is the component? How does the code look (for the component in general, and for the ConnectionString property in particular)? How are you consuming the component in your host application? Did you compile the project to a DLL? How is your host application organised? Did you add a reference to the DLL or are you using project references?

Are you saying that Intelli Sense is not working, or do you have other problems? Can you see the ConnectionString in the Property Grid? If you can't see it, can you see it in code with Intelli Sense?

It would be really useful if you'd provide more background about your problem, because otherwise I cannot be of much help.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #13 (permalink)  
Old March 29th, 2004, 06:20 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Ok sorry for my useless info. lets start again.
Now what I have is: a Windows C# App. using MS-SQL 2000 as DB.
& what I want is: to make a connection string for all Forms & ...
I mean I wanna to have a Connection string to be used for all my connection Obj.
Let me say that app. will genrate the connection string thats why I wanna use it, otherwise I'd use [u]Dynamic proerty.</u>
I dont know if I was give the all info u wanted or not, I'll glad to give more info if its need again, & wait for ur messages also.:D

Always:),
Hovik Melkomian.
Reply With Quote
  #14 (permalink)  
Old March 29th, 2004, 06:45 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Well, if all you need is a global ConnectionString, why not just define a public property?
Code:
private string connectionString = "Bla bla bla, your ConnectionString here";
public string ConnectionString()
{
  get
  {
    return connectionString;
  }
}
This way, the entire application can access the Connection String.

But somehow, I don't think this is the answer to your question, but that's because it's still unclear to me what the question is. I fired a number of questions back at you, requesting more information about the problem. Is any of the questions appropriate or not? If so, maybe you can answer a few of them so it becomes clear what you want. You talked about a Component, you talked about that you can't see the ConnectionString at design-time (either in Intelli Sense or in the Properties grid???). But you don't show the code, you don't tells us how your projects are related, you don't tell us what kind of projects (in VS.NET) you created and how they are related.

Without that information, I cannot give you a useful answer. Someone else may, but I doubt that. All I can do is ask for more information.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #15 (permalink)  
Old March 29th, 2004, 07:25 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Dear Imar:
I answered to ur questions in previous message. I told that I have a Windows Application & ...
Well when I say I cant see connection string in design time I meant that I cant see it from another form not in the component from.
Anyway, let me say in more detail now.
As I said before Im working with C# Windows Application into VS.NET.
I use MS-SQL Server as DataBase. I gentrate Connection String myself in App. (when User login) then I use this [u]Connection sting</u> in my whole app. this is my scenario & excuse me if I was not clear before!
Then u told to use connection string as
Code:
private string connectionString = "Bla bla bla, your ConnectionString here";
public string ConnectionString()
{
  get
  {
    return connectionString;
  }
}
but u didnt mentioned where should I define it & how could I use it!
I used it before but since I had to new that from & it was renewing (
Code:
mainform mf = new mainform();
)it has no value, anyway maybe I was wronge, I dont know.
u ask me to write my code here, there is no problem but if my code was working I didnt need it, my code is not correct thats why im here:D.
Still wait for ur answer;)

Always:),
Hovik Melkomian.
Reply With Quote
  #16 (permalink)  
Old March 29th, 2004, 07:39 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:I answered to ur questions in previous message. I told that I have a Windows Application & ...
Right, but I also asked this:
  • What kind of project is the component?
  • How does the code look (for the component in general, and for the ConnectionString property in particular)?
  • How are you consuming the component in your host application?
  • Did you compile the project to a DLL?
  • How is your host application organised?
  • Did you add a reference to the DLL or are you using project references?
  • Are you saying that Intelli Sense is not working, or do you have other problems?
  • Can you see the ConnectionString in the Property Grid?
  • If you can't see it, can you see it in code with Intelli Sense?
Quote:
quote:u ask me to write my code here, there is no problem but if my code was working I didnt need it, my code is not correct thats why im here
Now I could be missing something, but isn't that the whole point? If your code *was* working, you indeed didn't need to be here. But since it is *not* working, doesn't it make sense to post the part that doesn't work so we can take a look at it and suggest a fix?

Anyway, I think you need to do something like this (the general idea was already mentioned in this tread by Olorin) to implement a global connection string:

1. Create a new class called Globals for example. Add the class to your Windows Application
2. Add the following code:
Code:
namespace myProject
{
  public class Globals
  {
    private string connectionString = "Bla bla bla, your ConnectionString here";
    public string ConnectionString()
    {
      get
      {
        return connectionString;
      }
    }
  }
}
If you want to change the ConnectionString at run-time, also provide a set method, so calling code can change the connection string.

Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #17 (permalink)  
Old March 29th, 2004, 08:12 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I used it before, but didnt succeed. Where is my problem.
I add a class in my project & called it globals as follow:
Code:
namespace WinApp_Test
{
    public class globals
    {
        private string connectionString = "Bla bla bla, your ConnectionString here";
        public string ConnectionString
        {
            set
            {
                connectionString = value;
            }
            get
            {
                return connectionString;
            }
        }
    }
}
Then in my main from (start up) i set as follow:
Code:
        public globals gl = new globals();

        public main()
        {
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            gl.ConnectionString = "Server is Hovik.";
        }

now how can I set & get its value from onter forms?! [u]This</u> is my probelm
Shuld I have to new it again as
Code:
public globals gl = new globals();
Plz help

Always:),
Hovik Melkomian.
Reply With Quote
  #18 (permalink)  
Old March 29th, 2004, 08:26 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Ah code. And all of a sudden, everything becomes much clearer ;)

The way you have set it up now, each time the form loads, you get a new instance of the Globals class. So, each time you set the ConnectionString, it is set only for that instance, but not globally. As soon as the instance goes out of scope, the ConnectionString property is destroyed with it. What you need is a static (Shared in Visual Basic) property.
A static property is guaranteed to be created for a class only once. This means you can access and change it from anywhere in your application, and get the same value every time. With a static property or method, there is no need to instantiate an object of the Global class; you can access the static members using ClassName.MemberName, instead of InstanceName.MemberName.

Try this instead:
Code:
namespace WinApp_Test
{
    public class Globals
    {
        private string connectionString = "Bla bla bla, your ConnectionString here";
        public static string ConnectionString
        {
            set
            {
                connectionString = value;
            }
            get
            {
                return connectionString;
            }
        }
        private Globals()
        {
        }
    }
By making the class constructor private, calling code cannot instantiate a new object of the Globals class using new Globals(). All that the calling code can do is access the static members:
Code:
public main()
{
  InitializeComponent();
  Globals.ConnectionString = "Server is Hovik.";
}
Notice that I used Globals.ConnectionString instead of gl.ConnectionString. You need to use the class name to access the static members and no longer an instance of that class.

Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #19 (permalink)  
Old March 29th, 2004, 08:45 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Dear Imar tnx for ur aswer! im going to leave office for today. plz give me time to work on it & tell u the result.
u had helped me much till now!;)

Always:),
Hovik Melkomian.
Reply With Quote
  #20 (permalink)  
Old March 29th, 2004, 04:58 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Note that in order for a static property to access the internal class field, that field must also be static:
Code:
    public class Globals
    {
        private static string connectionString = "Bla bla bla, your ConnectionString here";
        public static string ConnectionString
        {
            set
            {
                connectionString = value;
            }
            get
            {
                return connectionString;
            }
        }
        private Globals()
        {
        }
    }
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Global Variable techfreak123 XSLT 0 August 3rd, 2006 07:45 AM
global variable? milk_vanilla Classic ASP Basics 3 January 8th, 2005 06:50 PM
global variable in c# kobystud C# 3 April 22nd, 2004 05:56 AM



All times are GMT -4. The time now is 10:19 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.