Global Variables

M

Materialised

Hi All,

I am trying the follow the following MSDN tutorial:

Walkthrough: Authoring a Simple Multithreaded Component with Visual C#
(ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclicc/html/7bc03b7b-d680-499b-8179-5f414b2d650c.htm)

The tutorial says:

To create the Calculator component
From the Project menu, select Add Component.

Name the component Calculator.

To add public variables to the Calculator component
Open the Code Editor for Calculator.

Add statements to create public variables that you will use to pass
values from frmCalculations to each thread.

The variable varTotalCalculations will keep a running total of the total
number of calculations performed by the component, and the other
variables will receive values from the form.

Copy Code
public int varAddTwo;
public int varFact1;
public int varFact2;
public int varLoopValue;
public double varTotalCalculations = 0;



I am lost here, where do I create public variables within my component?
The source to my component currently looks as follows:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Calculations
{
public partial class Calculator : Component
{
public Calculator()
{
InitializeComponent();
}

public Calculator(IContainer container)
{
container.Add(this);

InitializeComponent();
}
}
}

Thanks for any help you can offer.
Kind regards.
 
C

Cerebrus

Hi,

I assume this is VS 2005, since I couldn't locate the MSDN article in
my locally installed help.

Public variables are those variables which are accessible even outside
the class. In this case, you need variables that are not within any
method, but still within the class.

So, the place to declare them can be anywhere you want, within the
class, except within any method. Although, the generally accepted place
to declare them is right after your "Designer generated code" ends.

This would mean that you can copy and paste the list of variables right
after the ending bracket of the public Calculator(IContainer container)
method.

In other words :
-----------------------

:
public partial class Calculator : Component
{
:
public Calculator(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public int varAddTwo;
public int varFact1;
public int varFact2;
public int varLoopValue;
public double varTotalCalculations = 0;

:
:
// You can add additional methods and functions below this...

} // end class
} // end namespace



Regards,

Cerebrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top