C
C#2y ago
00tree00

Unable to cast object of type - tried to explicit conversion operator

I have encountered a problem in my game launcher, namely when assigning e.UserState I get an error "Unable to cast object of type 'GameLauncher.Changes' to type 'GameLauncher.Version', I have read that I need to create an explicit conversion operator from Version to Changes, unfortunately I don't know how to go about this, could someone help or direct me with this? below my two structures to Version I take from the page and modify values, for example: 1.5.2 to Changes I download from the site and modify the values of the directories to be downloaded from the server, for example: - data - modules - images mechanisms for downloading and modifying work correctly, unfortunately, the error is in the following lines marked on the screenshot
3 Replies
00tree00
00tree00OP2y ago
struct Version
{
internal static Version zero = new Version(0, 0, 0);

private short major;
private short minor;
private short subMinor;

internal Version(short _major, short _minor, short _subMinor)
{
major = _major;
minor = _minor;
subMinor = _subMinor;
}
internal Version(string _version)
{
string[] versionStrings = _version.Split('.');
if (versionStrings.Length != 3)
{
major = 0;
minor = 0;
subMinor = 0;
return;
}

major = short.Parse(versionStrings[0]);
minor = short.Parse(versionStrings[1]);
subMinor = short.Parse(versionStrings[2]);
}

internal bool IsDifferentThan(Version _otherVersion)
{
if (major != _otherVersion.major)
{
return true;
}
else
{
if (minor != _otherVersion.minor)
{
return true;
}
else
{
if (subMinor != _otherVersion.subMinor)
{
return true;
}
}
}
return false;
}

public override string ToString()
{
return $"{major}.{minor}.{subMinor}";
}
}
struct Version
{
internal static Version zero = new Version(0, 0, 0);

private short major;
private short minor;
private short subMinor;

internal Version(short _major, short _minor, short _subMinor)
{
major = _major;
minor = _minor;
subMinor = _subMinor;
}
internal Version(string _version)
{
string[] versionStrings = _version.Split('.');
if (versionStrings.Length != 3)
{
major = 0;
minor = 0;
subMinor = 0;
return;
}

major = short.Parse(versionStrings[0]);
minor = short.Parse(versionStrings[1]);
subMinor = short.Parse(versionStrings[2]);
}

internal bool IsDifferentThan(Version _otherVersion)
{
if (major != _otherVersion.major)
{
return true;
}
else
{
if (minor != _otherVersion.minor)
{
return true;
}
else
{
if (subMinor != _otherVersion.subMinor)
{
return true;
}
}
}
return false;
}

public override string ToString()
{
return $"{major}.{minor}.{subMinor}";
}
}
struct Changes
{

internal static Changes zero2 = new Changes("");


private string major;


internal Changes(string _major)
{
major = _major;

}

internal bool IsDifferentThan(Changes _changesVersion)
{
if (major != _changesVersion.major)
{
return true;
}

return false;
}
public override string ToString()
{
return $"{major}";
}
}
struct Changes
{

internal static Changes zero2 = new Changes("");


private string major;


internal Changes(string _major)
{
major = _major;

}

internal bool IsDifferentThan(Changes _changesVersion)
{
if (major != _changesVersion.major)
{
return true;
}

return false;
}
public override string ToString()
{
return $"{major}";
}
}
in Version structure i will added
public static explicit operator Version(Changes changes)
{
return new Version
{

major = short.Parse(changes.major)
};
}
public static explicit operator Version(Changes changes)
{
return new Version
{

major = short.Parse(changes.major)
};
}
and in Changes structure
public static explicit operator Changes(Version content)
{
return new Changes
{
major = $"{content.major2}.{content.minor2}.{content.subMinor2}".ToString()
};
}
public static explicit operator Changes(Version content)
{
return new Changes
{
major = $"{content.major2}.{content.minor2}.{content.subMinor2}".ToString()
};
}
but still doesn't work
00tree00
00tree00OP2y ago
Here is url to function InstallGameFiles https://hastebin.com/share/ihopiguwut.java wherever DownloadChangesCompletedCallback is used there UserState is passed
Hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
00tree00
00tree00OP2y ago
errors
Want results from more Discord servers?
Add your server