小编Nic*_*ick的帖子

管理枚举字段

我有一个enum:

public enum Enumeration
{
    A,
    B,
    C
}
Run Code Online (Sandbox Code Playgroud)

并且一个方法接受一个类型的参数Enumeration:

public void method(Enumeration e)
{
}
Run Code Online (Sandbox Code Playgroud)

我希望它method只能接受AB(C被认为是错误的值),但我需要C在我的Enumeration因为其他方法可以接受它作为正确的值.做这个的最好方式是什么?

c# enums

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

为什么新的FontFamily("无效字体")不会抛出异常?

为什么以下代码不会抛出异常?

FontFamily font = new FontFamily("bla bla bla");
Run Code Online (Sandbox Code Playgroud)

我需要知道font我当前的操作系统中是否存在特定的(如FontFamily,FontStyle,FontWeight,...的组合).我该怎么办?

c# wpf fonts font-family

4
推荐指数
1
解决办法
2087
查看次数

我应该使用goto声明吗?

我有一段代码如下:

try
{
Work:

   while(true)
   {
      // Do some work repeatedly...
   }
}
catch(Exception)
{
   // Exception caught and now I can not continue 
   // to do my work properly

   // I have to reset the status before to continue to do my work
   ResetStatus();

   // Now I can return to do my work
   goto Work; 
}
Run Code Online (Sandbox Code Playgroud)

与使用相比,有更好的替代品goto吗?或者这是一个很好的解决方案?

c# goto

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

如何在特定时间后停止执行某个方法?

如果方法在一段有限的时间内没有完成,我需要停止执行.

要做这项工作,我可以这样使用Thread.Abort方法:

void RunWithTimeout(ThreadStart entryPoint, int timeout)
{
    var thread = new Thread(() =>
    {
        try
        {
            entryPoint();
        }
        catch (ThreadAbortException)
        {   }

    }) { IsBackground = true };

    thread.Start();

    if (!thread.Join(timeout))
        thread.Abort();
}
Run Code Online (Sandbox Code Playgroud)

鉴于我使用的是.NET 3.5,还有更好的方法吗?

编辑:跟随我的评论entryPoint,但我正在寻找一个好方法entryPoint.

void entryPoint()
{
   // I can't use ReceiveTimeout property
   // there is not a ReceiveTimeout for the Compact Framework
   socket.Receive(...);
}
Run Code Online (Sandbox Code Playgroud)

c# c#-3.0

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

一次只检查一个ListViewItem

我正在使用Compact Framework开发智能设备项目.

我有ListView几个可检查的ListViewItems:属性CheckBoxes是真的.我需要每次只检查一个ListViewItem,所以我订阅了这个ItemCheck活动:

// I need to know the last item checked
private ListViewItem lastItemChecked;

private void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (lastItemChecked != null && lastItemChecked.Checked)
    {
        /* I need to do the following to prevent infinite recursion:
        i.e. subscribe and then unsubscribe the ItemCheck event. */
        listView.ItemCheck -= listView_ItemCheck;
        lastItemChecked.Checked = false;
        listView.ItemCheck += listView_ItemCheck;
    }

    lastItemChecked = listView.Items[e.Index];
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来阻止无限递归,从而Stack Overflow

c# events listview compact-framework winforms

4
推荐指数
1
解决办法
7303
查看次数

Objective-C Cocoa中的ListView

我曾经使用过.net框架,特别是Wpf.现在我需要使用cocoa为Os X开发一个应用程序.在wpf中我有ListView对象,其中每个元素都可以是我想要的对象.我需要在cocoa中使用一个替代方案,允许我滚动我的个人"用户控件"列表.

有这样的选择吗?

cocoa objective-c

4
推荐指数
1
解决办法
6772
查看次数

如果必须返回布尔值,我的函数是否应该返回False?

写作有什么好处:

def contains(lst, n):
  for x in lst:
    if x == n:
      return True
  return False
Run Code Online (Sandbox Code Playgroud)

代替:

def contains(lst, n):
  for x in lst:
    if x == n:
      return True
Run Code Online (Sandbox Code Playgroud)


请注意,在第二个示例中,缺少最后一行代码,而函数返回None而不是False.

旁注:我知道我应该写n in lst,这只是一个例子.

python boolean nonetype

4
推荐指数
1
解决办法
724
查看次数

资源管理 - 矢量和指针

我需要存储一系列类型的元素ThirdPartyElm,我正在使用std::vector(或者std::array如果我需要一个固定大小的序列).

我想知道如何初始化序列.第一个版本创建一个新元素,并且(如果我是对的)在元素插入序列时创建元素的副本:

for (int i = 0; i < N; i++)
{
   auto elm = ThirdPartyElm();
   // init elm..
   my_vector.push_back(elm);  // my_array[i] = elm;
}
Run Code Online (Sandbox Code Playgroud)

第二个版本存储一系列指针(或更好的智能指针与c ++ 11):

for (int i = 0; i < N; i++)
{
   std::unique_ptr<ThirdPartyElm> elm(new ThirdPartyElm());
   // init elm..
   my_vector.push_back(std::move(elm));  // my_array[i] = std::move(elm);
}
Run Code Online (Sandbox Code Playgroud)

哪个是最轻量级的版本?

请突出显示任何错误.

c++ pointers initialization vector c++11

4
推荐指数
1
解决办法
188
查看次数

如何对一片字节进行求和,减少溢出的可能性

我有一个ASCII字符串切片,我需要计算所有字符的总和,当看作字节.

let word = "Hello, World";
let sum = word.as_bytes().iter().sum::<u8>();
Run Code Online (Sandbox Code Playgroud)

我需要指定sum的类型,否则Rust将无法编译.问题是这u8是一个太小的类型,如果总和溢出程序将会出现恐慌.

我想避免这种情况,但我无法找到一种方法来指定更大的类型,例如,u16或者u32在使用时sum().

我可能会尝试使用fold(),但我想知道是否有一种方法可以sum()通过指定另一种类型来使用.

let sum = word.as_bytes().iter().fold(0u32, |acc, x| acc + *x as u32);
Run Code Online (Sandbox Code Playgroud)

string byte sum slice rust

4
推荐指数
1
解决办法
118
查看次数

为什么除非我使用临时变量,否则我不能推入 dyn Trait 的 Vec?

这是我的代码:

use std::rc::{Rc, Weak};
use std::cell::RefCell;

trait Trait {}

fn push<E: Trait>(e: E) {
    let mut v: Vec<Rc<RefCell<Box<dyn Trait>>>> = Vec::new();
    
    // let x = Rc::new(RefCell::new(Box::new(e)));
    // v.push(x); // error

    v.push(Rc::new(RefCell::new(Box::new(e)))); // works fine
}
Run Code Online (Sandbox Code Playgroud)

v.push(x)引发此错误:

error[E0308]: mismatched types
  --> src/main.rs:12:12
   |
7  | fn push<E: Trait>(e: E) {
   |         - this type parameter
...
12 |     v.push(x);
   |            ^ expected trait object `dyn Trait`, found type parameter `E`
   |
   = note: expected struct `std::rc::Rc<std::cell::RefCell<std::boxed::Box<dyn Trait>>>`
              found struct …
Run Code Online (Sandbox Code Playgroud)

polymorphism traits rust parametric-polymorphism trait-objects

4
推荐指数
1
解决办法
656
查看次数