Click here to Skip to main content
15,742,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do i cast a sting type to another type? i have tried different options but when i run the application it throws an error message
public static Xxl.Query QueryBySize(string SizeNo)
            Query result = new Query();
            result.SizeInfo.SizeCode = "Xxl";
            Xxl.Query result1 = new Xxl.Query()
                SIZE = SizeNo
            JsonSerializerOptions options = new JsonSerializerOptions
                IgnoreNullValues = true
            string str = JsonSerializer.Serialize<Xxl.Query>(result1, options);
            Console.WriteLine("Retrieved Details: " + str);
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            result.Data.Content = Convert.ToBase64String(bytes);
            Xxl.Query xxl = (Xxl.Query)QueryRequest(result );

            return xxl;

public class Query
        public ItemName ItemName { get; set; }

        public string Result { get; set; }

        public Query()
            ItemName = new ItemName();

What I have tried:

i have tried to create an object of the class and used it to cast but it hasn't not helped to solve the problem.
Updated 10-Oct-22 14:42pm

1 solution

You don't cast strings to class instances: tough it would be possible to write a constructor that accepts a string, or a static cast operator it's not what would normally happen - you cast between similar types, not totally different ones!

I assume that your QueryRequest method returns a string of some type and this is the line that throws the error:
Xxl.Query xxl = (Xxl.Query)QueryRequest(result );
If so, you need to look at either the QueryRequest method itself to find out exactly what it returns, and how that relates to your Query class, or use the debugger to work it out from an actual example of the string it returns.

Sorry, but we can't help you do that: we have no access to your code or data!
Share this answer
Member 10578733 10-Oct-22 8:58am    
hello, thanks for the feedback. i have updated the question with the QueryRequest method
OriginalGriff 10-Oct-22 9:28am    
So the string you get back will depend on whatever the heck the remote server returns to you - and we don't even have a clue what the server is, much less what it returns.

Break out the debugger.
Look at what you get returned.
Think about it, and look for what you need out of that.

Then you can start fixing this, but not until then.
Member 10578733 10-Oct-22 12:34pm    
Got it solved. it turns out there was nothing wrong with my code. The issue was with one of the dependencies of the System.text.json.dll library called system.numeric.vectors.dll ,once i added it to the project i stopped getting the exception.Thanks for the effort

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900