开发者

Problem with implicitly conversion of string

开发者 https://www.devze.com 2023-02-23 22:20 出处:网络
I have a object \'currentVersion\' of type Data and string variable of \'version\',i need to assign what ever things coming\'version\' to \'currentVer开发者_运维问答sion\'

I have a object 'currentVersion' of type Data and string variable of 'version',i need to assign what ever things coming 'version' to 'currentVer开发者_运维问答sion' Code is like this

    private static DataVersion currentVersion = new DataVersion();

    string version = this_event.variableData[1].atr_value;

and in internal layers 
public SPD_variableData[] variableData;
and 
      /// <summary>
      /// Definition of variable data for events.
      /// </summary>
      public struct SPD_variableData
      {
         /// <summary>
         /// attribute name 
         /// </summary>
         public string attribute;
         /// <summary>
         /// attribute value
         /// </summary>
         public string atr_value;
      }

but when i did currentVersion = version ; i am getting error like this "Error 1 Cannot implicitly convert type 'string' to 'Safe.Model.Data' " if that is the case how i can assign values coming on version to currentVersion


You cannot convert a string to any old object - in .NET any old object can be turned into a string using the ToString method though.

If you are storing something meaningful in the string that you think you convert into a Data object, then write a method to instantiate a Data object from the string. For example:

public static Data FromString(string input)
{
//get something meaningful from the string. eg. if it is a CSV, use split
Data ret = new Data();
string[] fields = input.Split(',');
ret.property1 = fields[0];
ret.property2 = fields[1];
return ret;
}

Or was there a reason you thought you could turn a string into a Data object?

Update

Something like this?

public static DataVersion FromVariableData(SPD_variableData input)
{
    //set the properties of a new object before returning it
    DataVersion ret = new DataVersion();
    ret.attribute = input.attribute;
    ret.atr_value = input.atr_value;
    return ret;
}


First of all Data doesn't seem to be an array...
Second: which type is value?
EDITED:
you changed your code: what is level? It doesn't seem related to Data...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号