Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all

Is it possible in visual studio 2019 have the list about all public variables declared and their values ?

Best regards

What I have tried:

I tryed in menu Debug -> Window -> Local but without successfully.
Posted
Updated 11-Nov-21 9:59am

No.
The problem is that in C# there are no global variables: every variable is either a local variable or as a part of a class instance.

Local variables only exists as long as the method or code block it is declared as local to is still running - as soon as the ends, the variable is destroyed. This is called scope and it is absolute: once it goes out of scope it no longer exists at all and cannot have a value.

Class variables (called fields) also don't exist in the way you are probably thinking: they ar part of a class instance and don't have a value without the specific instance of the class being referred to in the same way that a glove box only has a content when you talk about a specific car. You can't "list the content of all glove boxes" without unlocking every single car, opening the glove box and having a good rummage about.

So you can't list "all public variables" because they don't exist in isolation in the same way as global variables do in - for example - C.

Instead, use the debugger to look at the class instance, and use that to examine it's properties and fields.
 
Share this answer
 
Here is a slightly modified example from C# | Type.GetFields() Method - GeeksforGeeks[^] that lists the public variables from a class:
// C# program to demonstrate the
// Type.GetFields() Method
using System;
// using System.Globalization;
using System.Reflection;

public class GFG {

	// Main Method
	public static void Main()
	{
		// Declaring and initializing object of Type
		Type objType = typeof(Student);

		// try-catch block for handling Exception
		try {

			// Getting array of Fields by
			// using GetField() Method
			FieldInfo[] info = objType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

			// Display the Result
			Console.WriteLine("Fields of current type is as Follows: ");
			for (int i = 0; i < info.Length; i++)
				Console.WriteLine(" {0}", info[i]);
		}

		// catch ArgumentNullException here
		catch (ArgumentNullException e)
		{
			Console.Write("name is null.");
			Console.Write("Exception Thrown: ");
			Console.Write("{0}", e.GetType(), e.Message);
		}
	}
}

// Defining class Student
public class Student
{
	public string Name = "Rahul";
	public string Dept = "Electrical";
	public int Roll = 10;
	public static int id = 02;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900