How do I call string a defined outside of the class ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using  static2 ;

namespace static1
{
    class Program
    {

        string a = "dsfsdf";
        static void Main(string[] args)
        {

            Automobile.Drive();
            int i = Automobile.NumberOfWheels;



           // Automobile car = new Automobile.compute(2,3);


            //Console.Write(car);

            Console.WriteLine(Program.a);

        }
    }
}

Recommended Answers

Can you rephrase your question? What string do you want to be able to access and in which class?

Jump to Post

You'd need to make it static, or if it never changes, const

Jump to Post

All 5 Replies

Can you rephrase your question? What string do you want to be able to access and in which class?

You'd need to make it static, or if it never changes, const

Change line 28 to Console.WriteLine(a); or Console.WriteLine(this.a);

I just re-read your question. I'm confused by what you're asking. String "a" isn't defined outside of the class. It is a global variable--which is defined outside of Main. If you are trying to reference it within class "Program" do it as in my previous post.

You could potentially reference it from another class by making it "public static" as stated in a post above. You could also just make it public.

You can create a public property instead of a variable of type string and put that in a public class, then you inherit that class into the one you are using and can therefore, easily access that property's value.
Hope that helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.