小编mak*_*akc的帖子

.NET,如何确定字段是否已被清零

我很确定这很简单.我试图搜索,但没有快乐.

假设我有一个类型的对象A,它有一个F类型的公共字段B.我创建了一个实例B和设置A.F=B.

结果A.F == null是假的.结果A.F is B是真的.

但后来B设置为null.该A.F属性仍然保存旧实例的句柄B.运行时查找A.F仍然显示为类型B.所以测试A.F == null仍然给出False,测试A.F is B仍然给出True.尽管B已被摧毁.

那么如何检查A.F属性以查看它的句柄现在是否指向消失的对象?

c# garbage-collection memory-management

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

如何在ListBox.ItemsSource的2个数据源之间切换

将ItemsControl.ItemsSource绑定到给定属性上的2个不同源的最佳/优雅方法是什么?

绑定只应该对2个集合中的一个进行,ItemsControl绑定到的集合的选择应该基于某个属性.

我有一个绑定到ViewModel的View.我想要绑定的集合位于该ViewModel下的不同层次结构路径中.

我有一个基于MultiBinding的解决方案,但我认为应该有更优雅的解决方案.

<CollectionViewSource x:Key="CVS">
      <CollectionViewSource.Source  >
          <MultiBinding Converter="{StaticResource myMultiBindingConverter}">
              <Binding  Path="XXXX.YYYY.ObservableCollection1"  />
              <Binding Path="XXXX.ObservableCollection2" />                    
          </MultiBinding>
      </CollectionViewSource.Source>                            
</CollectionViewSource>

<ListBox  x:Name="myListBox"                                   
          ItemsSource="{Binding Source={StaticResource CVS}}" />
Run Code Online (Sandbox Code Playgroud)

转换器:

public class myMultiBindingConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {

        foreach (var item in values)
        {
            if(myDependecyProperty == getFirstCollection)
            {
              //make sure the item is of first collection type based on its item property
              return item;
             }
            else
            {
              //make sure the …
Run Code Online (Sandbox Code Playgroud)

wpf binding itemscontrol multibinding

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

数组内存分配不起作用

我有下一堂课:

class A {
};

class B : public A {
  int num;
};
Run Code Online (Sandbox Code Playgroud)

在我的主要我有:

int main() {
    A* vec; // A is a class with pure virtual functions
    vec = new B[2]; // want to create a vector of B
}
Run Code Online (Sandbox Code Playgroud)

正确定义了vec [0],但vec [1]为NULL.为什么不给我一个合适的记忆?

我不想改变主线.只是让它工作.

(我知道我可以把主要改成:B*vec = new B [2]但我不想要)

任何帮助赞赏!

c++ polymorphism memory-management pointer-arithmetic

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

在我完成使用此对象之前调用析构函数来破坏对象

在我的代码中有 operator+ 重载。在这个范围内,我定义了 object ans,我想构建并返回它,但似乎析构函数ans在我可以返回它之前就进行了破坏,所以这个方法返回了一些未定义的值。

我不明白我错在哪里?它是析构函数、构建器还是在我的 operator+ 重载中?

这是我的代码:

class vector1{
    int * arr;
int size;
public:
//constructors
vector1(){}
vector1(int n){
    size=n;
    arr= new int[size];
}
//functions
int get_size(){return size;}

void init(){  //initialize all array cells to 0
    for(int i=0;i<size;i++)
        arr[i]=0;
}
int get_value_in_index(int index){
    return arr[index];
}
void set_value_in_index(int i, int num){
    arr[i]=num;
}
int & operator[](int i){
    int default_val=0;
    if (i<0 || i>size){
        cout<<"index not valid"<<endl;
        return default_val;
    }
    return arr[i];
}
vector1 operator+(vector1 & …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor shallow-copy

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

在 C++/CLI 中将托管 System::String 转换为 std::string

我在 C++/CLI 中将托管 System::String 转换为 std::string 时遇到问题。这段代码不起作用,我不明白为什么:

string SolvingUnitWrapper::getName(String ^name)
{
    pin_ptr<const wchar_t> wstr = PtrToStringChars(name);
    ostringstream oss;
    oss << wstr; 
    return oss.str();
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ c++-cli

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

WPF中切换按钮的代码设计

我在WPF中切换按钮,当用户点击它时,更改其图标.什么是最好的设计?单击该按钮时,将调用事件处理程序.然后怎样呢?我应该使用依赖属性吗?

谢谢

wpf togglebutton

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

了解抽象工厂设计模式

在抽象工厂设计模式中,类通过组合将对象实例化的责任委托给另一个对象.

可以用一些例子解释一下.

oop design-patterns

-1
推荐指数
1
解决办法
585
查看次数