Click here to Skip to main content
15,914,338 members
Home / Discussions / C#
   

C#

 
QuestionASF Files Pin
dotman127-Nov-08 2:57
dotman127-Nov-08 2:57 
Questioncustomizing crystal reports Pin
harcaype27-Nov-08 2:55
harcaype27-Nov-08 2:55 
AnswerRe: customizing crystal reports Pin
Eddy Vluggen27-Nov-08 4:37
professionalEddy Vluggen27-Nov-08 4:37 
GeneralRe: customizing crystal reports Pin
harcaype28-Nov-08 13:40
harcaype28-Nov-08 13:40 
QuestionMaking a class public Pin
Thekaninos27-Nov-08 2:32
Thekaninos27-Nov-08 2:32 
AnswerRe: Making a class public Pin
Simon P Stevens27-Nov-08 2:53
Simon P Stevens27-Nov-08 2:53 
GeneralRe: Making a class public [modified] Pin
Thekaninos27-Nov-08 3:14
Thekaninos27-Nov-08 3:14 
GeneralRe: Making a class public Pin
Simon P Stevens27-Nov-08 3:24
Simon P Stevens27-Nov-08 3:24 
Pass it around as a parameter to a constructor or method, or as a property:
class Program
{
    static void Main(string[] args)
    {
        // Create the connection.
        SqlConnection connection = new SqlConnection("YourConnectionString");

        // Pass it to class A as a parameter to the constructor.
        ClassA a = new ClassA(connection);
        a.DoStuff();

        // You can also pass it to class B as a parameter to a constructor.
        ClassB b = new ClassB(connection);
        b.DoStuff();

        // Or you could pass it to class C as a paramerter to a method.
        ClassC c = new ClassC();
        c.DoStuff(connection);

        // Or you could pass it to class D by setting a property.
        ClassD d = new ClassD();
        d.ConnectionForD = connection;
        d.DoStuff();
    }
}

public class ClassA
{
    private SqlConnection _sharedConnectionInA;

    public ClassA(SqlConnection connection)
    {
        _sharedConnectionInA = connection;
    }

    public void DoStuff()
    {
        _sharedConnectionInA.Open();
        // Do stuff
    }
}

public class ClassB
{
    private SqlConnection _sharedConnectionInB;

    public ClassB(SqlConnection connection)
    {
        _sharedConnectionInB = connection;
    }

    public void DoStuff()
    {
        _sharedConnectionInB.Open();
        // Do stuff
    }
}

public class ClassC
{
    public void DoStuff(SqlConnection connectionForC)
    {
        connectionForC.Open();
        // Do stuff
    }
}

public class ClassD
{
    private SqlConnection _sharedConnectionInD;

    public SqlConnection ConnectionForD
    {
        get
        {
            return _sharedConnectionInD;
        }
        set
        {
            _sharedConnectionInD = value;
        }
    }


    public void DoStuff()
    {
        _sharedConnectionInD.Open();
        // Do stuff
    }
}


Simon

AnswerRe: Making a class public Pin
PIEBALDconsult27-Nov-08 4:10
mvePIEBALDconsult27-Nov-08 4:10 
QuestionLetter Combination Pin
Silvyster27-Nov-08 2:01
Silvyster27-Nov-08 2:01 
AnswerRe: Letter Combination Pin
Simon P Stevens27-Nov-08 3:12
Simon P Stevens27-Nov-08 3:12 
AnswerRe: Letter Combination Pin
Guffa27-Nov-08 3:20
Guffa27-Nov-08 3:20 
QuestionWhy doesnt this function for controls objects work? [modified] Pin
Matjaz-xyz27-Nov-08 1:45
Matjaz-xyz27-Nov-08 1:45 
AnswerRe: Why doesnt this function for creating objects work? Pin
leppie27-Nov-08 1:55
leppie27-Nov-08 1:55 
GeneralRe: Why doesnt this function for creating objects work? Pin
Matjaz-xyz27-Nov-08 2:01
Matjaz-xyz27-Nov-08 2:01 
AnswerRe: Why doesnt this function for creating objects work? Pin
leppie27-Nov-08 1:57
leppie27-Nov-08 1:57 
GeneralRe: Why doesnt this function for creating objects work? Pin
Matjaz-xyz27-Nov-08 2:17
Matjaz-xyz27-Nov-08 2:17 
GeneralRe: Why doesnt this function for creating objects work? Pin
Tom Deketelaere27-Nov-08 2:21
professionalTom Deketelaere27-Nov-08 2:21 
GeneralRe: Why doesnt this function for creating objects work? Pin
Matjaz-xyz27-Nov-08 2:23
Matjaz-xyz27-Nov-08 2:23 
AnswerRe: Why doesnt this function for creating objects work? Pin
Matjaz-xyz27-Nov-08 2:20
Matjaz-xyz27-Nov-08 2:20 
AnswerRe: Why doesnt this function for controls objects work? Pin
Thomas Weller27-Nov-08 2:25
Thomas Weller27-Nov-08 2:25 
QuestionFind Hndlers of an other application Windows Pin
franco nero27-Nov-08 1:40
franco nero27-Nov-08 1:40 
AnswerRe: Find Hndlers of an other application Windows Pin
Giorgi Dalakishvili27-Nov-08 1:55
mentorGiorgi Dalakishvili27-Nov-08 1:55 
QuestionReflection - practical application Pin
devvvy27-Nov-08 1:38
devvvy27-Nov-08 1:38 
AnswerRe: Reflection - practical application Pin
Simon P Stevens27-Nov-08 1:45
Simon P Stevens27-Nov-08 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.