Results 1 to 18 of 18

Thread: Directcast in C#

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Directcast in C#

    When I used VB I would use directcast to change my Main menu items visibility like this:

    Code:
    DirectCast(Me.MdiParent, frmMain).MenuItem13.Visible = True
                DirectCast(Me.MdiParent, frmMain).MenuItem4.Visible = True
                DirectCast(Me.MdiParent, frmMain).MenuItem14.Visible = True
    How do I accomplish the same thing in C#?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Directcast in C#

    First of all, you wouldn't cast the same variable three times in either language. You would cast only once and then use the new variable three times:
    VB.NET Code:
    1. Dim mainForm As frmMain = DirectCast(Me.MdiParent, frmMain)
    2.  
    3. mainForm.MenuItem13.Visible = True
    4. mainForm.MenuItem4.Visible = True
    5. mainForm.MenuItem14.Visible = True
    C# Code:
    1. frmMain mainForm = (frmMain)Me.MdiParent;
    2.  
    3. mainForm.MenuItem13.Visible = true;
    4. mainForm.MenuItem4.Visible = true;
    5. mainForm.MenuItem14.Visible = true;
    I'm also a little concerned about any app with 14 menu items without descriptive names. I wouldn't do that even in a test app.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Directcast in C#

    DirectCast doesn't exist in C#?

    Just try
    C# Code:
    1. this.mdiParent.menuItem4.Visible = true;
    2. this.mdiParent.menuItem13.Visible = true;
    3. this.mdiParent.menuItem14.Visible = true;
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Directcast in C#

    Quote Originally Posted by RudiVisser
    DirectCast doesn't exist in C#?

    Just try
    C# Code:
    1. this.mdiParent.menuItem4.Visible = true;
    2. this.mdiParent.menuItem13.Visible = true;
    3. this.mdiParent.menuItem14.Visible = true;
    That will not work because it would require late-binding. DirectCast was added to VB so it could behave like other languages. Casting has existed in C and languages derived from C for ages, and presumably before that too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    Yes I was lazy about the descriptive names. They were all done in VB and I am now doing them over in C# with proper name formating.

    I guess my biggest problem is that I don't understand what casting really is.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    I tried this from your example:

    Code:
    frmMain mainForm = (frmMain)Me.MdiParent;
    mainForm.itmLogin.Visible = true;
    Me doesn't exist and I changed it to this.

    CODE]frmMain mainForm = (frmMain)this.MdiParent;[/CODE]

    When I now type mainform and hit the ".", I do not get any of the menu items in my intellisense. I do get Menu and then MenuItems but no where to specify the item. Any ideas?
    Last edited by Beast777; Jul 17th, 2007 at 09:29 AM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Directcast in C#

    Have you ever heard the expression "to cast something in a different light"? Literally that means to cast light on an object from a different direction to reveal previously unseen aspects of it. Figuratively it means to look at the same thing in a different way. Casting in programming is exactly the same. Your MdiParent property refers to a frmMain object, but because it is type Form you can only access members of the Form class. When you cast it you create a new variable of a different type to refer to the same object. Because the new variable is type frmMain you can access all the members of the frmMain class, not just those inherited from the Form class. You're looking at the same object in a different way.

    Here's an analogy I like to use. A vet can treat essentially an animal. You bring a box to the vet and they know it contains an animal. They don't know what kind of animal though, so they don't know how to treat it yet. All they can do are the things that are common to all animals because that's as much as they know about it. You open the box and they see that your animal is a cat. Now they know they can treat it as a cat. The cat didn't magically change. It's the same thing it always was. It's just how it can be treated that's changed because now the vet knows more about it.

    The Form type is analogous to the animal type, the frmMain type to the cat type and the compiler is the vet. The compiler knows it has a Form but no more. When you cast you are telling the compiler that the object is actually type frmMain, so the compiler can now treat it as such.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    Thanks for the explanation. Great analogy! My menu is called mnuLibrary. I would of thought by the explanation I should of been able to do the

    Main mainForm = (Main)this.MdiParent;

    and then when I hit the period after typing mainForm that I would be able to see mnuLibrary but I cant. I am getting lost.

    I just made my menu public and I can now see the it with intellisense but I still do not have my items listed just MenuItems as a choice.
    Last edited by Beast777; Jul 17th, 2007 at 09:30 AM.

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Directcast in C#

    I agree. Great explanation.

    If this is the MainMenuStrip of your form you can do something like this:
    Code:
       
                MDIParent1 frm = (MDIParent1)this.MdiParent;
                frm.MainMenuStrip.Items["editMenu"].Visible = false;

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    Okay, slowly getting there. Now I have this code:

    Code:
    Main mainForm = (Main)this.MdiParent;
    mainForm.mnuLibrary.Items["mnuLogin"].Visible = true;
    and I get this error:

    Object reference not set to an instance of an object.

  11. #11
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Directcast in C#

    So this is not your form's MainMenuStrip? That is a property of the form itself. So now, as jmc said, we have a MenuStrip in the box, but the vet is not sure which one.

    I could be off base, but you could try something like this:

    Code:
        
                MDIParent1 frm = (MDIParent1)this.MdiParent;
                MenuStrip mnu = (MenuStrip)frm.Controls["mnuLibrary"];
                mnu.Items["myMenuItem"].Visible = false;

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    I have the same error with this code -

    Code:
    Main mainForm = (Main)this.MdiParent;
    MenuStrip mnu = (MenuStrip)mainForm.Controls["mnuLibrary"];
    mainForm.mnuLibrary.Items["mnuLogin"].Visible = true;
    Object reference not set to an instance of an object.

  13. #13
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Directcast in C#

    That last line is exactly the same as the last line you posted in post #10. That doesn't work.
    Look at this again:
    Code:
    mnu.Items["myMenuItem"].Visible = false;

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    I just tried this:

    Code:
    Main mainForm = (Main)this.MdiParent;
                        MenuStrip mnu = (MenuStrip)mainForm.Controls["mnuLibrary"];
                        mnu.Items["mnuLogin"].Visible = true;
    and got the same error as before, then I did try:

    Code:
    Main mainForm = (Main)this.MdiParent;
                        MenuStrip mnu = (MenuStrip)mainForm.Controls["mnuLibrary"];
                       // mainForm.mnuLibrary.Items["mnuLogin"].Visible = true;
                        ((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items[0]).Visible = false;
    and it will make the whole menu dissapear. I wish I could do subitems now.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    Well this finally worked using

    Code:
    ((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items[1]).DropDown.Items[0].Visible = true;
    I would of thought there would of been an easier way.

  16. #16
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Directcast in C#

    So this was your MainMenuStrip?
    Anyway, glad you figured it out.

    Let everybody know and mark this thread resolved.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Directcast in C#

    So this was your MainMenuStrip?
    I used to just drop a MainMenu control on the form in 2003 but now there are menustrips, menus, etc.

    What is the norm to use for a MDI app?

  18. #18
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Directcast in C#

    The MenuStrip is the control that "replaced" MainMenu. MainMenu is still there, but mostly for backward compatibility: http://msdn2.microsoft.com/en-us/lib...ip(vs.80).aspx

    The MainMenuStrip is a form property of which menu strip is, well, the main MenuStrip.
    http://msdn2.microsoft.com/en-us/lib...ip(vs.80).aspx

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