标签: casting

将对象连接成单个字符串的最佳方法?

string resultString = dtMRow("mcode") + "_" + dtRow("pcode")
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码时,我希望resultString被分配给类似的东西"2356_ASDKJ",但是我得到以下异常:

从字符串"_"到"Double"类型的转换无效.

为什么编译器试图将其"_"转换为Double

让它变成一切的最简单方法是什么strings

.net c# string casting type-conversion

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

将 myType 转换为 Int

我有我的类型,在这里定义: data Nat = Zero | Succ Nat deriving Show

我想要定义函数,将 Nat 转换为 Int。请帮助

我所有的尝试都是这样的:

toInt :: Nat -> Int
toInt n = show n :: Int
Run Code Online (Sandbox Code Playgroud)

并没有给出结果

haskell casting converters

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

为什么类型转换从int打印"0.0000"

下面是我为了解类型转换而编写的一些代码,但我不明白为什么浮点数或双精度值的值打印为"0.000000",即使我从整数数组中输入或尝试从联合的地址解释.

#include <stdio.h>

union test1
{
 int x;
 float y;
 double z;
};


int main()
{
 int x[2]={200,300};
 float *f;
 f=(float*)(x);
 printf("%f\n",*f); /*(1)why is this value 0.0*/

 *f=(float)(x[0]); /*(2)this works as expected and prints 200.00000*/
  printf("%f\n",*f);

  union test1 u;
  u.x=200;
  /*(3)this line give compilation error why*/
  //printf ("sizeof(test1) = %d \n", sizeof(test1));
  /*(4) this line also prints the value of float and double as 0.0000 why*/
  printf ("sizeof(test1) = %lu u.x:%d u.y:%f u.z:%lf \n", sizeof(u), u.x, u.y, u.z);
  return 0; …
Run Code Online (Sandbox Code Playgroud)

c pointers casting sizeof

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

无法将int隐式转换为int []

有一些解决方案,但没有一个对我有用.至于我的问题,我正在构建多个数组.以下是我的变量和我遇到问题的代码:

static int      numOfEmployees;
static string[] nameArray;
static int[]    idArray, deptArray;
static double[] payArray, hoursArray;

static void InputEmployeeData()
{
    int      i;
    string[] words;

    numOfEmployees  = Int32.Parse(fileIn.ReadLine());
    idArray         = new int[numOfEmployees + 1];
    nameArray       = new string[numOfEmployees + 1];
    deptArray       = new int[numOfEmployees + 1];
    payArray        = new double[numOfEmployees + 1];
    hoursArray      = new double[numOfEmployees + 1];

    for (i = 1; i <= numOfEmployees; i++)
    { 
        words       = fileIn.ReadFields();
        idArray     = Int32.Parse(words[0]);
        nameArray   = words[1];
        deptArray   = Int32.Parse(words[2]);
        payArray    = Double.Parse(words[3]); …
Run Code Online (Sandbox Code Playgroud)

c# casting console-application

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

将A类转换为B类而不使用泛型

我有两个彼此没有联系的类:

public class A
{
   public String Address {get;set}
}

public class B 
{
   public String Address {get;set}
}

List<A> addressList = DB.Addresses.GetAll();
Run Code Online (Sandbox Code Playgroud)

当我做

List<B> addressListOther = addressList.Cast<B>().ToList();
Run Code Online (Sandbox Code Playgroud)

输出是:

附加信息:无法将"A"类型的对象强制转换为"B"类型.

知道怎么解决这个问题吗?

.net c# linq casting

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

C++中的C风格转换给出了奇怪的行为

我总是读到C++中的C风格演员与reinterpret_cast相同.但是,我刚刚在Visual Studio中测试了这段代码,看起来C样式的转换与静态转换的行为相同.有人知道为什么吗?这似乎是一个错误......

#include <string>
#include <iostream>

class Thing1
{
    std::string theString;

public:
    Thing1(const std::string& theString) : theString(theString)
    {
        //
    }

    std::string getTheString()
    {
        return theString;
    }
};

class Thing2
{
    std::string theString;

public:
    Thing2(const std::string& theString) : theString(theString)
    {
        //
    }

    std::string getTheString()
    {
        return theString;
    }
};

class Thing3 : public Thing1, public Thing2
{
public:
    Thing3(const std::string& theString1, const std::string& theString2) : Thing1(theString1), Thing2(theString2)
    {
        //
    }
};

int main()
{
    Thing3* thing3 = new Thing3("string1", "string2");
    uintptr_t …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance casting multiple-inheritance

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

C#中从int到byte的隐式转换

所以今天我正在编写一些C#代码,当我收到错误时我感到非常惊讶:"不能隐式地将类型'int'转换为'byte'.存在显式转换(你是否错过了转换?)".我读了一下,显然,如果没有明确的转换,这是不可能的.

我打电话给胡说八道.我已经在C#项目上工作了几个星期,这个转换发生在整个地方,没有任何错误.所以,我的问题是,你在哪里改变这种行为?这显然是可能的.

c# casting type-conversion

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