Javatpoint Logo

About declaring a variable

By: saurav*** On: Tue May 02 18:29:58 IST 2017     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
Respected sir / Madam,
I am very confused about declaration of any variable. From simplest program like printing any String value to any complex program, I cannot understand which variables should be declared outside the main function and which should be inside. It is showing "non-static variable cannot be referenced from a static context...".Please suggest me.
Thanks and Regards,
Saurav Ray.
Up0Down

 
//Our main method is static so in order to call any non static method
//from main method either need to create object of class or make variable or method as static

//creating object of class we can handle non static variable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
public class Program
{
string b="test";

public void m1()
{
Console.WriteLine("non static method");
}

public static void Main(string[] args)
{
Program a1 = new Program();
a1.m1();

Console.WriteLine(a1.b);
Console.ReadLine();


}
}
}

or

declare variable and method as static to call from main method

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

namespace ConsoleApplication7
{
public class Program
{
static string b="test";

public static void m1()
{
Console.WriteLine("non static method");
}
public static void Main(string[] args)
{
m1();
Console.WriteLine(b);
Console.ReadLine();
}
}
}


Image Created0Down

By: [email protected] On: Tue May 02 21:40:20 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
//Our main method is static so in order to call any non static method
//from main method either need to create object of class or make variable or method as static

//creating object of class we can handle non static variable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
public class Program
{
string b="test";

public void m1()
{
Console.WriteLine("non static method");
}

public static void Main(string[] args)
{
Program a1 = new Program();
a1.m1();

Console.WriteLine(a1.b);
Console.ReadLine();


}
}
}

or

declare variable and method as static to call from main method

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

namespace ConsoleApplication7
{
public class Program
{
static string b="test";

public static void m1()
{
Console.WriteLine("non static method");
}
public static void Main(string[] args)
{
m1();
Console.WriteLine(b);
Console.ReadLine();
}
}
}


Image Created0Down

By: [email protected] On: Tue May 02 21:40:58 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Image Created0Down

By: [email protected] On: Wed May 03 00:27:44 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No