public int getRandomID ()
        {

         Random r = new Random();
         return r.Next(10000,99999);

         string value = ddlCType.SelectedItem.Value;

         switch (value)
         {
             case "Men Formal":
                 return "KML"+r;
         }
        }

i get an error Cannot implicitly convert type string to int in return "KML"+r;
how to solve it?

Recommended Answers

"KML" isn't a meaningful integer value. Presumably since the ID cannot work as an int, your only option is to return a string.:

public string getRandomID()

The function also doesn't return a value in all execution paths. What if value doesn't match "Men Formal"?

Jump to Post

All 2 Replies

"KML" isn't a meaningful integer value. Presumably since the ID cannot work as an int, your only option is to return a string.:

public string getRandomID()

The function also doesn't return a value in all execution paths. What if value doesn't match "Men Formal"?

Int can't store anything but numeric values. If your string is made of ONLY numeric values you can convert it like so:

int MyInt = Convert.ToInt32("1234");

But you CAN NOT do:

int MyInt = Convert.ToInt32("Chris 1234");

Have a look at this link, it will explain the different data types and how they work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.