简体   繁体   中英

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;

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?

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;

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 . There's really no doubt there, because the compiler tells me so when I hover it; ( 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

Are you sure you get a cpmpiler -error? 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 . Thus you have to split it at . using 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 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?

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 .

To create a version you can use 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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