Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i did something like below:
C#
string name="something";

var upload = new Upload
            {
                UploadID=1,
                ImageName = name,

            };


here Upload is a class name which has two property like:
C#
public class Upload
    {
        public int UploadID { get; set; }
        public string ImageName { get; set; }
    }


it's not possible to assign a string to a class type . So how can I convert the string type to the upload class type???
Posted
Updated 12-Jul-14 0:06am
v2

That should work fine - if you are targeting V3.0 or above of the .NET framework - it won't work below that.
MSDN: Object Initializers[^]
For example, this works:
C#
class Program
    {
    static void Main(string[] args)
        {
        string name = "something";

        var upload = new Upload
        {
            UploadID = 1,
            ImageName = name,
        };
        Console.ReadLine();
        }
    }
public class Upload
    {
    public int UploadID { get; set; }
    public string ImageName { get; set; }
    }
 
Share this answer
 
If i understand you well...
It's possible, but you need to define class constructor[^].

C#
public class Upload
    {
        //variables
        private int id=0;
        private string simagename = String.Empty;

        //
        public Upload(int iid, string sname){id = iid; simagename = sname;}
        public int UploadID { get; set; }
        public string ImageName { get; set; }
    }


Usage:
C#
Upload u = new Upload(1, "something");

or
C#
int myid = 1;
string simagename = "Wallpaper.jpg";
Upload u = new Upload(myid, simagename);
 
Share this answer
 
Comments
rayhan12345 12-Jul-14 6:24am    
but what's the relationship with simagename and ImageName, as I need ImageName.

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