By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,495 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,495 IT Pros & Developers. It's quick & easy.

Declare public variable in C#

P: n/a
I am beginner in using C#, actually I am trying to move from VB6 to C#
I need very small help in programming problem my be you will laugh when you
get it
That simply I have to form let’s say Form1 (main form) and Form2, there is
parameter in form2 I have to called from form1.
So please would you tell me how?

Any way thanks

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

MMSJED wrote:
I am beginner in using C#, actually I am trying to move from VB6 to C#
I need very small help in programming problem my be you will laugh when you
get it
That simply I have to form let's say Form1 (main form) and Form2, there is
parameter in form2 I have to called from form1.
So please would you tell me how?

Any way thanks


assuming you have a class called Form1:

class Form1
{
// declare a public variable in the form.
public int param;
}

class Form2
{
// A method to use the variable in a form1 object
private void myfunction( Form1 f1 )
{
int mynum = f1.param;
}
}

To find the forms from other forms, you can easily pass them
in the constructor.

class Form2
{
Form1 pParent = null;

public Form2( Form1 parent )
{
pParent = parent;
}
private void myfunction ()
{
int mynum = pParent.param;
}
}

Naturally, you want to check for pParent being null and such.

Hope that helps.
Matt

Nov 17 '05 #2

P: n/a
thank you Matt but What I tried is like this
//I call public variable lets say SqlReportPar in Form1

Public string SqlReportPar;

//Then go to Form2
Form test=new Form1();
Test.(the SqlReportPar does not appear) why?????

//What happened?
Form test=new Form1(). //(the SqlReportPar appear here )!!!!!
Test. //(the SqlReportPar does not appear) why?????
"Matt" wrote:

MMSJED wrote:
I am beginner in using C#, actually I am trying to move from VB6 to C#
I need very small help in programming problem my be you will laugh when you
get it
That simply I have to form let's say Form1 (main form) and Form2, there is
parameter in form2 I have to called from form1.
So please would you tell me how?

Any way thanks


assuming you have a class called Form1:

class Form1
{
// declare a public variable in the form.
public int param;
}

class Form2
{
// A method to use the variable in a form1 object
private void myfunction( Form1 f1 )
{
int mynum = f1.param;
}
}

To find the forms from other forms, you can easily pass them
in the constructor.

class Form2
{
Form1 pParent = null;

public Form2( Form1 parent )
{
pParent = parent;
}
private void myfunction ()
{
int mynum = pParent.param;
}
}

Naturally, you want to check for pParent being null and such.

Hope that helps.
Matt

Nov 17 '05 #3

P: n/a
"MMSJED" <MM****@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
thank you Matt but What I tried is like this
//I call public variable lets say SqlReportPar in Form1

Public string SqlReportPar;

//Then go to Form2
Form test=new Form1();
Test.(the SqlReportPar does not appear) why?????

//What happened?
Form test=new Form1(). //(the SqlReportPar appear here )!!!!!
Test. //(the SqlReportPar does not appear) why?????


try this:

Form test = new Form1();
test.SqlReportPar

or

Form Test = new Form1();
Test.SqlReportPar

From your code, you appear to be using "Test" rather than "test". Remember,
C# is case sensitive.

Cletus
Nov 17 '05 #4

P: n/a

MMSJED wrote:
thank you Matt but What I tried is like this
//I call public variable lets say SqlReportPar in Form1

Public string SqlReportPar;

//Then go to Form2
Form test=new Form1();
Test.(the SqlReportPar does not appear) why?????

//What happened?
Form test=new Form1(). //(the SqlReportPar appear here )!!!!!
Test. //(the SqlReportPar does not appear) why?????


Well, assuming that Public is really "public" and "Test" is really
"test"...
the only thing I can think of easily is that the two are not within the
same namespace, thus Form2 does not know about Form1. Otherwise, C# is
VERY case
sensitive, so the cases must match for everything.

Matt

Nov 17 '05 #5

P: n/a
You declared your variable 'test' as type Form when it should have been
type Form1:

//(Watch for typos)

Form1 test = new Form1;
test.SqlReportPar = ......

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.