Solved

.NET code

Posted on 2004-10-13
5
203 Views
Last Modified: 2013-11-25
hello....

I am a c++ and java programmer, and I am doing now a "speech" course. I chose to do my presentation about .NET. however, I dont know how to write C# or ASP.NET programs.

I'd like to show a little demo at the end of the presentation about language interoperability in .NET and how languages can inherit from each other and such things.

can any one please provide a C++ program that uses some C# code, or that inherits from a C# class or anything like that proper for my presentation.

I also appreciate it if any one gave me some code that illustrates the power of .NET and managed code over normal unmanaged code.

thanks....waiting....
0
Comment
Question by:barhoooooom
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:stumpy1
ID: 12298606
You could create a new blank .Net solution.
Then add a (say) C# class library project, Say called project1. Inside this create a base page called BasePage.cs which will be used as a base page for all other pages created. Build the solution.
Now add a new say VB.Net project to the solution, say called project2. In this solution add a reference to the project 'project1'.
Now at the top of an aspx code behind page add an Imports project1 statement.
Change the code
Public Class WebForm1
    Inherits System.Web.UI.Page
to
Public Class WebForm1
    Inherits BasePage

You now have a VB.Net page called WebForm1 that inherits from a C# Basepage called BasePage instead of the normal System.Web.UI.Page. Note: the BasePage class will inherit from System.Web.UI.Page, ie.
      public class PhilePage : System.Web.UI.Page

you can get more information on basepages on this site but with your C++ & Java experience I doubt you will need to
http://www.dotnetjunkies.com/Tutorial/95EBDB54-B8D2-4D33-9DC2-C246793A3A77.dcik
0
 

Author Comment

by:barhoooooom
ID: 12306129
ok...first of all thank you for your response,

I want to know something, can I do something like this:

(C++ code)

class car public: vehicle {
        string  getName();
}

vehicle is a class written in C# or any other .NET language, and name is a data member from class vehicle.

can I do that?

or can I do something similar to that using .NET?

can you show me a similar example that I can show in my demo? say a J# code that uses something written in c++, or C# using something written in VB?

or can you write me a simple code of the BasePage thing you talked to me about?

thanks for your help...waiting.
 
0
 
LVL 5

Assisted Solution

by:stumpy1
stumpy1 earned 175 total points
ID: 12307125
Here is a very simple example of it working with C# and VB.Net. You should be able to extend this to use as many other .Net languages you want.

Create a new blank solution using VS.
Add a new C# Project - ASP.Net Web App called 'TestMultipleLanguages'
Add a new class called BasePage.cs and put the following code in it. This will be our base class (in C#) for Pages:

using System;
using System.Diagnostics;

namespace TestMultipleLanguages
{
      public class BasePage : System.Web.UI.Page
      {
            public BasePage()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            protected override void OnInit(EventArgs e)
            {
                  base.OnInit(e);
                  this.Load += new System.EventHandler(this.BasePage_Load);
                  this.Error += new System.EventHandler(this.BasePage_Error);
            }

            protected void LogEvent(string message, EventLogEntryType entryType)
            {
                  if (!EventLog.SourceExists("TestApp.COM"))
                  {
                        EventLog.CreateEventSource("TestApp.COM", "Application");
                  }
                  EventLog.WriteEntry("TestApp.COM", message, entryType);
            }

            private void LogEvent(string message)
            {
                  if (!EventLog.SourceExists("TestApp.COM"))
                  {
                        EventLog.CreateEventSource("TestApp.COM", "Application");
                  }
                  EventLog.WriteEntry("TestApp.COM", message, EventLogEntryType.Error);
            }

            protected void BasePage_Error(object sender, System.EventArgs e)
            {
                  string errMsg;
                  Exception currentError = Server.GetLastError();

                  errMsg = "<link rel=\"stylesheet\" href=\"/TestApp/Styles/TestApp.css\">";
                  errMsg += "<h1>Page Error</h1><hr/>An unexpected error has occurred on this page. The system " +
                        "administrators have been notified. Please feel free to contact us with the information " +
                        "surrounding this error.<br/>"+
                        "The error occurred in: "+Request.Url.ToString()+"<br/>"+
                        "Error Message: <font class=\"ErrorMessage\">"+ currentError.Message.ToString() + "</font><hr/>"+
                        "<b>Stack Trace:</b><br/>"+
                        currentError.ToString();

                  Response.Write(errMsg);
                  Server.ClearError();
            }

            private void BasePage_Load(object sender, System.EventArgs e)
            {
                  // Place any code that will take place BEFORE Page_Load
            }
      }
}
Save and compile.

Create a new VB.Net - ASP.Net Web App inside the same solution called VBtest.
Add a reference to the 'TestMultipleLanguages' project in this project. (you could just reference the dll but for convience Im doing it this way) Here we will have a VB.Net ASP.Net webform inheriting from a C# class.

Here is the HTML for my WebForm1.aspx page in the VB project. I have 1 textbox:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="VBtest.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body>
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="TextBox1" runat="server">Testing</asp:TextBox>
            </form>
      </body>
</HTML>

Here is the code-behind for my VB WebForm1.aspx page. ie WebForm1.aspx.vb. The only thing that we change is that there is an Imports TestMultipleLanguages statement and we change the code so It now inherits from our C# Base class.

Imports TestMultipleLanguages

Public Class WebForm1
    Inherits BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Convert.ToInt32("asdfg") 'This line is to create an error to show the custom error handling from the C# baseclass
    End Sub

End Class

There is a line of code in the VB.Net code-behind to cause an error to show the inheritance from the C# Base Class.

This is a simple example. You can extend it using as many other .Net languages as you wish but heres an example of how the concept works.
0
 
LVL 4

Accepted Solution

by:
Farshid-Zaker earned 75 total points
ID: 12307301
use below codes, They demonstrate the interlanguage inheritance:

C# File:
using System;
namespace ClassLibrary1
{
      /// <summary>
      /// Summary description for Vehicle.
      /// </summary>
      public class Vehicle
      {
            public Vehicle()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }
            public string getName()
            {
                  return "My Name";
            }
      }
}


CPP File:
using namespace System;
using namespace ClassLibrary1;

namespace vcppChild
{
public __gc class VCPPClass : public ClassLibrary1::CsClass
      {
            // TODO: Add your methods for this class here.
      };
}

additional information: Your VC++ project usually should be a .NET ClassLibrary or WebService or Windows Application. Because it should be a managed C++ code. (attention to __gc at class definition)

Farshid
0
 
LVL 4

Expert Comment

by:Farshid-Zaker
ID: 12307314
sorry for my mistake in copy and paste! change the line
public __gc class VCPPClass : public ClassLibrary1::CsClass
to
public __gc class VCPPClass : public ClassLibrary1::Vehicle
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now