Answered by:
CS0029: Cannot implicitly convert type 'string' to 'int'

Question
-
User-539280341 posted
private string m_SortOrder;
public int SortOrder {
get {
return m_SortOrder; (error is here )
}
set {
m_SortOrder = "value";
if (!int.TryParse(m_SortOrder.ToString(), out value)) {
m_SortOrder = "0";
}Tuesday, May 7, 2013 9:56 AM
Answers
-
User-1910946339 posted
Can you explain what you are trying to achieve? For example, what do you want the code that uses the SortOrder property to look like? Would it be something like
instance.SortOrder = 34; // What would this mean? Or do you only want to allow a subset of values like +1 and -1?
or would it be something like
instance.SortOrder = "ascending";
instance.SortOrder = "descending";
or would you want to set the value with an enum?
instance.SortOrder = Ascending;
instance.SortOrder = Descending;
or maybe you want to pass integers as strings with error handling for invalid strings
instance.SortOrder = "12"; // This would return "12" on a subsequent get
instance.SortOrder = "five"; // This would return "0" on a subsequent get with no error or exception being thrown
How you intend to use the SortOrder property will determine how you need to define it.
The code you have shown is wrong in so many aspects that just correcting the line you have marked will not help you. It is not possible for us to provide meaningful help unless you can specify what you are trying to achieve.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 10, 2013 4:04 AM
All replies
-
User-492460945 posted
Hi,
Since the return type of the method is int, you cannot return m_SortOrder.. instead return in a way
Convert.toInt32(m_SortOrder)
Tuesday, May 7, 2013 9:58 AM -
User1196771204 posted
hi Bari,
what rajesh suggested here is already correct. You just have to use Convert.toInt32(some_string_here) to convert a string-based number to an integer.
Likewise, you can use Convert.toDouble( ) or Convert.toDecimal( ), etc
Please mark this response as an "Answer" if it helps you. Thanks heaps!Wednesday, May 8, 2013 12:45 AM -
User-158764254 posted
i thing i'd just keep the Type of the backing var the same as the Type of the property:
private int m_sortOrder; public int SortOrder { get { return m_sortOrder;} set { m_sortOrder = value; } }
or drop the back var all-together:
public int SortOrder { get; set; }
Wednesday, May 8, 2013 1:00 AM -
User-1910946339 posted
Can you explain what you are trying to achieve? For example, what do you want the code that uses the SortOrder property to look like? Would it be something like
instance.SortOrder = 34; // What would this mean? Or do you only want to allow a subset of values like +1 and -1?
or would it be something like
instance.SortOrder = "ascending";
instance.SortOrder = "descending";
or would you want to set the value with an enum?
instance.SortOrder = Ascending;
instance.SortOrder = Descending;
or maybe you want to pass integers as strings with error handling for invalid strings
instance.SortOrder = "12"; // This would return "12" on a subsequent get
instance.SortOrder = "five"; // This would return "0" on a subsequent get with no error or exception being thrown
How you intend to use the SortOrder property will determine how you need to define it.
The code you have shown is wrong in so many aspects that just correcting the line you have marked will not help you. It is not possible for us to provide meaningful help unless you can specify what you are trying to achieve.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 10, 2013 4:04 AM