CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: boxing

  1. #1
    Join Date
    Aug 2008
    Posts
    112

    Question boxing

    I try this
    PHP Code:
    StructX x=func();
    while(
    null!=(object)x)
    {
       
    //do something
       
    x=anotherfunc(); 

    This fails, so I say boxing is a flaw. If not, then how can I check for a null struct please?
    Thanks
    hi,,,

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: boxing

    Structs are Valutypes, use CLASSES !!


    Another way would be using nullable Types.
    Code:
    MyStruct? xy = new MyStruct();
    if (xy != null) {
    	Console.WriteLine("Struct is created");
    	MyStruct stest = xy.Value;
    	stest.Val = 5;
    	stest.Name = "Hallo";
    	xy = new Nullable<MyStruct>(stest); 
    }
    	
    public struct MyStruct {
    	public int Val;
    	public string Name;
    }
    As you see handling a simple struct to use it as nullable is a bit a strange task. If you need objects use referene types --> classes
    Last edited by JonnyPoet; October 20th, 2008 at 06:19 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: boxing

    Yeah, you are guaranteed that a value type will not be null by default. I would not start making nullable structs unless you have a very good reason for it. This is definitely not a language flaw.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured