小编Gra*_*ICA的帖子

try catch块中的变量范围

我在尝试关闭Finally代码块中的文件时遇到错误:

static void Main(string[] args)
{
    try
    {
        StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt");

        string line = "";

        while (line != null)
        {
            line = myReader.ReadLine();
            Console.WriteLine(line);
        }

        //myReader.Close();
    }
    catch (FileNotFoundException e)
    {
        Console.WriteLine("sorry file not found! {0}", e.Message);
    }
    catch (DirectoryNotFoundException e)
    {
        Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
    }
    catch (Exception e)
    {
        Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
    }
    finally
    { 
        myReader.Close();
    }

    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

c# scope exception-handling

0
推荐指数
2
解决办法
3700
查看次数

能修改课外的私人会员吗?

我遇到了这样的情况,我可以在外面修改班级的私人成员.这是我的班级:

public class MyClass
{
    private List<string> _myClassMember = null;

    public MyClass()
    {
        _myClassMember = new List<string>();
    }

    public List<string> GetClassMembers()
    {
        return _myClassMember;
    }

    public void PrintMembers()
    {
        Console.WriteLine("No of items in member {0}", _myClassMember.Count.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是它的调用方式:

class Program
{
    static void Main(string[] args)
    {
        MyClass cls = new MyClass();
        cls.PrintMembers();
        IList<string> lst = cls.GetClassMembers();
        lst.Add("Item1");
        lst.Add("Item2");
        cls.PrintMembers();
    }
}
Run Code Online (Sandbox Code Playgroud)

它打印如下:

No of items in member 0
No of items in member 2
Run Code Online (Sandbox Code Playgroud)

能够更新外面的私人会员是否正确?

.net c# oop

0
推荐指数
2
解决办法
267
查看次数

string.IsNullOrWhiteSpace() 和 string.IsNullOrEmpty() 中的 NullReferenceException

我正在检查可能为空/空/空的列的单元格的单元格值,所以我需要一些东西来避免NullReferenceException.

我该怎么做,因为即使有IsNullOrWhiteSpace()并且IsNullOrEmpty()我以某种方式得到了那个例外。

这是我正在使用的代码的一部分:

s = "Total = " + dataGridView1.Rows[0].Cells.Count +
    "0 = " + dataGridView1.Rows[0].Cells[0].Value.ToString() +
    "/n  1 = " + dataGridView1.Rows[0].Cells[1].Value.ToString() +
    "/n  2= " + dataGridView1.Rows[0].Cells[2].Value.ToString() +
    "/n  3 = " + dataGridView1.Rows[0].Cells[3].Value.ToString() +
    "/n  4= " + dataGridView1.Rows[0].Cells[4].Value.ToString() +
    "/n  5 = " + dataGridView1.Rows[0].Cells[5].Value.ToString() +
    "/n  6= " + dataGridView1.Rows[0].Cells[6].Value.ToString() +
    "/n  7 = " + dataGridView1.Rows[0].Cells[7].Value.ToString();

    if (string.IsNullOrEmpty(dataGridView1.Rows[0].Cells[8].Value.ToString()))
    {

    }
    else
    {
        s += "/n  8 = " …
Run Code Online (Sandbox Code Playgroud)

c# string nullreferenceexception isnullorempty

0
推荐指数
1
解决办法
4399
查看次数

在 DataGridView C# 中删除行

我尝试循环遍历dataGridView1并删除不满足以下条件的行:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
    {
        dataGridView1.Rows.Remove(row); //error: Uncommitted new row cannot be deleted.
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到了这个错误:

无法删除未提交的新行。

如果代码也是VB.NET,我可以管理。

.net c# vb.net datagridview row

0
推荐指数
1
解决办法
1万
查看次数

调用Bitmap.SetPixel时获取ArgumentOutOfRangeException

在某种程度上,一个不那么容易的错误已经潜入这段代码,而我不知道修复它:

static void Main(string[] args)
{
    Bitmap bm = new Bitmap(1, 1);
    bm.SetPixel(1, 1, Color.AliceBlue);
    bm.Save("C:\\Users\\Lasse\\Pictures\\Midlertidigt\\hej.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);//
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

例外:

System.Drawing.dll中发生未处理的"System.ArgumentOutOfRangeException"类型异常

附加信息:参数必须为正且<宽度.

c# bitmap console-application

0
推荐指数
1
解决办法
749
查看次数

复制文件时出现DirectoryNotFoundException错误

我正在尝试开发一个Web应用程序,它将文件发送到服务器(在这种情况下,服务器是本地服务器)。但是,当我尝试发送文件时,它总是给出:

DirectoryNotFoundException:找不到路径的一部分。

我已经确认该文件夹确实存在。并且为了确保,我在复制之前创建了目录,但是仍然出现错误。有人可以帮忙吗?这是代码:

string FileName = System.IO.Path.GetFileName("c:\\test\\Sample.txt");
string SaveLocation = HttpContext.Current.Server.MapPath("Uploadfile") + "\\";

if (System.IO.Directory.Exists(SaveLocation))
{
     System.IO.Directory.CreateDirectory(SaveLocation);
     System.IO.File.Copy("C:\\test\\Sample.txt", SaveLocation, true);
}
Run Code Online (Sandbox Code Playgroud)

的值为SaveLocation

C:\ Users \ Nerd \ Documents \ Visual Studio 2012 \ Projects \ WebApplication3 \ WebApplication3 \ Uploadfile \

c# asp.net web-applications

0
推荐指数
1
解决办法
1675
查看次数

使扩展器控件标头变为粗体

我将在运行时动态创建n扩展器控件,如下所示,

theExpanderCaption[counter] = new TextBlock();
theExpanderCaption[counter].FontWeight = FontWeights.Bold;
theExpanderCaption[counter].Text = ITEMIMP.DataConstants.GroupIds.GetGroupCaption(group.GroupId);

theGroupExpander[counter] = new Expander();
theGroupExpander[counter].Header = theExpanderCaption[counter];
theGroupExpander[counter].Margin = new Thickness(10);
theGroupExpander[counter].HorizontalAlignment = HorizontalAlignment.Left;
theGroupExpander[counter].IsExpanded = true;
theGroupExpander[counter].Content = this.theGroupGrids[counter];
theGroupExpander[counter].Style = null;
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我使用文本块数组来设置扩展器头(使其变为粗体).这里的缺点是我必须使用n no 文本块控件.有没有其他方法来实现这一目标?

c# wpf expander

0
推荐指数
2
解决办法
1426
查看次数

程序不会停止用户输入

当我运行我的程序时,它直接运行并且不允许我输入用户输入.

这是我的主要功能

[STAThread]
static void Main()
{
    valueCalculation(validateX(), validateY(), validateZ());
}
Run Code Online (Sandbox Code Playgroud)

这是我的validateX函数

public static String validateX()
{
    Console.WriteLine("Enter your X value: ");
    string x = null;
    do
    {
        x = Console.ReadLine();

        //do stuff
    }
    while(x != null);

    return x;
}
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
74
查看次数

如何使用LINQ提取所有负序子

我需要从源序列中提取所有负序子.例如,我有:

{-2,-2,5,9,1,-6,-7,-2,5,11,-2}

在结果中我想得到这些序列:

{-2,-2},{ - 6,-7,-2}和{-2}

是否可以使用LINQ解决任务?

c# linq

0
推荐指数
1
解决办法
268
查看次数

字符串中"{0}"的含义是什么?

当我{0}在下面的代码中使用时:

class Program
{
    double width;
    double height;

    public void getData() {
        Console.WriteLine("Enter Width:");
        width = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Enter Height:");
        height = Convert.ToDouble(Console.ReadLine());
    }

    public double calcArea() {
        return width * height;
    }

    public void display() {
        Console.WriteLine("Area is : {0}",calcArea());
    }
}

class Area
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.getData();
        p.display();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

输入宽度:
6
输入高度:
9
面积为:54

当我在这里使用{0}时:

class NewArea
{
    static void Main(String[] args) {
        double width;
        double height; …
Run Code Online (Sandbox Code Playgroud)

c# string console.writeline

0
推荐指数
1
解决办法
149
查看次数