简体   繁体   English

将字符串转换为int会产生编译错误?

[英]converting a string to an int gives a compile error?

After looking through some of the 'how to convert string to int' posts on SO, I resorted to using; 看完SO上的一些“如何将字符串转换为int”文章之后,我求助于使用;

public MyObject(Object parObject)
{
    this.VersionInt = Convert.ToInt32(this.parObject.VersionString);
    // object has a field called 'VersionString' which holds a 
    // string-typed version number (1.0, 2.3, etc.)
}

public int VersionInt{ get; set; }

However, I get the compile message "Cannot implicitly convert type 'int' to 'string'. Isn't that exactly what the method is supposed to do and thus makes the compile error obsolete? 但是,我收到了编译消息“无法将类型'int'隐式转换为'string'。这不正是该方法应该执行的操作,从而使编译错误过时了吗?

My apologies if this is somehow a duplicate to something, but I couldn't find anyone else with the same problem. 我很抱歉,如果这是某件事的重复,但是我找不到其他人遇到同样的问题。 Might be because I'm overlooking something obvious.. 可能是因为我忽略了一些明显的问题。

Converting it to a double and casting it to an int like this; 像这样将其转换为double并转换为int;

this.VersionInt = (int)Convert.ToDouble(this.parObject.VersionString);

or 要么

this.VersionInt = (Int32)Convert.ToDouble(this.parObject.VersionString);

gives me the same message. 给我同样的信息

EDIT: Because everybody is telling me the pseudo-names are more confusing than clarifying, here's the actual code; 编辑:因为每个人都告诉我,伪名称比澄清更令人困惑,这是实际的代码;

private EA.Element element;

public Requirement(EA.Element element)
{
    this.element = element;
    this.Status = (int)Convert.ToDouble(this.element.Status);
}

public string Status { get; set; }

the this.element.Status is a string . this.element.Status是一个string There's really no doubt there, because the compiler tells me so when I hover it; 毫无疑问,那里是因为编译器告诉我,所以当我将其悬停时,编译器会告诉我。 ( string EA.IDualElement.Status { get; set; } string EA.IDualElement.Status { get; set; }

Another Edit: Might be because I'm overlooking something obvious.. Might be because I'm overlooking something obvious.. Might be because I'm overlooking something obvious.. Might be because I'm overlooking something obvious.. Might be because I'm overlooking something obvious.. Might be because I'm overlooking something obvious.. Might be because I'm overlooking something obvious.. 另一个编辑:可能是因为我忽略了明显的东西。可能是因为我忽略了明显的东西。可能是因为我忽略了明显的东西。可能是因为我忽略了明显的东西。可能是因为我忽略了明显的东西..可能是因为我忽略了明显的东西..可能是因为我忽略了明显的东西..

Might be because at the end of my workday I'm starting to convert Version-types to Status-types.. I am so terribly sorry for taking up your time, I'm going to get myself another coffee 可能是因为在工作结束时,我开始将Version-types转换为Status-types。.非常抱歉,您抽出宝贵的时间,我要再喝杯咖啡

Are you sure you get a cpmpiler -error? 您确定会收到cpmpiler错误吗? I suppose you get an error when executing your code, making this a runtime -error. 我想您在执行代码时遇到错误,使之成为运行时错误。 In particular you can´t convert "1.2" to an integer, as it is - if anything - a double . 特别是您不能将"1.2"转换为整数,因为它是double精度整数(如果有的话)。 Thus you have to split it at . 因此,您必须将其拆分为. using String.Split : 使用String.Split

string[] numbers = mthis.parObject.VersionString.Split('.');
this.VersionMajor = Convert.ToInt32(numbers[0]);
this.VersionMinor = Convert.ToInt32(numbers[1]);

Anyway, why not simply use the Version -class which does all this for you out of the box? 无论如何,为什么不简单地使用Version -class来为您提供所有这些功能呢?

Version version;
if(Version.TryParse("1.2", out version)) { ... };

It's confusing, because actually you can't get the error that you've mentioned with that code. 这很令人困惑,因为实际上您无法获得该代码中提到的错误。 Why should the compiler complain about that he can't convert an int to string when the code that you've shown converts a string to int? 当您显示的代码将字符串转换为int时,为什么编译器为什么会抱怨无法将int转换为string?

However, since you use versions that aren't integers at all. 但是,由于您使用的版本根本不是整数。 You should hold a Version instance or at least multiple int -properties like Major and Minor . 您应该拥有一个Version实例或至少多个int properties,例如MajorMinor

To create a version you can use Version.TryParse : 要创建一个版本,可以使用Version.TryParse

// note that i've changed Object to MyObject 
public MyObject(MyObject parObject)
{
    Version v;
    if(Version.TryParse(parObject.VersionString, out v))
    {
        this.Version = v;
    }
    else
        throw new ArgumentException("invalid version: " + parObject.VersionString);
}

public Version Version { get; set; }

Use the TryParse method for this. 为此使用TryParse方法。

    var intAsString = "99";
    var intAsInt = 0;
    if(!TryParse(intAsString, out intAsInt))
    {
        //Didn't parse int properly.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM