小编Gus*_*ori的帖子

C#接口作为参数类型转换问题

我有这个方法......

public static IList<IOutgoingMessage> CompressMessages(IList<IOutgoingMessageWithUserAndPtMedInfo> msgs)
    {
        StoreOriginalMessages(msgs);
        ...
    }
Run Code Online (Sandbox Code Playgroud)

调用这种方法......

 private static void StoreOriginalMessages(IList<IOutgoingMessage> msgs) {...}
Run Code Online (Sandbox Code Playgroud)

并且IOutgoingMessageWithUserAndPtMedInfo被定义为这样......

public interface IOutgoingMessageWithUserAndPtMedInfo: IOutgoingMessage
{
    IPatientMedication PatientMedication { get; set; }
    IUserContainer User{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试StoreOriginalMessages从我的CompressMessages方法调用时出现此错误:

无法从'System.Collections.Generic.IList <MyMediHealth.DataAccess.Containers.IOutgoingMessageWithUserAndPtMedInfo>' 转换为'System.Collections.Generic.IList <MyMediHealth.DataAccess.Containers.IOutgoingMessage>'

不明白为什么.我希望它会接受它,因为IOutgoingMessageWithUserAndPtMedInfo继承自IOutgoingMessage.

我究竟做错了什么?

c# inheritance interface

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

课程和指针,它们如何工作?

主要问题是创建动态类.我做了什么:

ptr = new animal[2];
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个大小为2的动态数组,由指针ptr指向.当我尝试这些操作时会出现问题:

ptr[0].setspeed(9);
ptr++->setspeed(13);
Run Code Online (Sandbox Code Playgroud)

我正在使用DDD(gdb图形)调试器,当我显示ptr时,我只看到它指向一个对象.当我尝试设置速度时,第一个似乎工作,但第二个不会(速度默认为0).打印只会变成垃圾.

我不太确定发生了什么,请帮忙.

当我这样做时:

ptr->print();
Run Code Online (Sandbox Code Playgroud)

难道这就是打印两个ptr[0]ptr[1],或者只是ptr[0]

此外,有人可以快速绘制出ptr动态类和新动态类的图片吗?我看到它的方式,它是一个指向数组的ptr,数组大小为2,每个都有一个动物对象.

#include <iostream>
using namespace std;

class animal
{
    private:
        int speed;
        double position_x;
        double position_y;

   public:
        animal() : speed(0), position_x(0), position_y(0)
        {
        }

        animal (int v, double x, double y)
        {
            this->speed = v;
            this->position_x = x;    
            this->position_y = y;
        }

        animal(const animal & g)
        {
            this->speed = g.speed;
            this->position_x = g.position_x;  
            this->position_y = g.position_y;
        }

        ~animal();

        void …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

0
推荐指数
3
解决办法
183
查看次数

((i%2 == 0)&&(i%i == 0))此技术不适用于生成素数c#

嗨!我正在尝试生成素数,但我的条件i%i是产生错误.

我收到错误"尝试除以零".

我该怎么解决这个问题?

int a, n, i;

Console.WriteLine("Enter ur number");

n = Convert.ToInt32(Console.ReadLine());

for (i = 0; i <= n; i++)
{
    if ((i % 2 == 0) && (i % i == 0))
    {
        a = i;
        Console.WriteLine("The prime numbers are", a);
    }
}

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

希望很快听到你的意见......

.net c#

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

If和Else条件都执行C++

我有一个从文本文件中删除数据的功能.

输入有效输入时遇到问题:

  • 如果输入错误的输入,它只会else按预期执行该部分
  • 如果我输入有效输入,它将执行ifelse部分.

这是我的代码:

void Member::processTransaction()
{
    fstream f;

    f.open("Member.txt",ios::in|ios::out);

    int x = 0, y = 0, z = 0;

    while (!f.eof())
    {
        f >> idList[x] >> nameList[y];

        if (id == idList[x])
        {
            cout << idList[x] << "\t" << nameList[x] << endl;

            cout << "Do you want to delete the entry (Y/N) : " << endl;

            char deleteEntry = getche();

            if(deleteEntry=='Y'||deleteEntry=='y')
                deleteInformation();  

            f.close();
        }
        else
        {
            cout << "No Matches Found!";
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c# ×2

c++ ×2

.net ×1

inheritance ×1

interface ×1

pointers ×1