equivalent C# code for this VB.NET code..

  • Thread starter Thread starter Maheshkumar.R
  • Start date Start date
M

Maheshkumar.R

Hi groups,

Can anyone give me the equivalent C# sharp code for this VB.ET code,

:: VB.NET ::

If Not Me.ActiveMdiChild Is Nothing Then
Dim child As IMDIChild = DirectCast(Me.ActiveMdiChild, IMDIChild)
child.calculate()
End If
--

C# ??



Mähésh Kumär. R
 
Hi,

I don't think this is a literal translation of your code, but it should
accomplish what you want:

IMDIChild child = this.ActiveMdiChild as IMDIChild;
if (child != null)
{
child.calculate();
}

The advantage is that this will not throw an exception if ActiveMdiChild does
not implement IMDIChild, it will just not call the calculate method.

I believe the literal translation would be:
if (this.ActiveMdiChid != null)
{
IMDIChild child = (IMDIChild) this.ActiveMdiChild;
child.calculate();
}

which will throw an exception if ActiveMdiChild is not not null and is not an
IMDIChild.

Best regards,

Rodger

Achieve Planner time managment software - Project/task outliner with calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
Thnkz rodger, I was sitting for this, by tring boxin and all...but simply
superb and effective..WORKING FINE now..
THNKZ THNKZ

by the by i have few more queries.,
Assuming i'm using VS2005 beta 2, C#


q(1). I have MDI parent and MDI child window. I have placed tools menu in
MDI parent which is containing a list of items say imagesharp, imagecontrast
etc....if i click imagesharp of MDI parent, i have to sharp the image in
MDI child. How i can communicate [ MDI Parent ---- > MDI child ] ??

q(2). Wht are all the ways to access the MDI child member functions from MDI
parent class... ??

q(3). Using this.activechild to get the current active child window, but do
know how to pass an event or variables from MDI parent to that current
active child...???

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak
 
Anytime you want to access a child form from the parent forms class, the two
best available options are MdiChildren and ActiveMdiChild.

The first is simply an array of Forms that are the children of the current
form and you can easily iterate through each and do what ever work you want
to to each one. Note though, your child form is probably something that
inherits from Form, so when making calls to things that are part of your
custom Form class, you would need to typecast.

The second, ActiveMdiChild is a little more useful, it returns a reference
to the currently active child form. Like mentioned above, typecasting is
often necessary for accessing custom implementations.

Brendan


Maheshkumar.R said:
Thnkz rodger, I was sitting for this, by tring boxin and all...but simply
superb and effective..WORKING FINE now..
THNKZ THNKZ

by the by i have few more queries.,
Assuming i'm using VS2005 beta 2, C#


q(1). I have MDI parent and MDI child window. I have placed tools menu in
MDI parent which is containing a list of items say imagesharp, imagecontrast
etc....if i click imagesharp of MDI parent, i have to sharp the image in
MDI child. How i can communicate [ MDI Parent ---- > MDI child ] ??

q(2). Wht are all the ways to access the MDI child member functions from MDI
parent class... ??

q(3). Using this.activechild to get the current active child window, but do
know how to pass an event or variables from MDI parent to that current
active child...???

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak

Rodger Constandse said:
Hi,

I don't think this is a literal translation of your code, but it should
accomplish what you want:

IMDIChild child = this.ActiveMdiChild as IMDIChild;
if (child != null)
{
child.calculate();
}

The advantage is that this will not throw an exception if ActiveMdiChild does
not implement IMDIChild, it will just not call the calculate method.

I believe the literal translation would be:
if (this.ActiveMdiChid != null)
{
IMDIChild child = (IMDIChild) this.ActiveMdiChild;
child.calculate();
}

which will throw an exception if ActiveMdiChild is not not null and is not an
IMDIChild.

Best regards,

Rodger

Achieve Planner time managment software - Project/task outliner with calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
Rodger Constandse said:
I don't think this is a literal translation of your code, but it should
accomplish what you want:

IMDIChild child = this.ActiveMdiChild as IMDIChild;
if (child != null)
{
child.calculate();
}

The advantage is that this will not throw an exception if ActiveMdiChild does
not implement IMDIChild, it will just not call the calculate method.

Personally I don't believe that's a benefit in most situations - not if
the code is really *expecting* that the child is an IMDIChild.
I believe the literal translation would be:
if (this.ActiveMdiChid != null)
{
IMDIChild child = (IMDIChild) this.ActiveMdiChild;
child.calculate();
}

which will throw an exception if ActiveMdiChild is not not null and is not an
IMDIChild.

Yup - which means that if you've got something hooked up wrongly, you
get to find out early, rather than wondering why your calculations
aren't working :)
 
As Jon stated, the literal translation conveys the intent of the original code.
Interestingly, there is no perfect equivalent in C# to "DirectCast".
DirectCast basically says to the compiler "bypass some basic type checking
for better performance because I guarantee that the run-type type is the same
as the receiving entity". C# has no such concept (although C# does have "as"
which is a totally different casting concept than either CType or DirectCast).

David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
 
Back
Top