when assign static variable in a class, would that procedure run everytime we initialize new class? - static

I have the class
class CongNhan
{
public static decimal pr1= LoadFromDB(query1);
public static decimal pr2= LoadFromDB(query2);
public static int pr3= LoadFromBD(query3);
private string name;
public CongNhan()
{
name = "";
}
public CongNhan(string Name)
{
name = Name;
}
}
The question is how many times does 3 assigns to the static variables run. And if we new Class like: new CongNhan(), will it call three first assigns.
Because the static variables get value from Database so knowing how many times is it called is so much important to optimize and make it run faster.
Thank you!

AFAIK, static members of a class are initialized when app-domain with the class gets loaded into memory. So the three fields pr1, pr2, pr3 will be initialized once every time app-domain gets loaded. If you new up the class using new CongNhan(), those static fields will not be initialized again.

Related

How do I call this list from another class in C#?

I am still new to the whole C# thing but I found this code posted from grovesNL about 5 years ago which I believe will work.
namespace DataAccessClass
{
public class FileReader
{
static void Main(string[] args)
{
List<DailyValues> values = File.ReadAllLines("C:\\Users\\Josh\\Sample.csv")
.Skip(1)
.Select(v => DailyValues.FromCsv(v))
.ToList();
}
}
public class DailyValues
{
DateTime Date;
decimal Open;
decimal High;
decimal Low;
decimal Close;
decimal Volume;
decimal AdjClose;
public static DailyValues FromCsv(string csvLine)
{
string[] values = csvLine.Split(',');
DailyValues dailyValues = new DailyValues();
dailyValues.Date = Convert.ToDateTime(values[0]);
dailyValues.Open = Convert.ToDecimal(values[1]);
dailyValues.High = Convert.ToDecimal(values[2]);
dailyValues.Low = Convert.ToDecimal(values[3]);
dailyValues.Close = Convert.ToDecimal(values[4]);
dailyValues.Volume = Convert.ToDecimal(values[5]);
dailyValues.AdjClose = Convert.ToDecimal(values[6]);
return dailyValues;
}
}
}
I am trying to read a csv file skipping the header and get it into a list that is accessible from another class. So my Architecture is DataAccessClass that has a class called FileReader and a class called Values. My task is to read this csv file into class FileReader and then to create an object list to hold it in the class Values. When I go to the Values class to call it I can't figure it out. This is how I am trying to call it. It is saying DailyValues.FromCsv(string) is a method that is not valid.
public List<string> GetList()
{
return DataAccessClass.DailyValues.FromCsv.dailyValues;
}
I want to be able to access this list further up the stack.
Your expression DataAccessClass.DailyValues.FromCsv.dailyValues is the culprit.
DataAccessClass.DailyValues.FromCsv is valid, and references the static method named FromCsv in the class DataAccessClass.DailyValues. But then going on by adding .dailyValues is incorrect. It is a method, nothing to peek into and extract stuff using ..
You could (if that was the intention) call the function, and directly work with the result:
DataAccessClass.DailyValues.FromCsv(some_csv_string) is an expression of type DailyValues. There you could then access - as an example - 'High' with:
DailyValues dv;
dv = DataAccessClass.DailyValues.FromCsv(some_csv_string);
dosomething(dv.High);
But for that to work, High would have to have the visibility of public.

Difference in the declaration beyween two syntaxes

Ref : Cannot reference "X" before supertype constructor has been called, where x is a final variable
class ArrayFunctions {
//public Integer[] arrayTemplate;
private static Scanner scanner = new Scanner(System.in);
ArrayFunctions(int k){
Integer[] arrayTemplate = new Integer[k] ;
}
.
.
.
public class ArrayFunctionsImplementation{
public static void main(String[] args) {
ArrayFunctions newArray = new ArrayFunctions(5);
newArray.getIntegers(newArray.arrayTemplate);
newArray.printIntegers(newArray.arrayTemplate);
newArray.sortArray(newArray.arrayTemplate);
newArray.printIntegers(newArray.arrayTemplate);
}
}
}
If I use the declaration //public Integer[] arrayTemplate; that is currently commented out , I am able to access the variable "arrayTemplate" in the public class.
But if I declare the variable by calling the constructor as per the code below, I am unable to access it anywhere. If I understand correctly, both the ways declare the variable and by the time I am trying to access it , the object is already created.
PS : I am using Integer class just for experimentation instead of using plain int
Cheers
Your current code declares a variable of the ArrayFunctions constructor and so, that variable is only accessible in your constructor.
Your commented code, declares a member of the ArrayFunctions class, which then can be accessed anywhere from the class (or elsewhere since you made it public).

How to define an array in hadoop partitioner

I am new in hadoop and mapreduce programming and don't know what should i do. I want to define an array of int in hadoop partitioner. i want to feel in this array in main function and use its content in partitioner. I have tried to use IntWritable and array of it but none of them didn't work . I tried to use IntArrayWritable but again it didn't work. I will be pleased if some one help me. Thank you so much
public static IntWritable h = new IntWritable[1];
public static void main(String[] args) throws Exception {
h[0] = new IntWritable(1);
}
public static class CaderPartitioner extends Partitioner <Text,IntWritable> {
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
if you have limited number of values, you can do in the below way.
set the values on the configuration object like below in main method.
Configuration conf = new Configuration();
conf.setInt("key1", value1);
conf.setInt("key2", value2);
Then implement the Configurable interface for your Partitioner class and get the configuration object, then key/values from it inside your Partitioner
public class testPartitioner extends Partitioner<Text, IntWritable> implements Configurable{
Configuration config = null;
#Override
public int getPartition(Text arg0, IntWritable arg1, int arg2) {
//get your values based on the keys in the partitioner
int value = getConf().getInt("key");
//do stuff on value
return 0;
}
#Override
public Configuration getConf() {
// TODO Auto-generated method stub
return this.config;
}
#Override
public void setConf(Configuration configuration) {
this.config = configuration;
}
}
supporting link
https://cornercases.wordpress.com/2011/05/06/an-example-configurable-partitioner/
note if you have huge number of values in a file then better to find a way to get cache files from job object in Partitioner
Here's a refactored version of the partitioner. The main changes are:
Removed the main() which isnt needed, initialization should be done in the constructor
Removed static from the class and member variables
public class CaderPartitioner extends Partitioner<Text,IntWritable> {
private IntWritable[] h;
public CaderPartitioner() {
h = new IntWritable[1];
h[0] = new IntWritable(1);
}
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
Notes:
h doesn't need to be a Writable, unless you have additional logic not included in the question.
It isn't clear what the h[] is for, are you going to configure it? In which case the partitioner will probably need to implement Configurable so you can use a Configurable object to set the array up in some way.

What do you mean by stateless in static method?

Static method should not contain a state. What does 'state' means here ?
I have read that static method do not need to be instantiated, and do not use instance variables. So when can I use static methods? I have read that static methods are bad? Should I include it when coding?
State means storing some information, static methods are loaded when a class is loaded so there is no need of instance to call the static methods, you can call this methods using name of class, it's depend on condition when to use static methods. you can use static methods as single component of product just pass your parameters and get your work done.
As an answer here's an example:
public class SomeUtilityClass {
private static boolean state = false;
public static void callMeTwiceImBad() throws Exception {
if (state) {
throw new Exception("I remember my state from previous call!");
}
state = true;
}
public static int sum(int a, int b) {
return a + b;
}
}
By themselves they are neither bad nor good, they are just static.

C#/WinForms: Sets and Gets Value of Static Variable

I have the following Global class file:
Global.cs
public static class Global
{
private static string _globalVar = "";
public static string GlobalVar
{
get { return _globalVar; }
set { _globalVar = value; }
}
}
I set the new value of string GlobarVar in Form1.cs as '1234'.
Form1.cs
public Form1()
{
InitializeComponent();
Global.GlobalVar = "1234";
}
I tried to display the value to Form2.cs using the message box
public Form2()
{
InitializeComponent();
MessageBox.Show(Global.GlobalVar); // displays blank values
}
Am I missing something?
Four options:
You're not constructing Form1 before you construct Form2
Something else is setting Global.GlobalVar back to null or an empty string
Your forms are in different app domains, so they'll have entirely separate Global types
You're running the application twice; static variables don't live on across different processes
It's hard to tell which of these is the case, but personally I'd try to avoid using global state to start with. It's a pain for testability and reasoning about how your program works.
Try your property page (file Global.cs) like these:
public class Global
{
private static string _globalVar;
public string GlobalVar
{
get { return _globalVar; }
set { _globalVar = value; }
}
}

Resources