Give a start value to a get/set variable - get

Is it possible to give a start value to a variable that has a get and setter? So it should look something like this:
public static float myVariable = 10 {get; set;};
Thanks in advance.
Edit:
Working in C#
Edit 2:
So I tried this:
public static class GlobalVariables {
public static float groundSearchRayDistance{get;}
static GlobalVariables()
{
groundSearchRayDistance = 10;
}
}
But it doesn't work.

Try this:
public class ClassName{
public ClassName(){
myVariable = 10;
}
public static float myVariable {get; set;}
}

Dunno what language you are using, but usually you would put that within the constructor. You would just override the constructor of that variable to take a number and make that the initial value. Hope that helps! if not, give details!

Related

I'm doing an assignment for my class where I need to create the functions below for a Path. I'm having trouble on the functions, am I overthinking it?

import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;
public class Path
{
ArrayList<Point> pointOne;
ArrayList<Point> pointTwo;
public Path() {
pointOne = new ArrayList<Point>();
pointTwo = new ArrayList<Point>();
}
public Path(Scanner s)
{
pointOne = new ArrayList<Point>();
pointTwo = new ArrayList<Point>();
}
public int getPointCount()
{
return 0;
}
public int getX(int n)
{
return n;
}
public int getY(int n)
{
return n;
}
public void add(int x, int y)
{
}
public String toString()
{
}
So far this is what I have for the getX and getY function, please feel free to correct me if im not doing something right.
I've tried to research some different ways to add two points to an arraylist. I found one here but it didn't help as much as I thought it was going to. I am also confused on how to use the scanner to scan in the points and build a path. Am I just being dumb and overthinking this? I'm going to talk to my teacher to see if he can clear anything up but any help would be much appreciated thanks
public class Main {
public static void main {
ArrayList<Point> path = new ArrayList<Point>();
path.add(new Point(x1,y1));
path.add(new Point(x2,y2));
}
}
public class Point {
private double X;
private double Y;
public Point(double x, double y){
X = x;
Y = y;
}
public double getX(){return X;}
public double getY(){return Y;}
}
You see, a path is a list of points, conceptually, specifically an ArrayList of Points here. Point is a type of object with attributes x and y. You don't need to create an type called Path because a Path is just an ArrayList<Point>. You can't extend ArrayLists, probably you don't know yet about how inheritance anyways. But unless you are told specifically to do so, there's no need to make Path a class.
Furthermore, Point could be removed as a type and just made as an ArrayList<Double>. Conceptually, a point is just a list of its numeric coordinates in each direction.
Therefore, a Path could be represented as an ArrayList<ArrayList<Double>>.

Get and Set values of different variables in public static class from different threads C#

I tried to understand how it works but i am very slow with this(( so i decided ti ask here. In my prohramm i have static public class with different variables arrays, tabControls, Sizes, Pens an so on. But i need to set and get values of the variables from different threads how could i do it?
i have a class
public static class GLOBAL_STATIC_DATA
{
//.....
private static Size _get_Active_Project_ViewPort_Size()
{
//.....
}
public static Size Active_Project_ViewPort_Size
{
get
{
return _get_Active_Project_ViewPort_Size();
}
}
public static int Get_Panorama_Original_Image_Width()
{
}
public static TabControl MainTab = new TabContol();
public static int someInt = 100;
}
I need to write and to read all of that from different threads, could somebody help how shoud i change this static class to be able do that.
You just precede the public static entries with the class name...for example:
Size sz = GLOBAL_STATIC_DATA.Active_Project_ViewPort_Size;

Name generator using Generics

I am trying to generate a Name based on type of an object. In my system, I have,
class Employee {}
Class ContractEmp:Employee{}
class Manager:Employee{}
I am trying to generate name which looks like ContractEmp1 Where 1 will come from incrementer. I am trying to use Generics.
Any Help
Thank you,
With an extension method you could do something like this:
public static class NameExtension
{
private static Dictionary<string, int> counters = new Dictionary<string, int>();
public static string MakeUpName<T>(this T #object)
{
var t = typeof(T);
if ( ! counters.ContainsKey(t.FullName))
counters[t.FullName] = 0;
return t.Name + counters[t.FullName]++;
}
}
Test:
[TestFixture]
class NameTest
{
[Test]
public void test()
{
Console.WriteLine(new NameTest().MakeUpName());
Console.WriteLine(new NameTest().MakeUpName());
Console.WriteLine(new NameTest().MakeUpName());
Console.WriteLine(new NameTest().MakeUpName());
}
}
Output:
NameTest0
NameTest1
NameTest2
NameTest3
You can use a private static int in the Employee class which gets incremented on each constructor call. Combining this number with the typeof(this).Name value you can generate the names as described. Do note that the counter will count for all Employee extending classes so if you want an consecutive list of numbers for each Employee extending class, a specific counter should be implemented for every extending class. Also, the counters will be set to zero each time the application restarts.
public Class ContractEmp:Employee{
private static int counter = 1;
private String name = "";
public ContractEmp() {
name = typeof(this).Name + counter++;
}
}
Something like this should work!

Accessing static field in annotation

Im trying use a Java annotation in a Groovy class but have trouble to set a static field of a java class as a parameter:
The Annotation: Id.java
package x.y.annotations;
import java.lang.annotation.ElementType;
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.FIELD)
public #interface Id {
public Class<Adapter> adapter();
public Class<Object> targetType();
public String targetAttribute();
public String onDelete();
}
The java class with the static fields: XPerson.java
package x.y.static.domain;
public class XPerson {
public static String ID;
}
And the groovy class, where the problem occurs: Person.groovy
package x.y.domain
import x.y.annotations.Id
import x.y.static.domain.XPerson
class Person {
#Id(adapter = Adapter, targetType = XPerson, targetAttribute = XPerson.ID, onDelete = "delete")
long id
}
Eclipse marks the "targetAttribute = XPerson.ID" part with:
Groovy:expected 'x.y.domain.XPerson.ID' to be an inline constant of type java.lang.String not a property expression in #x.y.annotations.Id
I also tried things like "XPerson.#ID" or defining a getter for the ID field, but nothing helped.
Any hints would be great.
Regards,
michael
I have found a related issue in the Groovy JIRA. It is a bug. Should work. See https://issues.apache.org/jira/browse/GROOVY-3278
Annotation values may only be compile-time constant expressions. Making the field final is an option. (With the caveat that the field can't be initialized in a static initializer/etc. as the snippet implies.)

Can extension methods modify extended class values?

I was just trying to code the following extension method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4Testing
{
static class ExtensionMethods
{
public static void AssignMe(this int me, int value)
{
me = value;
}
}
}
But it is not working, i mean, can I use an extension method to alter values from extended classes? I don't want to change void return type to int, just changing extended class value. Thanks in advance
Your example uses int, which is a value type. Classes are reference types and behaves a bit differently in this case.
While you could make a method that takes another reference like AssignMe(this MyClass me, MyClass other), the method would work on a copy of the reference, so if you assign other to me it would only affect the local copy of the reference.
Also, keep in mind that extension methods are just static methods in disguise. I.e. they can only access public members of the extended types.
public sealed class Foo {
public int PublicValue;
private int PrivateValue;
}
public static class FooExtensions {
public static void Bar(this Foo f) {
f.PublicValue = 42;
// Doesn't compile as the extension method doesn't have access to Foo's internals
f.PrivateValue = 42;
}
}
// a work around for extension to a wrapping reference type is following ....
using System;
static class Program
{
static void Main(string[] args)
{
var me = new Integer { value = 5 };
int y = 2;
me.AssignMe(y);
Console.WriteLine(me); // prints 2
Console.ReadLine();
}
public static void AssignMe(this Integer me, int value)
{
me.value = value;
}
}
class Integer
{
public int value { get; set; }
public Integer()
{
value = 0;
}
public override string ToString()
{
return value.ToString();
}
}
Ramon what you really need is a ref modifier on the first (i.e. int me ) parameter of the extension method, but C# does not allow ref modifier on parameters having 'this' modifiers.
[Update]
No workaround should be possible for your particular case of an extension method for a value type. Here is the "reductio ad absurdum" that you are asking for if you are allowed to do what you want to do; consider the C# statement:
5.AssignMe(10);
... now what on earth do you think its suppose to do ? Are you trying to assign 10 to 5 ??
Operator overloading cannot help you either.
This is an old post but I ran into a similar problem trying to implement an extender for the String class.
My original code was this:
public static void Revert(this string s)
{
char[] xc = s.ToCharArray();
s = new string(xc.Reverse());
}
By using the new keyword I am creating a new object and since s is not passed by reference it will not be modified.
I changed it to the following which provides a solution to Ramon's problem:
public static string Reverse(this string s)
{
char[] xc = s.ToCharArray();
Array.Reverse(xc);
return new string(xc);
}
In which case the calling code will be:
s = s.Reverse();
To manipulate integers you can do something like:
public static int Increment(this int i)
{
return i++;
}
i = i.Increment();

Resources