Click here to Skip to main content
15,895,557 members
Home / Discussions / C#
   

C#

 
AnswerRe: Blog site using asp.net 2.0/C# Pin
dan!sh 30-Apr-09 2:51
professional dan!sh 30-Apr-09 2:51 
AnswerRe: Blog site using asp.net 2.0/C# Pin
saanj30-Apr-09 3:07
saanj30-Apr-09 3:07 
Question.NET Remoting - Auto Start Service Object Pin
ATCsharp30-Apr-09 2:31
ATCsharp30-Apr-09 2:31 
AnswerRe: .NET Remoting - Auto Start Service Object Pin
ATCsharp30-Apr-09 3:45
ATCsharp30-Apr-09 3:45 
QuestionMp3 Player Pin
sagiklan30-Apr-09 2:17
sagiklan30-Apr-09 2:17 
AnswerRe: Mp3 Player Pin
DaveyM6930-Apr-09 2:20
professionalDaveyM6930-Apr-09 2:20 
AnswerRe: Mp3 Player Pin
Alaa' Al Atrash30-Apr-09 2:35
Alaa' Al Atrash30-Apr-09 2:35 
QuestionUnboxing an unknown type Pin
mtherien30-Apr-09 1:17
mtherien30-Apr-09 1:17 
I have a strange problem with unboxing.

Take this code:
public class MyCar
{
    public string Name;
    public MyCar(string name) { this.Name = name;  }
    public MyCar(int carNbr) { if (carNbr==1) this.Name="Honda"; else this.Name="Unknown"; }
    public static explicit operator MyCar(string value)
    {
        return new MyCar(value);
    }
    public static explicit operator MyCar(int value)
    {
        return new MyCar(value);
    }
}
private static MyCar GetMyCar(object car)   // car is a boxed string or int
{
    return (MyCar)car;      // does not work
}
static void Main(string[] args)
{
    string carName  = "Honda";
    object carObj = carName;          // carObj is of type object with carName boxed
    MyCar theCar = (MyCar)carObj;    // Invalid cast
    MyCar theCar2 = (MyCar)carName;   // Works !
    MyCar theCar3 = (MyCar)(string)carObj;   // Works
    MyCar theCar4 = GetMyCar((object)1);    // invalid cast
    MyCar theCar5 = GetMyCar(carObj);       // invalid cast
    MyCar theCar6 = GetMyCar(carName);      // invalid cast
}


So can I get the function GetMyCar to return properly if the car type
is a string or integer? I though of doing something with Type, but couldn't find anything.

Anyone have any ideas?

Thanks in advance for your help.
AnswerRe: Unboxing an unknown type Pin
musefan30-Apr-09 1:34
musefan30-Apr-09 1:34 
GeneralRe: Unboxing an unknown type Pin
DaveyM6930-Apr-09 1:42
professionalDaveyM6930-Apr-09 1:42 
GeneralRe: Unboxing an unknown type Pin
musefan30-Apr-09 2:51
musefan30-Apr-09 2:51 
AnswerRe: Unboxing an unknown type Pin
DaveyM6930-Apr-09 1:41
professionalDaveyM6930-Apr-09 1:41 
GeneralRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:02
mtherien30-Apr-09 3:02 
AnswerRe: Unboxing an unknown type Pin
Alan N30-Apr-09 1:59
Alan N30-Apr-09 1:59 
AnswerRe: Unboxing an unknown type Pin
Gideon Engelberth30-Apr-09 2:47
Gideon Engelberth30-Apr-09 2:47 
GeneralRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:13
mtherien30-Apr-09 3:13 
AnswerRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:49
mtherien30-Apr-09 3:49 
GeneralRe: Unboxing an unknown type Pin
S. Senthil Kumar30-Apr-09 9:47
S. Senthil Kumar30-Apr-09 9:47 
QuestionHow to write service in C# ? Pin
Yanshof30-Apr-09 0:41
Yanshof30-Apr-09 0:41 
AnswerRe: How to write service in C# ? Pin
stancrm30-Apr-09 0:53
stancrm30-Apr-09 0:53 
AnswerRe: How to write service in C# ? Pin
Alaa' Al Atrash30-Apr-09 2:18
Alaa' Al Atrash30-Apr-09 2:18 
QuestionProblrm with setup project. Pin
Narendra Reddy Vajrala30-Apr-09 0:30
Narendra Reddy Vajrala30-Apr-09 0:30 
AnswerRe: Problrm with setup project. [CROSS-POST Please Ignore!!!!!] Pin
Henry Minute30-Apr-09 0:53
Henry Minute30-Apr-09 0:53 
GeneralRe: Problrm with setup project. [CROSS-POST Please Ignore!!!!!] Pin
Narendra Reddy Vajrala30-Apr-09 0:59
Narendra Reddy Vajrala30-Apr-09 0:59 
QuestionAdding a text file to a c# project Pin
doubleteam30-Apr-09 0:21
doubleteam30-Apr-09 0:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.