Enum could not be found even though it's in the same namespace - c#

I'm creating a simple Blazor RPS application for learning purposes, and I created an Enum for the results and options of the game, but whenever I try to call/use the enum, it says that it could not be found even though the same namespace is used
Main file:
#using RockPaperScissors.Client
#code
{
List<Hand> OpponentHandOptions = new List<Hand>()
{
new Hand {Choice=Options.Rock, WinAgainst=Options.Scissors, LoseAgainst=Options.Paper, Image="Rock.png"},
new Hand {Choice=Options.Paper, WinAgainst=Options.Rock, LoseAgainst=Options.Scissors, Image="Paper.png"},
new Hand {Choice=Options.Scissors, WinAgainst=Options.Paper, LoseAgainst=Options.Rock, Image="Scissors.png"}
};
}
Enum file:
namespace RockPaperScissors.Client
{
public enum Options
{
Rock, Paper, Scissors
}
}
The "Hand" class which is using the same namespace works fine and can be called

Related

Form1 is a namespace but is used like a type [duplicate]

My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'.
Could someone please tell me what this error is and how to fix it?
namespace TimeTest
{
class TimeTest
{
static void Main(string[] args)
{
Time2 t1 = new Time2();
}
}
}
I suspect you've got the same problem at least twice.
Here:
namespace TimeTest
{
class TimeTest
{
}
... you're declaring a type with the same name as the namespace it's in. Don't do that.
Now you apparently have the same problem with Time2. I suspect if you add:
using Time2;
to your list of using directives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of why it's a bad idea.)
(Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who does do exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)
namespace TestApplication // Remove .Controller
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Remove the controller word from namepsace
The class TimeTest is conflicting with namespace TimeTest.
If you can't change the namespace and the class name:
Create an alias for the class type.
using TimeTest_t = TimeTest.TimeTest;
TimeTest_t s = new TimeTest_t();
All the answers indicate the cause, but sometimes the bigger problem is identifying all the places that define an improper namespace. With tools like Resharper that automatically adjust the namespace using the folder structure, it is rather easy to encounter this issue.
You can get all the lines that create the issue by searching in project / solution using the following regex:
namespace .+\.TheNameUsedAsBothNamespaceAndType
If you're working on a big app and can't change any names, you can type a . to select the type you want from the namespace:
namespace Company.Core.Context{
public partial class Context : Database Context {
...
}
}
...
using Company.Core.Context;
someFunction(){
var c = new Context.Context();
}
I had this problem as I created a class "Response.cs" inside a folder named "Response". So VS was catching the new Response () as Folder/namespace.
So I changed the class name to StatusResponse.cs and called new StatusResponse().This solved the issue.
If you are here for EF Core related issues, here's the tip:
Name your Migration's subfolder differently than the Database Context's name.
This will solve it for you.
My error was something like this:
ModelSnapshot.cs error CS0118: Context is a namespace but is used like a type
Please check that your class and namespace name is the same...
It happens when the namespace and class name are the same.
do one thing write the full name of the namespace when you want to use the namespace.
using Student.Models.Db;
namespace Student.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<Student> student = null;
return View();
}
}
if the error is
Line 26:
Line 27: #foreach (Customers customer in Model)
Line 28: {
Line 29:
give the full name space
like
#foreach (Start.Models.customer customer in Model)

Converting Razor to C# [duplicate]

I can create an inline component like
<h1>#foo</h1>
#functions {
string foo = "foo";
}
However when I create Foo.razor containing just:
<h1>#foo</h1>
And Foo.razor.cs containing:
namespace MyApp.Client.Components {
public class Foo: ComponentBase {
public string foo;
}
}
I get:
Error CS0101 The namespace 'MyApp.Client.Components' already contains a definition for 'Foo'
I am using the latest VS 2019 and Blazor libraries.
What am I doing wrong?
Since October 2019, it is possible to use partial classes. So today you can name the class in the code-behind file like this:
public partial class Foo : ComponentBase
{
protected override Task OnInitializedAsync()
{
// do stuff
}
}
If you name the class file Foo.razor.cs it will appear under the Foo.razor razor file in solution explorer.
UPDATE: This is now possible: https://stackoverflow.com/a/59470941/1141089
Currently (May 13, 2019), the "code-behind" and .razor view can't share the same name.
So when you have Foo.razor.cs and Foo.razor it is seen as the same file and thus causes a collision.
Workaround for now:
Rename your Foo.razor.cs to FooBase.cs (or something else).
Then in your Foo.razor, add #inherits FooBase
There is a GitHub issue regarding this here: https://github.com/aspnet/AspNetCore/issues/5487
I don't like explaining ways by words but visualizing.
In Vs2019 v 16.8.4 using the experimintal Razor, a new option "Extract block to code behind"
The steps are:
Enable expermintal Razor:
Option->Environment -> Preview Featurs-> Enable expermintal Razor
editor (need restart)
Put the cursor over #{code} or press Control + . //dot
Dropdown menu is displayed with "Extract block to code behind"
Click "Extract block to code behind"
Then Vs2019 extract the code in a new *.razor.cs file being created side-by-side
e.g "Counter.razor.cs" for the counter.razor Component.
The generated class sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Lab2.Pages
{
public partial class Counter
{
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
}
Note: The generated partial class don't inherit from ComponentBase

Embeddinator-4000 : Method is getting ignored while generating .aar in Visual Studio

I am using visual studio 17 (Windows) and trying to build a .aar file from a Xamarin C# Android Library using embeddinator-4000 tool, but the method using user defined class in parameter or return type is getting ignored by tool and not coming as part of .aar
Below is the 2 classes used :
Custom Class which extend ArrayList.
using Android.Runtime;
using Java.Interop;
using Java.Util;
namespace CalculationAndroid
{
[Register("mono.embeddinator.android.ViewSubclass")]
public class ViewSubclass : ArrayList
{
[Export("ViewSubclass")]
public ViewSubclass() : base() { }
}
}
Class in which the method using the ViewSubclass in parameter.
using Android.Runtime;
using Java.Interop;
namespace CalculationAndroid
{
[Register("mono.embeddinator.android.UseViewSubclass")]
public class UseViewSubclass
{
[Export("store")]
public void Store(ViewSubclass arrayList)
{
arrayList.Add("aaa");
System.Console.WriteLine(arrayList.Get(0));
}
}
}
The class as shown in the .aar file (Decompiled).
package calculationandroid.calculationandroid;
import mono.embeddinator.*;
import com.sun.jna.*;
public class UseViewSubclass {
public com.sun.jna.Pointer __object;
public UseViewSubclass(com.sun.jna.Pointer object) { this.__object = object; }
public UseViewSubclass() {
__object = calculationandroid.Native_CalculationAndroid.INSTANCE.CalculationAndroid_UseViewSubclass_new();
mono.embeddinator.Runtime.checkExceptions();
}
}
Please help in figuring out, why the method Store is not being taken as a part of .aar file.
Thanks in advance.
I have been struggling with this for days.
The issue is with Types.
Try using basic array instead of arraylist and most probably your Store() method will not get ignored and will show up in the generated lib.
There are a number of limitations like these.
However, a complete list of limitations is not there.
Embeddinator 4000 is a great concept, but there has to be more documentation on how to build, use and interpret/fix errors in its generation log.
Hope this helps.

xamarin RewardedVideo how to replace 'this'

enter image description here
enter image description here
I have tried to copy https://forums.xamarin.com/discussion/66452/xamarin-admob-rewardedvideoad for my app but i cannot add a RewardedVideoAdListener
Every help would be appreciated
Your class appears to be derived from a class or interface in the AdsGoogle.Droid namespace, while the sample you are linking to is using a class or interface in the Android.GMS namespace. This suggests that you are trying to use a tutorial for some other product to integrate with Google Ads? The error you are getting says that the compiler doesn't know how to convert between the AdsGoogle.Droid version of the class (your class) and the Android.GMS version of the class (the class type that it is expecting).
To make the problem clear, you can have two classes with the same name as long as they are in different namespaces. The using statements at the top of the page are used to tell the compiler which version of the class to use. If it could belong to either, you have to declare the full namespace and class name, for example, System.IO.File or MyNamespace.File instead of just File.
To solve the problem, remove the line using AdsGoogle.Droid; Any errors that this causes will be coming from parts of code that don't have anything to do with the tutorial you are using - the tutorial doesn't use anything from that namespace.
using Android.Gms.Ads;
using Android.Gms.Ads.Reward;
using Xamarin.Forms;
using Android.Views;
using AdsGoogle;
using Android.Widget;
using System;
using System.Timers;
using Android.OS;
using Android.Support.V7.App;
[assembly: Dependency(typeof(AdsGoogle.Droid.AdInterstitial_Droid))]
namespace AdsGoogle.Droid
{
public class AdInterstitial_Droid : AppCompatActivity, IRewardedVideoAdListener, IAdInterstitial
{
public IRewardedVideoAd RewardedVideoAd;
public AdInterstitial_Droid()
{
RewardedVideoAd = MobileAds.GetRewardedVideoAdInstance(Android.App.Application.Context);
RewardedVideoAd.RewardedVideoAdListener = this;
//RewardedVideoAd.AdUnitId = "ca-app-pub-2667741859949498/7232000911";
LoadAd();
}
void LoadAd()
{
var requestbuilder = new AdRequest.Builder();
RewardedVideoAd.LoadAd("ca-app-pub-2667741859949498/7232000911", requestbuilder.Build());
}
public void ShowRewardedVideo()
{
if (RewardedVideoAd.IsLoaded)
{
RewardedVideoAd.Show();
//Toast.MakeText(Android.App.Application.Context, MainPage.AdCoins.ToString(), ToastLength.Long).Show();
}
LoadAd();
}
It works with this code above.
Thank you elemental Pete for helpin me.

How do I access custom fields in Acumatica C#

I'm attempting to update a custom field in GLTran. On the Account Details screen I have a button that, when clicked, should update a custom field for each selected transaction. I am unable to access the custom field in order to update it. Here's my most recent iteration of my code:
public class AccountByPeriodEnq_Extension:PXGraphExtension<AccountByPeriodEnq>
{
public PXAction<AccountByPeriodFilter> recon;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Reconcile")]
protected void Recon()
{
PXCache gltran = Base.Caches[typeof(GLTran)];
foreach (GLTran tran in gltran.Updated)
{
var GLTranEx = tran.GetExtension<GLTranExt>();
// ^^^^
//This is giving "The type or namespace name "GLTranExt" could not be found
GLTranEx.UsrRecon = true;
}
}
}
Please go easy on me as this is my first attempt at Acumatica customization.
At first glance it looks like the namespace of type GLTranExt is not accessible from the namespace of type AccountByPeriodEnq_Extension. You could put both types in the same namespace or with 'using' directive you could declare the namespace.
On second look, it could be that you have some naming mismatch as I see in some place you use:
GLTranEx
And in others:
GLTranExt
When using Customization Project Editor you can check the namespace of the DAC extension with VIEW SOURCE button:
If you have the DAC extension as a code file, namespace is in the source code:
namespace PX.Objects.GL
{
public class GLTranExt : PXCacheExtension<PX.Objects.GL.GLTran>
{
}
}
As you can see by default extension of GLTran goes into namespace PX.Objects.GL
In your code, I would expect a 'using' directive on that namespace:
using PX.Objects.GL;
public class AccountByPeriodEnq_Extension:PXGraphExtension<AccountByPeriodEnq>
{
}
Or put the namespace prefix on the type:
PX.Objects.GL.GLTranExt GLTranEx = tran.GetExtension<PX.Objects.GL.GLTranExt>();

Categories