How to resolve the class name conflict problem in C# - c#

I use a third party dll which does not use namespace, it contains a enum named Speaker.
// Decompiled with JetBrains decompiler
// Type: Speaker
// Assembly: StreamSDK, Version=1.0.6782.19546, Culture=neutral,PublicKeyToken=null
// MVID: 82353EB3-505A-4A47-8EEB-ED74ED4FC9B9
// Assembly location: /Users/haha/test/Assets/_ThirdParty/SteamSDK/Core/XMLSerializer/StreamSDK.dll
public enum Speaker
{
remote,
local,
none,
}
My local project also has this class name under a specified namespace Photon.Voice.Unity;. After I import the dll, error happens because the compiler treats the local Speaker as the third party's Speaker.
I already use namespace in my local project:
using Photon.Voice.Unity;
Error happens in the following codes :
private void OnSpeakerCreated(Speaker speaker)
{
speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}
The error:
error CS1061: 'Speaker' does not contain a definition for 'gameObject' and no accessible extension method 'gameObject' accepting a first argument of type 'Speaker' could be found (are you missing a using directive or an assembly reference?)
After I add the full namespace, the codes are passed.
private void OnSpeakerCreated(Photon.Voice.Unity.Speaker speaker)
{
speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}
But I don't want to do that I just want to ban the use of the third party Speaker in the specified cs files or any other ways that I don't need to change the current codes.

If you are just lazy to write Photon.Voice.Unity.Speaker every time, you can create an alias for using a using alias directive:
using PhotonSpeaker = Photon.Voice.Unity.Speaker;
Now you can write:
private void OnSpeakerCreated(PhotonSpeaker speaker)
{
speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}

Related

Best way to reuse C# code to pass system.servicemodel.security.usernamepasswordclientcredential credentials to different class files

I'm a beginner at writing and understanding C#. I would like to know how to reuse some code I have that updates a WebService username and password credentials.
Here is code in 1st cs File:
...
public class AuthenticateLogin
{
public static object PassCredentials()
{
ServiceName.UsersClient clientauthentication = new ServiceName.UsersClient();
clientauthentication.ClientCredentials.UserName.UserName = "user";
clientauthentication.ClientCredentials.UserName.Password = "pwd";
}
}
...
Here is code in 2nd cs File:
var test = AuthenticateLogin.PassCredentials();
Console.WriteLine(test.EchoAuthenticated("Successful Login"));
Error Received:
Error CS1061 'AuthenticateLogin' does not contain a definition for 'EchoAuthenticated' and no accessible extension method 'EchoAuthenticated' accepting a first argument of type 'AuthenticateLogin' could be found (are you missing a using directive or an assembly reference?)
Solution desired:
In the second file, I want to be able to use the methods in the 'clientauthentication' of the first file. I need to be able to use the 'clientauthentication' like a common object in multiple other cs files. FYI: I can use the 'clientauthentication' methods fine in the 1st file and it works okay.

C# Missing Reference

When running this code:
using System;
using System.Text.RegularExpressions;
namespace WebAutomationDemo
{
public class ExtractDigitsFromString
{
public static void Main(string[] args)
{
string inputData = "No 38, Bunder Garden Street, Perambur, Chennai - 600011";
var data = Regex.Match(inputData, "/s([a - z][A - Z]) +/s -/s/d[6]").Value;
Console.WriteLine(data);
Console.ReadLine();
}
}
}
I get the following error:
The type or namespace name 'RegularExpressions' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
This happens even after referencing System.Text.RegularExpressions assembly.
Are you still facing the same error,may be your assembly System is missing RegularExpressions method as stated by many.
Goto Object browser i.e click on any project solutions's references(expand) open any assembly will take it to take object browser window.
Now check whether the System.Text contains the RegualrExpressions method.
Object browser
If you follow the link there is an image of my object browser that do contains System.Text.RegularExpressions assembly.
If its missing ,download a valid assembly from MSDN
Below is one of link for a System.dll,download it or google it you can downlaod it easily.
https://www.dllme.com/dll/files/system_dll.html
Then goto references of your project and then add the downloaded assembly.

The type or namespace name 'Stack' could not be found (only with .NET 4.0)

I get an error when i compile this program:
using System;
using System.Collections.Generic;
static class main {
public static void Main() {
Stack<int> a;
}
};
with this cmd:
C:\Users\tomc\Desktop\l>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc test.cs
I get this error:
test.cs(12,3): error CS0246: The type or namespace name 'Stack' could not be
found (are you missing a using directive or an assembly reference?)
However when using this command it builds fine:
C:\Users\tomc\Desktop\l>C:\Windows\Microsoft.NET\Framework64\v3.5\csc test.cs
Was Stack removed from .NET 4 ? I can't find any indication that it was on MSDN.
The C# compiler has a default "configuration" file (called a response file) located in the .NET Framework installation directory (c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.rsp). This response file is just a text file that contains a set of compiler command-line switches that are passed by default when compiling a program. You can also specify your own file in addition to the default one (see the link above).
In your case, this file might be missing or corrupted. You can check whether the references to the core libraries are present there. For example, here is the csc.rsp file from my machine:
# This file contains command-line options that the C#
# command line compiler (CSC) will process as part
# of every compilation, unless the "/noconfig" option
# is specified.
# Reference the common Framework libraries
/r:Accessibility.dll
/r:Microsoft.CSharp.dll
/r:System.Configuration.dll
/r:System.Configuration.Install.dll
/r:System.Core.dll
/r:System.Data.dll
/r:System.Data.DataSetExtensions.dll
/r:System.Data.Linq.dll
/r:System.Data.OracleClient.dll
/r:System.Deployment.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceModel.dll
/r:System.ServiceModel.Web.dll
/r:System.ServiceProcess.dll
/r:System.Transactions.dll
/r:System.Web.dll
/r:System.Web.Extensions.Design.dll
/r:System.Web.Extensions.dll
/r:System.Web.Mobile.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.Workflow.Activities.dll
/r:System.Workflow.ComponentModel.dll
/r:System.Workflow.Runtime.dll
/r:System.Xml.dll
/r:System.Xml.Linq.dll

missing a using directive or an assembly reference?

I am getting this error Error 4 The type or namespace name 'Sample' could not be found (are you missing a using directive or an assembly reference?) F:\oadmin\sea\Controls\Dlg.cs 11 7 Controls
here i added Samplein reference section,,,then i used it as Using Sample,,i dont know why i am getting this error
this is the class
namespace sample
{
public class Server
{
public class client
{
Bool sent = true
}
}
}
Can i share exe,,this Sample is an exe
Perhaps because your namespace is sample and not Sample?
So if your Dlg namespace is different it needs to have a using sample line not using Sample.
Or perhaps your class is in another project within your solution and you need to include the hierarchy of namespaces?
Your namespace "sample" is lowercase, but you are referring to it in uppercase "Sample" ...
EDIT: You Bool sent is internal, it cannot be accessed from outside of the assembly

Assembly access

If you have a assembly identity/namespace of Library.Testing.
Then you created another assembly with identity/namespace of Library.Testing.One
Library.Testing.One project references Library.Testing.
Why is it you have to use using Library.Testing; in your classes in Library.Testing.One to access anything in Library.Testing?
Example1:
using System;
namespace Library.Testing.One
{
// 'Library.Testing' is a reference in this assembly
public class foo : Library.Testing.BooBase
{
}
}
This does not work I get two exception
Warning 1 Load of property
'RootNamespace' failed. The string
for the root namespace must be a valid
identifier. Error 2 The type or
namespace name 'BooBase' does not
exist in the namespace
'Library.Testing.One.Library.Testing'
(are you missing an assembly
reference?)
Example2:
using System;
using Library.Testing;
namespace Library.Testing.One
{
// 'Library.Testing' is a reference in this assembly
public class foo : Library.Testing.BooBase
{
}
}
This works!
Adding a "using" for Library.Testing.One does not automatically bring everything in Library and Library.Testing into scope. The fact that the namespaces appear to be hierarchical is probably what's leading to your confusion.
Think of, for example, adding using System.Data.SqlClient to a file. That doesn't automatically bring everything in System and System.Data into scope.

Categories

Resources