I have recently converted a VB class to C# and it seems I ran into a problem; I think I know how to solve it but with all my reading I am now looking for a more clear answer with guidance.
Consider the following code with a struct FileDetail inside (This is just an example - so please do not assume it is FileDetail as in Files..)
The Struct needs to be accessed internally and externally - they are passed by value and not reference types so struct appears to be the way to go here instead of a class (looking at the whole code that is). In the form class MyForm I get the error that FileDetails does not exist in class IAFT.
public class IAFT
{
public struct FileDetail
{
public string FileType;
public int FileNumber;
}
}
public class MyForm
{
MyForm()
{
public IAFT.FileDetail fd = new IAFT.FileDetail();
// IAFT.FileDetail
}
}
ERROR I get.
The type name 'FileDetail' does not exist in the type 'IAFT' (CS0426)
Red squigly in VS2013 is under the type declaration; left hand side of the assignment.
Both are in the same namespace if that is any help.
I have read posts on SO that tell me I can declare my variable fd just as it is above (did not make sense to me since I have no instance - but I tried it.) I do not want to create an instance to get an instance ; I believe I want the one as it exists inside the instance of IAFT. Maybe there is something I am not understanding.
Should I encapsulate the struct as a class instead?
Should I put the struct outside of the class IAFT?
[This is what I was thinking I should do.]
Should I do something else ?
Your code example is wrong and wont compile. Your error is that you are delcaring
public IAFT.FileDetail fd = new IAFT.FileDetail();
Inside the public constructor. You cannot declare scope in a function scoped variable. If you take the Public declaration off your code compiles just fine.
So try this;
public class IAFT
{
public struct FileDetail
{
public string FileType;
public int FileNumber;
}
}
public class MyForm
{
public MyForm()
{
IAFT.FileDetail fd = new IAFT.FileDetail();
}
}
Its perfectly good practise to nest classes.
There's no need to do the struct static nor moving it outside, the problem lies that you declared the var as public inside a function, and that's not allowed, localvariables don't have access modifiers as they're local.
Try this:
public class IAFT
{
public struct FileDetail
{
public string FileType;
public int FileNumber;
}
}
public class MyForm
{
public IAFT.FileDetail fd = new IAFT.FileDetail();
}
I would to it this way:
public class IAFT
{
fileDetail FileDetail = new fileDetail();
}
public class fileDetail
{
public string FileType;
public int FileNumber;
}
public class MyForm
{
MyForm()
{
IAFT.FileDetail.FileType = "test";
//IAFT.FileDetail
}
}
I hope that it works, I haven't tested it.
Related
I am trying to make a c# mod for a game. In order to do so, first I wanted to recreate some classes, methods, etc. in a simpler way. So far I have this code (everything is in the same namespace):
class MyWeapons
{
public class SL_Item
{
public SL_ItemStats StatsHolder;
public int New_ItemID;
public int Target_ItemID;
}
public class SL_Weapon : SL_Item
{
public bool? Unblockable;
public float? HealthLeechRatio;
}
public class SL_Damage
{
public float Damage;
public string Type;
}
public class SL_ItemStats
{
public int BaseValue;
}
public class SL_WeaponStats : SL_ItemStats
{
public List<SL_Damage> BaseDamage;
public float AttackSpeed;
}
}
public static void Init()
{
var myItem = new SL_Weapon()
{
StatsHolder = new SL_WeaponStats()
{
//adding new SL_Damage to the BaseDamage list when initializing it
BaseDamage = new List<SL_Damage>() { new SL_Damage { Damage = 25f, Type = "Something" } },
}
};
myItem.StatsHolder.AttackSpeed = 5f;
}
Now, the problem is, that in the last line the program 'thinks' that AttackSpeed is a property of SL_ItemStats (because StatsHolder is of that type; the error says: SL_ItemStats does not contain a definition for AttackSpeed...). But the class SL_WeaponStats inherits from SL_ItemStats and I made StatsHolder a new object of the type SL_WeaponStats. Now, the thing I absolutely don't understand is that I can access StatsHolder properties of the SL_WeaponStats class in the object initializer (BaseDamage in my code; but I also tried with AttackSpeed and it works fine). However I cannot access the same properties after the object (myItem) already exists (the last line).
You are hitting upon the distinction between an object's static type and its dynamic type.
The dynamic type of a variable is the type of the object it holds while running.
The static type of a variable is the type it's defined as in the code.
The compiler can only know a variable's static type. StatsHolder is defined as a SL_ItemStats, so as far as the compiler is concerned, that's all it can be. T.S.'s answer shows how to enlighten the compiler as to the wider uses of StatsHolder.
Works by design.
public class SL_Item
{
// you declared StatsHolder as SL_ItemStats
public SL_ItemStats StatsHolder;
.....
// you derived to its superclass
StatsHolder = new SL_WeaponStats()
your expectations are false because StatsHolder does not have AttackSpeed. The base SL_WeaponStats has it.
This works
((SL_WeaponStats)myItem.StatsHolder).AttackSpeed = 5f;
public class MyClass
{
public int x;
}
public class MyClass2: MyClass
{
x=1;
}
I'm trying to access variable x from base class but I get an error saying that the name x does not exists in current context.
You can access it from inside a method like this:
public class MyClass2: MyClass
{
private void MyMethod()
{
x=1;
}
}
Or from a different class:
var test = new MyClass2();
test.x = 1;
If you intend for your x field to be public, you should consider changing it to a property and capitalizing it, like this:
public int X { get; set; }
You can read about the difference between fields and properties here. If you do make it a property, be sure to follow Microsoft design guidelines and use PascalCase. See here.
Try to make your variables public and if this doesn't work try to initialize your variable inside the MyClass and not in the MyClass2
Also happy new year
(sorry for my bad english)
Hi trying to make a class inside a static class to use in JINT but when it's referenced I get an error
C# code
namespace Hi {
public static class Ok {
public class Wowa {
public Wowa(){}
}
}
}
But when I try to make a new one in JavaScript I get an error "the object cannot be used as a constructor" from JINT
var k = new Hi.Ok.Wowa()
Am I doing this right? How can I set up the C# to be able to use the above code in JavaScript from JINT?
BTW IF instead of "Ok" being a static class, rather a namespace, it works, but I want it as a class because I want to have static methods in it also
you cant use none-static class in a static class (ReadThis) but if you remove (static) in your frist class
namespace Hi {
public class Ok {
public class Wowa {
public Wowa(){}
}
}
}
and it can be said that it does not make much difference because (Static) only makes subcategories of your class have to use (Static).
But if you want your class to be impossible to build on variables, you can use abstract(ReadThis)
namespace Hi {
public abstract class Ok {
public class Wowa {
public Wowa(){}
}
}
}
and
Main()
{
Ok k = new Ok();//Error
}
Imagine you have this:
namespace Hi
{
public static class Ok
{
public class Wowa
{
public Wowa() { }
public static string MyStaticMethod() => "Hello from 'Static Method'";
public string MyNormalMethod() => "Hello from 'Normal Method'";
}
}
}
It's possible to use non-static class Wowa by making an instance of it , and then you can call MyNormalMethod of that instance (you can only call not-static method within instance of that class).
Hi.Ok.Wowa wowa = new Hi.Ok.Wowa();
wowa.MyNormalMethod();
And without making any instance of Wowa you can call static method within it, like this:
Hi.Ok.Wowa.MyStaticMethod();
Finally you can see working code here.
Is it possible to force C# compiler to treat var as a keyword and not as a class identifier when a class of name var is declared?
public class var
{
}
public class A
{
}
public class Program
{
public static void Main()
{
var a=new A{}; // Cannot implicitly convert type 'A' to 'var'
}
}
https://dotnetfiddle.net/O1wVoO
In your code, you can no longer use var as a keyword within the context.
var is a contextual keyword (there are two types of keywords: normal and contextual), where it behaves like a keyword within its respective contexts where it is used as a type name, but can still be used as an identifier (not reserved).
You declared it as class name therefore it loses its special meaning within the context.
Can you declare var class name as #var and check. #forces keywords to behave like variables.
yes of course you can do that:
namespace vartest
{
public class var {
public var()
{
MessageBox.Show("It is me!");
}
}
}
namespace myform {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
vartest.var theVar = new vartest.var();
var a = "whatever";
}
}
}
As you can understand the trick is in the namespace ;-)
Thus in your case to protect the c# var, define your var class at its own namespace which you should not be "using".
Cheers
This is a noob question
using System.name;
class class_name
{
private className Obj;
public class_name()
{
}
public function()
{
Obj.function <----- why i cant acesss the global varible here ??
}
}
When i type the class the instellisence docent show any thing :-s
I'm assuming there was just some confusion with the names and, by function, you meant class_name, or instead of class_name you meant className.
In order to access a method this way, it must be declared as static. Otherwise, you must first create an instance of the class and access the method through the instance.
EDIT The code you posted is very confusing. The following works just fine for me.
class Class1
{
public void Function1()
{
}
}
class Class2
{
private Class1 obj;
public void Function2()
{
obj.Function1();
}
}
Have you instantiated that class?