"The name 'n' does not exist in the current context"。如何在其上下文之外引用类型?
"The name 'n' does not exist in the current context". How can I reference a type outside of its context?
我正在数 DataTable
rows
和 int n = dt.Rows.Count
。
在我的方法中,在几个 foreach 循环和诸如此类的东西中,我需要引用 int n
。
我基本上不知道该怎么做。
"The name 'n' does not exist in the current context"
如何在上下文之外引用它?
我的代码是如何构建的(缩写):
try {
using (DataTable dt = new("aTableName")) {
// more SQL stuff
foreach (DataRow dr in ds.Tables[0].Rows) {
Collection.Add(New Class {
// adding to properties
});
}
int n = dt.Rows.Count; /* this is the int I am trying to reference */
进一步向下,我实例化了一个集合列表,并使用 foreach 循环进行迭代。在那个 foreach 循环中,我需要引用 int。
可以吗?
通常,变量在其声明的上下文中有效。如果在函数中声明 int n,则无法在该函数外访问它。
如果您需要一个可以从外部访问的函数,您有以下几种选择:
1- 使用更高上下文(例如,在两个函数共有的地方声明变量)。如果您的上下文更深入函数(例如,在 if 语句、for 循环等中),那么这就是您需要做的。
int n;
public int modifyingFunction() {
...
n = 5;
}
public int usingFunction() {
someFunction(n);
}
2- 使用带有引用参数 的public 声明。通过这样做并正确引用您在其中声明变量的任何地方的实例(假设您在 class 中声明它),这仅适用于您的值存储在单独对象中的情况, 有自己的实例
public class myClass
{
public int n;
public myClass()
{
n = 25;
}
}
class mainClass
{
static void Main()
{
myClass c = new myClass();
Console.WriteLine(c.n);
}
}
3- Return 来自函数 的n 的值,或者用它来调用另一个函数。如果您有一个值在另一个函数中,就会出现这种情况。
public int myFunction() {
n = 25;
.
.
return n;
}
或
static void Main(string[] args)
{
int x = 10;
Multiplication(ref x);
}
public static void Multiplication(ref int a)
{
a *= a;
}
}
示例取自 here
注意第一个 returns 值,而第二个方法 returns 对变量的引用。
要在循环范围内外使用变量,您应该在循环外部声明它:
- 在方法中作为
variable
;
- 在 class 中作为
field
或 property
;
在方法范围中声明n
变量的示例:
static void RowDeleter()
{
// Creating DataTable with 3 rows
DataTable dt = new DataTable();
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
// Declaring 'n' variable outside of loop scope and inside method scope
int n = dt.Rows.Count;
Console.WriteLine("Initial row count: " + n); // 3
foreach (DataRow row in dt.Rows.Cast<DataRow>().ToArray())
{
// Deleting each row from DataTable
dt.Rows.Remove(row);
// Updating variable value with current row count
n = dt.Rows.Count;
}
// Displaying result
Console.WriteLine("Remained rows count: " + n); // 0
}
在 class 范围 中将 n
变量声明为 field
:
的示例
class RowClass
{
// Declaring variable in class scope, outside of method and loop scopes
private static int n;
private static void RowDeleter()
{
// Creating DataTable with 3 rows same as upper...
// Assigning value to 'N' property which is outside of method scope
n = dt.Rows.Count; // No 'int N' again, we just accessing to it
Console.WriteLine("Initial row count: " + n); // 3
foreach (...)
{
// Same as upper
n = dt.Rows.Count;
}
// Same displaying result
Console.WriteLine("Remained rows count: " + n); // 0
}
}
在 class 范围 中将 n
变量声明为 property
:
的示例
class RowClass
{
private static int N { get; set; }
private static void RowDeleter()
{
// Creating DataTable with 3 rows same as upper...
// Assigning value to 'n' variable which is outside of method scope
N = dt.Rows.Count; // No 'int n'. Variable already declared, we just accessing to it
Console.WriteLine("Initial row count: " + N); // 3
foreach (...)
{
// Same as upper
N = dt.Rows.Count;
}
// Same displaying result
Console.WriteLine("Remained rows count: " + N); // 0
}
}
我正在数 DataTable
rows
和 int n = dt.Rows.Count
。
在我的方法中,在几个 foreach 循环和诸如此类的东西中,我需要引用 int n
。
我基本上不知道该怎么做。
"The name 'n' does not exist in the current context"
如何在上下文之外引用它?
我的代码是如何构建的(缩写):
try {
using (DataTable dt = new("aTableName")) {
// more SQL stuff
foreach (DataRow dr in ds.Tables[0].Rows) {
Collection.Add(New Class {
// adding to properties
});
}
int n = dt.Rows.Count; /* this is the int I am trying to reference */
进一步向下,我实例化了一个集合列表,并使用 foreach 循环进行迭代。在那个 foreach 循环中,我需要引用 int。 可以吗?
通常,变量在其声明的上下文中有效。如果在函数中声明 int n,则无法在该函数外访问它。
如果您需要一个可以从外部访问的函数,您有以下几种选择:
1- 使用更高上下文(例如,在两个函数共有的地方声明变量)。如果您的上下文更深入函数(例如,在 if 语句、for 循环等中),那么这就是您需要做的。
int n;
public int modifyingFunction() {
...
n = 5;
}
public int usingFunction() {
someFunction(n);
}
2- 使用带有引用参数 的public 声明。通过这样做并正确引用您在其中声明变量的任何地方的实例(假设您在 class 中声明它),这仅适用于您的值存储在单独对象中的情况, 有自己的实例
public class myClass
{
public int n;
public myClass()
{
n = 25;
}
}
class mainClass
{
static void Main()
{
myClass c = new myClass();
Console.WriteLine(c.n);
}
}
3- Return 来自函数 的n 的值,或者用它来调用另一个函数。如果您有一个值在另一个函数中,就会出现这种情况。
public int myFunction() {
n = 25;
.
.
return n;
}
或
static void Main(string[] args)
{
int x = 10;
Multiplication(ref x);
}
public static void Multiplication(ref int a)
{
a *= a;
}
}
示例取自 here
注意第一个 returns 值,而第二个方法 returns 对变量的引用。
要在循环范围内外使用变量,您应该在循环外部声明它:
- 在方法中作为
variable
; - 在 class 中作为
field
或property
;
在方法范围中声明n
变量的示例:
static void RowDeleter()
{
// Creating DataTable with 3 rows
DataTable dt = new DataTable();
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
// Declaring 'n' variable outside of loop scope and inside method scope
int n = dt.Rows.Count;
Console.WriteLine("Initial row count: " + n); // 3
foreach (DataRow row in dt.Rows.Cast<DataRow>().ToArray())
{
// Deleting each row from DataTable
dt.Rows.Remove(row);
// Updating variable value with current row count
n = dt.Rows.Count;
}
// Displaying result
Console.WriteLine("Remained rows count: " + n); // 0
}
在 class 范围 中将 n
变量声明为 field
:
class RowClass
{
// Declaring variable in class scope, outside of method and loop scopes
private static int n;
private static void RowDeleter()
{
// Creating DataTable with 3 rows same as upper...
// Assigning value to 'N' property which is outside of method scope
n = dt.Rows.Count; // No 'int N' again, we just accessing to it
Console.WriteLine("Initial row count: " + n); // 3
foreach (...)
{
// Same as upper
n = dt.Rows.Count;
}
// Same displaying result
Console.WriteLine("Remained rows count: " + n); // 0
}
}
在 class 范围 中将 n
变量声明为 property
:
class RowClass
{
private static int N { get; set; }
private static void RowDeleter()
{
// Creating DataTable with 3 rows same as upper...
// Assigning value to 'n' variable which is outside of method scope
N = dt.Rows.Count; // No 'int n'. Variable already declared, we just accessing to it
Console.WriteLine("Initial row count: " + N); // 3
foreach (...)
{
// Same as upper
N = dt.Rows.Count;
}
// Same displaying result
Console.WriteLine("Remained rows count: " + N); // 0
}
}