小编Bri*_*sen的帖子

C#中的对象初始化

当我声明如下:

class Professor
{
  string profid;
  public string ProfessorID
  {
     get { return profid;}
     set { profid=value;}
  }

  student st;

}


class student
{
  string name;
  string id;
  public string Name
  {
     get  { return name;}
     set  { name=value; } 
  }

 public string StudentID
 {
   get { return id;}
   set { id=value; }
 }

}


public void GetDetails()
{
  Professor prf=new Professor(){ ProfessorID=1, how to initialize student here?};

}
Run Code Online (Sandbox Code Playgroud)

在GetDetails()里面我如何初始化学生?

.net c# initialization object

2
推荐指数
1
解决办法
936
查看次数

我的for循环执行的次数比预期的多

for在我的程序中执行了以下循环,我无法看到它的设计与我收到的输出有何关联.

cout << no_of_lines << endl;
for (int count = 0; count < no_of_lines + 1; count ++)
{
    getline(Device, line, '=');
    cout << line << endl;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

3
DeviceName
GPU
Manufacturer
Intel
GraphicalRam
128MB
Run Code Online (Sandbox Code Playgroud)

这是文件DeviceList

DeviceName=GPU
Manufacturer=Intel 
GraphicalRam=128MB
Run Code Online (Sandbox Code Playgroud)

在循环中,no_of_lines指的是文件中的行数,在本例中为3.我提供此输出作为验证,即循环每行仅执行一次.谁能告诉我为什么这个循环执行的次数比预期的多?我猜这是因为我包含了=作为deliminator,并且循环在某种程度上在递增之前执行了额外的时间,但是为什么它会在最后一行的deliminator上停止,要求我在循环限制中加1 ?

c++ loops

2
推荐指数
1
解决办法
324
查看次数

当有多个using语句时,它们会按顺序执行吗?

例如

  using (Stream ftpStream = ftpResponse.GetResponseStream())       // a             
  using (FileStream localFileStream = (new FileInfo(localFilePath)).Create()) // b
  {
                 ........do something
  }
Run Code Online (Sandbox Code Playgroud)

两个陈述a和b会按照我的顺序执行吗?并按顺序排列?

谢谢

c# dispose using using-statement

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

可以在构造函数之外赋值的只读字段

有没有办法在类中拥有一个私有只读字段,该字段可以在类中的任何地方赋值,但只能赋值一次??

也就是说,我正在寻找一种私有的只读类型的字段,它只能被赋值一次,但不一定在构造函数中。因此,如果一个值被重新分配给一个字段,那么它会显示编译时错误(我确信这要求太多了)。

如果有任何模式(不是语言功能)可以做同样的工作,那么你也会真的很想知道这一点。

谢谢你的关注。

c# readonly

2
推荐指数
1
解决办法
1941
查看次数

String.Replace在运行时不替换字符串

以下代码中的以下行不会产生任何影响:

string1.Replace(string1.Substring(firstchar, lastchar - firstchar), "##");
Run Code Online (Sandbox Code Playgroud)

string1 保持不变,使用IndexOf时返回相同的索引.

while (firstchar != string1.LastIndexOf("test"))
{

    firstchar = string1.IndexOf("test");
    lastchar = string1.IndexOf(" ");
    using (StreamWriter writer = new StreamWriter("C:\\textfile1.txt"))
    {
        writer.WriteLine(string1.Substring(firstchar, lastchar - firstchar));
        writer.WriteLine();
        writer.Dispose();
    }
    string1.Replace(string1.Substring(firstchar, lastchar - firstchar), "##");

}
Run Code Online (Sandbox Code Playgroud)

.net c# string

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

如何从C#中的特定光标点开始读取文件?

我想读取文件但不是从文件的开头但是在文件的特定位置.例如,我想在文件开头之后读取977个字符后的文件,然后一次读取接下来的200个字符.谢谢.

c# file-io writing file

2
推荐指数
1
解决办法
2214
查看次数

从链接列表中删除节点

我想创建一个delete_node函数,它将列表中位置的节点删除为第一个节点的计数.到目前为止,这是我的代码:

class node:
    def __init__(self):
        self.data = None # contains the data
        self.next = None # contains the reference to the next node

class linked_list:
    def __init__(self):
        self.cur_node = None

    def add_node(self, data):
        new_node = node() # create a new node
        new_node.data = data
        new_node.next = self.cur_node # link the new node to the 'previous' node.
        self.cur_node = new_node #  set the current node to the new one.

    def list_print(self):
        node = ll.cur_node
        while node:
            print node.data
            node = node.next …
Run Code Online (Sandbox Code Playgroud)

python linked-list

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

当BindingList中的现有项发生更改时,Listbox拒绝更新

为了让这个工作变得更好,我一直都很疯狂.

我的代码中有几个类.我将尝试在下面发布相关代码并尽可能缩短

public class ServerSettings
{
    private BindingList<Server> serverList = new BindingList<Server>();

    public ServerSettings()
    {

    }

    private void readSettings()
    {           
        string list = "/Settings/Server";
        XmlNodeList Xn = settings.SelectNodes(list);

        foreach (XmlNode xNode in Xn)
        {
            Server tmpSrv = new Server();
            for (int i=0; i<xNode.ChildNodes.Count; i++)
            {
                if(xNode.ChildNodes[i].Name == "Name")
                    tmpSrv.Name = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Host")
                    tmpSrv.Host = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Username")
                    tmpSrv.Username = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Password")
                    tmpSrv.Password = xNode.ChildNodes[i].InnerText;
            }
            tmpSrv.ID = xNode.Attributes["ID"].Value;
            serverList.Add(tmpSrv);
        }
    }

    public …
Run Code Online (Sandbox Code Playgroud)

c# listbox bindinglist

2
推荐指数
1
解决办法
3586
查看次数

API的参数数组与IEnumerabl e <T>

我有一个C#API,可以采用相同类型的0/1 /多个参数.什么应该是更好的方法来定义API = paramsIEnumerable<T>

c#

2
推荐指数
1
解决办法
94
查看次数

将DBnull存储在数据库中,以接受null的日期列

我有一个DateTime值,我想保存到数据库.该列允许数据库中为null.当我尝试运行下面的代码时,我收到此错误消息.

无法确定条件表达式的类型,因为system.dbnull和system.datetime之间没有隐式转换

如何为日期列存储空值或空值?

public class ExcelData
{
  public DateTime? DateValue1 { get; set; }
}

DateValue1 = col28Value == null ? DBNull.Value : Convert.ToDateTime(col28Value)
Run Code Online (Sandbox Code Playgroud)

c#

2
推荐指数
1
解决办法
1416
查看次数