473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with c# variables

How can i use a variable from within a different function in which it was
not set.
Like the example.

using System;

namespace test
{
class class1
{
static void Main()
{
string name = "bob";
start();
}

static void start()
{
Console.WriteLine(name);
}
}
}
Feb 10 '06 #1
5 946
Lambert Symth <la******@googlemail.co.uk> wrote:
How can i use a variable from within a different function in which it was
not set.
Like the example.

using System;

namespace test
{
class class1
{
static void Main()
{
string name = "bob";
start();
}

static void start()
{
Console.WriteLine(name);
}
}
}


Currently the variable is a local variable - it's only available within
the method. You need to declare it as a static variable:

using System;

namespace test
{
class class1
{
static string name;

static void Main()
{
name = "bob";
start();
}

static void start()
{
Console.WriteLine(name);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 10 '06 #2

You could pass it as a parameter like this

using System;

namespace test
{
class class1
{
static void Main()
{
string name = "bob";
start(name);
}

static void start(string name)
{
Console.WriteLine(name);
}
}
}
Feb 10 '06 #3
Lambert Symth wrote:
How can i use a variable from within a different function in which it was
not set.
Like the example.

using System;

namespace test
{
class class1
{
public string Name=string.empty;
static void Main()
{
string Name = "bob";
start();
}

static void start()
{
Console.WriteLine(Name);
}
}
}

Feb 10 '06 #4
John Bailo <ja*****@texeme.com> wrote:
Lambert Symth wrote:
How can i use a variable from within a different function in which it was
not set.
Like the example.

using System;

namespace test
{
class class1
{


public string Name=string.empty;
static void Main()
{
string Name = "bob";
start();
}

static void start()
{
Console.WriteLine(Name);
}
}
}


That still won't compile (start is static) and even if it did, it would
print out an empty string rather than bob, as the local variable would
shadow the static/instance one.

In addition, your variable casing is somewhat odd (why change the OP's
variable name?) and variables shouldn't be public...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #5
"John Bailo" <ja*****@texeme.com> wrote in message
news:43**************@texeme.com...
Lambert Symth wrote:
How can i use a variable from within a different function in which it was
not set.
Like the example.

using System;

namespace test
{
class class1
{


public string Name=string.empty;
static void Main()
{
string Name = "bob";
start();
}

static void start()
{
Console.WriteLine(Name);
}
}
}
Almost. If you get rid of the double declaration, this would likely work.
Replace this:
string Name = "bob";
with this
Name = "bob";


(I'm guessing that this was your intent.)

Also, the variable could be declared 'private' instead. That would be
better data hiding.

To the OP: the best solution is to pass the variable as a parameter.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Feb 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
Hello, Can anyone help with this? On my online order form, I need to send a few variables to my credit-card processor. These variables are for non-secure customer comments, and will be sent...
1
by: Dave Smithz | last post by:
Hi there, I have implemented a login sequence using session variables for a PHP/MySQL DB which I took over development. I read some books and this NG and it seemed straight forward. However the...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
6
by: karafire2003 | last post by:
Hey there i was wondering if someone can help me with my program. I thought i had it down...but i'm having some problems. Here's the assignment. Write a C function named change() that accepts a...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.