namespace's conflict

R

Ruslan

Hi,



I have 3 projects:

1. Common

2. Desktop

3. WebServices



The Common is used in Desktop and WebServices project.

In Common I have the class Order.



I get the value for Order class from WebServices like this:

Order order = null;

order = localhost.GetOrder();



If I use different namespace in Common and Desktop I'll get this error,
which is correct.

Cannot convert source type Order to target name Common.Order.



But if I use the same namespace for both Common and Desktop project the
application work perfect. Is that OK, or can I have the problems in the
future? Should I convert the Order class from localhost to Common (using
IXmlSerializable, for instance)?



Thanks a lot

Ruslan
 
N

netnews.microsoft.com

Console app
webservice Order class (with namespace)
Common.Order class


Code follows
--------------------------------
Console test harness
--------------------------------
using System;
using Common; //for Common.Order class
using OrdersWebservice; //for OrdersWebservice.Order class

namespace Test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class WebServiceConsumer
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Instantiate Oder class from the OrdersWebservice namespace
OrdersWebservice.Order order0 = new OrdersWebservice.Order();

//Instantiate Oder class from the Common namespace
Common.Order order1 = new Common.Order();

//The commented code line below will have a compile error
//because the compiler cannot determine which Order class to use
//Order order2 = new OrdersWebservice.Order();


Console.WriteLine("OrdersWebservice.Order() " + order0.GetOrder(0));
Console.WriteLine("Common.Order() " + order1.GetOrder(0));

//Pause to keep console active
WaitApp();
}

public static void WaitApp()
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press <enter/return> key to exit this application");
Console.ReadLine();
}
}//~class
}//~namespace

--------------------------------------------------------------
Webservice Order class in OrdersWebservice namespace
--------------------------------------------------------------


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace OrdersWebservice
{
/// <summary>
/// Summary description for Service1.
/// </summary>
public class Order : System.Web.Services.WebService
{
public Order()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}

#region Component Designer generated code

//Required by the Web Services Designer
private IContainer components = null;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5

[WebMethod]
public string GetOrder(int productId)
{
//productId is not used because just example code
//test data only, I would query a database if a real webservice
return "0-596-00006-5|O'Reilly|Physics for Game Developers";
}



}//~class
}//~namespace

---------------------------------------
Order class in Common namespace
---------------------------------------
using System;

namespace Common
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Order
{
public Order()
{

}

public string GetOrder(int productId)
{
//productId is not used because just example code
//test data only, I would query a database if a business object
return "1-59059-344-8|Apress |Expert C# Business Objects";
}
}//~class
}//~namespace
 
N

netnews.microsoft.com

Console app
webservice Order class (with namespace)
Common.Order class


Code follows
--------------------------------
Console test harness
--------------------------------
using System;
using Common; //for Common.Order class
using OrdersWebservice; //for OrdersWebservice.Order class

namespace Test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class WebServiceConsumer
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Instantiate Oder class from the OrdersWebservice namespace
OrdersWebservice.Order order0 = new OrdersWebservice.Order();

//Instantiate Oder class from the Common namespace
Common.Order order1 = new Common.Order();

//The commented code line below will have a compile error
//because the compiler cannot determine which Order class to use
//Order order2 = new OrdersWebservice.Order();


Console.WriteLine("OrdersWebservice.Order() " + order0.GetOrder(0));
Console.WriteLine("Common.Order() " + order1.GetOrder(0));

//Pause to keep console active
WaitApp();
}

public static void WaitApp()
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press <enter/return> key to exit this application");
Console.ReadLine();
}
}//~class
}//~namespace

--------------------------------------------------------------
Webservice Order class in OrdersWebservice namespace
--------------------------------------------------------------


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace OrdersWebservice
{
/// <summary>
/// Summary description for Service1.
/// </summary>
public class Order : System.Web.Services.WebService
{
public Order()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}

#region Component Designer generated code

//Required by the Web Services Designer
private IContainer components = null;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5

[WebMethod]
public string GetOrder(int productId)
{
//productId is not used because just example code
//test data only, I would query a database if a real webservice
return "0-596-00006-5|O'Reilly|Physics for Game Developers";
}



}//~class
}//~namespace

---------------------------------------
Order class in Common namespace
---------------------------------------
using System;

namespace Common
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Order
{
public Order()
{

}

public string GetOrder(int productId)
{
//productId is not used because just example code
//test data only, I would query a database if a business object
return "1-59059-344-8|Apress |Expert C# Business Objects";
}
}//~class
}//~namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top