I have written the following code in visual studio 2019, but it gives me an error saying that BitVector32 is a namespace but is used as a type here and CreateMask() method is not existing in the BitVector32 namespace
using System;
using System.Collections.Specialized;
namespace BitVector32
{
class Program
{
static void Main(string[] args)
{
basicVector();
}
public static void basicVector()
{
BitVector32 b = new BitVector32(0);
int myBit1 = BitVector32.CreateMask();
int myBit2 = BitVector32.CreateMask(myBit1);
int myBit3 = BitVector32.CreateMask(myBit2);
int myBit4 = BitVector32.CreateMask(myBit3);
int myBit5 = BitVector32.CreateMask(myBit4);
}
}
}
i referred the Microsoft doc at https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.bitvector32?view=netcore-3.1 and did the same but it gives the above mentioned errors
This is due to your namespace at the top being BitVector32. Change the namespace to another name other than BitVector32:
using System;
using System.Collections.Specialized;
namespace SomethingOtherThanBitVector32
{
class Program
{
static void Main(string[] args)
{
basicVeector();
}
public static void basicVeector()
{
BitVector32 b = new BitVector32(0);
int myBit1 = BitVector32.CreateMask();
int myBit2 = BitVector32.CreateMask(myBit1);
int myBit3 = BitVector32.CreateMask(myBit2);
int myBit4 = BitVector32.CreateMask(myBit3);
int myBit5 = BitVector32.CreateMask(myBit4);
}
}
}
Related
I have two classes, one for defining the algorithm parameters and another to implement the algorithm:
Class 1 (algorithm parameters):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public static class AlgorithmParameters
{
public static int pop_size = 100;
public static double crossover_rate = 0.7;
public static double mutation_rate = 0.001;
public static int chromo_length = 300;
public static int gene_length = 4;
public static int max_allowable_generations = 400;
static Random rand = new Random();
public static double random_num = rand.NextDouble();
}
}
Class 2 (implement algorithm):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public class Program
{
public struct chromo_typ
{
public string bits;
public float fitness;
//public chromo_typ(){
// bits = "";
// fitness = 0.0f;
//}
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
public static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng =
new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
public string GetRandomBits()
{
string bits="";
for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i++)
{
if (VM_Placement.AlgorithmParameters.random_num > 0.5f)
bits += "1";
else
bits += "0";
}
return bits;
}
public static void Main(string[] args)
{
Random rnd = new Random(GetRandomSeed());
while (true)
{
chromo_typ[] Population = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
double Target;
Console.WriteLine("\n Input a target number");
Target = Convert.ToDouble(Console.ReadLine());
for (int i = 0; i < VM_Placement.AlgorithmParameters.pop_size; i++)
{
Population[i].bits = GetRandomBits();
Population[i].fitness = 0.0f;
}
}
}
}
}
I am getting an error on Population[i].bits = GetRandomBits(); in Main().
Error is:
An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()'
Am I missing anything?
The Main method is Static. You can not invoke a non-static method from a static method.
GetRandomBits()
is not a static method. Either you have to create an instance of Program
Program p = new Program();
p.GetRandomBits();
or make
GetRandomBits() static.
It looks like you want:
public static string GetRandomBits()
Without static, you would need an object before you can call the GetRandomBits() method. However, since the implementation of GetRandomBits() does not depend on the state of any Program object, it's best to declare it static.
The Main method is static inside the Program class. You can't call an instance method from inside a static method, which is why you're getting the error.
To fix it you just need to make your GetRandomBits() method static as well.
I have two classes, one for defining the algorithm parameters and another to implement the algorithm:
Class 1 (algorithm parameters):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public static class AlgorithmParameters
{
public static int pop_size = 100;
public static double crossover_rate = 0.7;
public static double mutation_rate = 0.001;
public static int chromo_length = 300;
public static int gene_length = 4;
public static int max_allowable_generations = 400;
static Random rand = new Random();
public static double random_num = rand.NextDouble();
}
}
Class 2 (implement algorithm):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public class Program
{
public struct chromo_typ
{
public string bits;
public float fitness;
//public chromo_typ(){
// bits = "";
// fitness = 0.0f;
//}
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
public static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng =
new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
public string GetRandomBits()
{
string bits="";
for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i++)
{
if (VM_Placement.AlgorithmParameters.random_num > 0.5f)
bits += "1";
else
bits += "0";
}
return bits;
}
public static void Main(string[] args)
{
Random rnd = new Random(GetRandomSeed());
while (true)
{
chromo_typ[] Population = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
double Target;
Console.WriteLine("\n Input a target number");
Target = Convert.ToDouble(Console.ReadLine());
for (int i = 0; i < VM_Placement.AlgorithmParameters.pop_size; i++)
{
Population[i].bits = GetRandomBits();
Population[i].fitness = 0.0f;
}
}
}
}
}
I am getting an error on Population[i].bits = GetRandomBits(); in Main().
Error is:
An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()'
Am I missing anything?
The Main method is Static. You can not invoke a non-static method from a static method.
GetRandomBits()
is not a static method. Either you have to create an instance of Program
Program p = new Program();
p.GetRandomBits();
or make
GetRandomBits() static.
It looks like you want:
public static string GetRandomBits()
Without static, you would need an object before you can call the GetRandomBits() method. However, since the implementation of GetRandomBits() does not depend on the state of any Program object, it's best to declare it static.
The Main method is static inside the Program class. You can't call an instance method from inside a static method, which is why you're getting the error.
To fix it you just need to make your GetRandomBits() method static as well.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 3 classes:
First and second class - have variables as listed below:
First class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project_1
{
class First_class
{
int variable_1 = 1;
int variable_2 = 1;
int variable_3 = 3;
string variable_4 = "test1"
}
}
Second class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project_1
{
class Second_class
{
int variable_5 = 5;
int variable_6 = 6;
int variable_7 = 7;
string varaible_8 = "test_2"
}
}
Third class is empty::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project_1
{
class Program
{
static void Main(string[] args)
{
}
}
}
What should I do to have possibility to use all variables from First class - in Third Class ?
What should I do to have possibility to use only some variables from First class and only some variables from Second class - in Third Class ?
If you want to use those variables outside of First_class, they must be declared as public variables.
class First_class
{
public static int variable_1 = 1; // This can be accessed from outside
public int variable_2 = 1; // This can be accessed from outside too
int variable_3 = 3; // This can not be accessed from outside
string variable_4 = "test1"; // This also can not be accessed from outside
}
class Program
{
static void Main(string[] args)
{
var o = new First_class();
Console.Writeline(First_class.variable_1);
Console.Writeline(o.variable_2);
Console.Writeline(o.variable_3); // Error: variable_3 is private
}
}
1.Here you have two possibilities:
a) To use all variables from First class in Third Class you have to make all of them public.
b) You can make all of them static and public or protected but in such case, the Program class needs to inherit
from the First class so it would look like:
public class First_class
{
protected static int variable_1 = 1;
protected static int variable_2 = 1;
protected static int variable_3 = 3;
protected static string variable_4 = "test1";
}
class Program: First_class
{
static void Main(string[] args)
{
Console.WriteLine(variable_1);
Console.WriteLine(variable_2);
Console.WriteLine(variable_3);
Console.WriteLine(variable_4);
}
}
2.The example:
class Second_class
{
int variable_5 = 5;
int variable_6 = 6;
int variable_7 = 7;
public string varaible_8 = "test_2"; //only this field can be accessed
}
public class First_class
{
public int variable_1 = 1;
public int variable_2 = 1;
protected int variable_3 = 3; // can be accessed via inheritance
private string variable_4 = "test1"; // cannot be accessed due to privacy level
}
class Program
{
static void Main(string[] args)
{
var obj1 = new First_class();
Console.WriteLine(obj1.variable_1);
Console.WriteLine(obj1.variable_2);
var obj2 = new Second_class();
Console.WriteLine(obj2.varaible_8);
}
}
From my main code I'm calling a method of a class meth2 which calls another method from another class meth1. It is important for me to respect this structure.
These methods make an assignment of values that are previously defined in the classes but I am not able to get a proper result in my command window (just a 0 instead of a 132). I assume I'm missing something.
Does anybody has an idea? Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace workingWithClasses
{
class meth1
{
public int op1;
public int multiply(int mult)
{
return op1 = 44 * mult;
}
}
class meth2
{
public int CallFuncsClass(int multiplicator)
{
return m1.multiply(int multiplicator);
}
}
class Program
{
static void Main(string[] args)
{
meth1 m1 = new meth1();
meth2 m2 = new meth2();
m2.CallFuncsClass(3);
int result_m1 = m1.op1;
Console.WriteLine(opm1);
Console.ReadKey();
}
}
}
Thanks a lot!
namespace workingWithClasses
{
public class meth1
{
public int op1;
public int multiply(int mult)
{
return op1 = 44 * mult;
}
}
public class meth2
{
meth1 m1 = new meth1();
public int CallFuncsClass(int multiplicator)
{
return m1.multiply( multiplicator);
}
}
class Program
{
static void Main(string[] args)
{
meth2 m2 = new meth2();
int result_m2 = m2.CallFuncsClass(3);
Console.WriteLine(result_m2);
Console.ReadKey();
}
}
}
This code won't compile, right? This line return m1.multiply(int multiplicator); is out of the place. You need to define what is m1. I guess you are looking for dependency injection. You can do this via constructor, so
class meth2
{
private meth1 _m1;
meth2(meth1 m1)
{
if(m1 == null) throw new ... // check input params
_m1 = m1;
}
public int CallFuncsClass(int multiplicator)
{
return _m1.multiply(int multiplicator);
}
}
The usage will be
meth1 m1 = new meth1();
meth2 m2 = new meth2(m1);
m2.CallFuncsClass(3);
Bonus points
Name your classes correctly, instead of meth1 it should be something like Calculator
Don't use public fields: public int op1;. It should be private
Usually you would want your classes to be public, by default the class is internal. In this way you can use it outside a single library
Check parameters for correct value, throw exception if something is incorrect
Make multiply static on class meth1:
public static int multiply(int mult)
Also don't use 'op1', just return the result of the operation.
Call like this:
return meth1.multiply(int multiplicator);
Here is the solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace workingWithClasses
{
class meth1
{
public int op1;
public int multiply(int mult)
{
return op1 = 44 * mult;
}
}
class meth2
{
public int CallFuncsClass(int multiplicator)
{
meth1 m1=new meth1();
return m1.multiply(multiplicator);
}
}
class Program
{
static void Main(string[] args)
{
meth2 m2 = new meth2();
int result_m1=m2.CallFuncsClass(3);
Console.WriteLine(result_m1);
Console.ReadKey();
}
}
}
If you want to call methods from meth1 from within meth2 like that (without creating an object of type meth1) you need to make multiply() static.
This will allow it to be used in the manner you're using:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace workingWithClasses
{
class meth1
{
//note statics
static int op1;
static int multiply(int mult)
{
return op1 = 44 * mult;
}
}
class meth2
{
public int CallFuncsClass(int multiplicator)
{
//access to multiply() is valid here
return meth1.multiply(multiplicator);
}
}
class Program
{
static void Main(string[] args)
{
meth1 m1 = new meth1();
meth2 m2 = new meth2();
m2.CallFuncsClass(3);
int result_m1 = m1.op1;
Console.WriteLine(opm1);
Console.ReadKey();
}
}
}
I have two classes, one for defining the algorithm parameters and another to implement the algorithm:
Class 1 (algorithm parameters):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public static class AlgorithmParameters
{
public static int pop_size = 100;
public static double crossover_rate = 0.7;
public static double mutation_rate = 0.001;
public static int chromo_length = 300;
public static int gene_length = 4;
public static int max_allowable_generations = 400;
static Random rand = new Random();
public static double random_num = rand.NextDouble();
}
}
Class 2 (implement algorithm):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public class Program
{
public struct chromo_typ
{
public string bits;
public float fitness;
//public chromo_typ(){
// bits = "";
// fitness = 0.0f;
//}
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
public static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng =
new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
public string GetRandomBits()
{
string bits="";
for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i++)
{
if (VM_Placement.AlgorithmParameters.random_num > 0.5f)
bits += "1";
else
bits += "0";
}
return bits;
}
public static void Main(string[] args)
{
Random rnd = new Random(GetRandomSeed());
while (true)
{
chromo_typ[] Population = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
double Target;
Console.WriteLine("\n Input a target number");
Target = Convert.ToDouble(Console.ReadLine());
for (int i = 0; i < VM_Placement.AlgorithmParameters.pop_size; i++)
{
Population[i].bits = GetRandomBits();
Population[i].fitness = 0.0f;
}
}
}
}
}
I am getting an error on Population[i].bits = GetRandomBits(); in Main().
Error is:
An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()'
Am I missing anything?
The Main method is Static. You can not invoke a non-static method from a static method.
GetRandomBits()
is not a static method. Either you have to create an instance of Program
Program p = new Program();
p.GetRandomBits();
or make
GetRandomBits() static.
It looks like you want:
public static string GetRandomBits()
Without static, you would need an object before you can call the GetRandomBits() method. However, since the implementation of GetRandomBits() does not depend on the state of any Program object, it's best to declare it static.
The Main method is static inside the Program class. You can't call an instance method from inside a static method, which is why you're getting the error.
To fix it you just need to make your GetRandomBits() method static as well.