ValueTuple: what is wrong with creation of octuple? - c#-7.0

ValueTuple as a new feature in C# 7.0 is having public method Create which helps to create ValueTuples (from singleton to octuple or more) on the other hand we can also use new to achieve the same results. I noticed these behave differently. I am trying to research is below implementation wrong or this is something as per design:
Method CreateOctuple() is working as expected:
private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> CreateOctuple()
{
return new ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>(1, 2, 3, 4, 5, 6, 7, new ValueTuple<int>(8)); ;
}
Now, I tried to achieve same output using Create() method, unfortunately, it is complaining about the return type:
private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, ValueTuple.Create(8));
}
What is wrong here?
P.S. All packages are up-to-date and I am using Visual Studio 2017 -latest release.
As suggested by Svick
static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
}
This prompts same compiler exception:

The issue is that ValueTuple.Create already takes care of calling ValueTuple.Create on the 8th element. So while the correct type for 8-tuple is ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>, you're creating ValueTuple<int, int, int, int, int, int, int, ValueTuple<ValueTuple<int>>>. The fix is just to remove the second call to ValueTuple.Create:
static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
}
Or you could just use the tuple syntax, but I'm assuming you have a reason to avoid it:
static (int, int, int, int, int, int, int, int) OctupleUsingCreate()
{
return (1, 2, 3, 4, 5, 6, 7, 8);
}

You need to update this package: Microsoft.Net.Compilers to 2.0 (you need to show pre-release). It would fix the error, check this here

Note: This is not a problem with the ValueTuple library or the C# compiler. No error on TryRoslyn with:
using System;
public class C
{
private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
}
}

Related

how to change the format of numbers in .csv data Kotlin

I receive the data from sensors and save it in an .csv file. but the formatt that I have is like 0.234.234.234.123.5434
What I Need is the Format like this 0,23452342234234234
private fun formatRowData(x: Float,
y: Float,
z: Float,
length: Double,
angle: Double): String {
return String.format(
"${SimpleDateFormat("H:m:s:SSS")
.format(today.time)};$x;${y};${z};${length};${angle}", Locale.GERMANY
)
}
Try this :
var value:String ="0.234.234.234.123.5434"
value = value.replaceFirst(".", ",")
value = value.replace(".","")
println(" : "+value)
Output :
: 0,2342342341235434

I cant insert my values into my SQLite table

So I have been working roughly all the Christmas with this, but it seems like I cant actually find a way, I just create this account because of it. So, lets go into the code.
In first stance, I wanna warning the variables and all that are in spanish, but I dont think it will be a problem.
I have been taking a lot of data, its... if I recall correctly 53 or 56 columns in my SQL table or something like. I have been recollecting data through all the app and getting it through each activity by "putExtra". This is for a project for my school, so I have to use SQLite (I would like to use firebase, being honest) so, whatever, lets go into the code, which is the important stuff.
#Override
public void onCreate(SQLiteDatabase BaseDeDatos) {
//Creamos instancia a nuestra clase de BD
BaseDeDatos.execSQL("create table datos_personaje (apellido text primary key, nombre text, edad int, genero text, raza text, clase text, " +
"fuerza int, destreza int, resistencia int, inteligencia int, percepcion int, voluntad int, carisma int, apariencia int, manipulacion int, " +
"pelea int, atletismo int, robar int, sigilo int, nadar int, montar int, abrirCerr int, resDolorF int, escalar int, evadir int, esquivar int, bloquear int, " +
"alerta int, supervivencia int, coherencia int, rastrear int, concentracion int, buscar int, esconderse int, resDolorM int, estrategia int, escuchar int, conGeneral int, tasar int, " +
"mentir int, empatia int, liderazgo int, intimidar int, callejeo int, comercio int, seducir int, protocolo int, convencer int, actuar int, timar int, disfraz int, " +
"tecnicasHechizos text, inventario text)");
}
This is the creation of the table, I would say its alright.
public void SQLiteAyuda() {
Bundle extras = getIntent().getExtras();
String nombre = extras.getString("nombre");
String apellidos = extras.getString("apellidos");
int edad = extras.getInt("edad");
String genero = extras.getString("genero");
String raza = extras.getString("raza");
String clase = extras.getString("clase");
int f = extras.getInt("principalesF");
int d = extras.getInt("principalesD");
int r = extras.getInt("principalesR");
int i = extras.getInt("principalesI");
int p = extras.getInt("principalesP");
int v = extras.getInt("principalesV");
int c = extras.getInt("principalesC");
int a = extras.getInt("principalesA");
int m = extras.getInt("principalesM");
int pelea = extras.getInt("pelea");
int atletismo = extras.getInt("atletismo");
int robar = extras.getInt("robar");
int sigilo = extras.getInt("sigilo");
int nadar = extras.getInt("nadar");
int montar = extras.getInt("montar");
int abrirCerr = extras.getInt("abrircerr");
int resDolorF = extras.getInt("resdolorf");
int escalar = extras.getInt("escalar");
int evadir = extras.getInt("evadir");
int bloquear = extras.getInt("bloquear");
int esquivar = extras.getInt("esquivar");
int alerta = extras.getInt("alerta");
int supervivencia = extras.getInt("supervivencia");
int coherencia = extras.getInt("coherencia");
int rastrear = extras.getInt("rastrear");
int concentracion = extras.getInt("concentracion");
int buscar = extras.getInt("buscar");
int esconderse = extras.getInt("esconderse");
int resDolorM = extras.getInt("resDolorM");
int estrategia = extras.getInt("estrategia");
int escuchar = extras.getInt("escuchar");
int conGeneral = extras.getInt("conGeneral");
int tasar = extras.getInt("tasar");
int mentir = extras.getInt("mentir");
int empatia = extras.getInt("empatia");
int liderazgo = extras.getInt("liderazgo");
int intimidar = extras.getInt("intimidar");
int callejeo = extras.getInt("callejeo");
int comercio = extras.getInt("comercio");
int seducir = extras.getInt("seducir");
int protocolo = extras.getInt("protocolo");
int convencer = extras.getInt("convencer");
int actuar = extras.getInt("actuar");
int timar = extras.getInt("conGeneral");
int disfraz = extras.getInt("disfraz");
String poderes = extras.getString("tecnicasHechizos");
String inventario = inventarioText.getText().toString();
I get all my data in the last activity, so I can get it into the SQL table:
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "datos_personaje", null, 2);
SQLiteDatabase BaseDeDatos = admin.getWritableDatabase();
ContentValues datos = new ContentValues();
datos.put("nombre", nombre);
datos.put("apellido", apellidos);
datos.put("edad", edad);
datos.put("genero", genero);
datos.put("raza", r);
datos.put("clase", c);
datos.put("fuerza", f);
datos.put("destreza", d);
datos.put("resistencia", r);
datos.put("inteligencia", i);
datos.put("percepcion", p);
datos.put("voluntad", v);
datos.put("carisma", c);
datos.put("apariencia", a);
datos.put("manipulacion", m);
datos.put("pelea", pelea);
datos.put("atletismo", atletismo);
datos.put("robar", robar);
datos.put("sigilo", sigilo);
datos.put("nadar", nadar);
datos.put("montar", montar);
datos.put("abrircerr", abrirCerr);
datos.put("resdolorf", resDolorF);
datos.put("escalar", escalar);
datos.put("evadir", evadir);
datos.put("esquivar", esquivar);
datos.put("bloquear", bloquear);
datos.put("alerta", alerta);
datos.put("supervivencia", supervivencia);
datos.put("coherencia", coherencia);
datos.put("rastrear", rastrear);
datos.put("concentracion", concentracion);
datos.put("buscar", buscar);
datos.put("esconderse", esconderse);
datos.put("resDolorM", resDolorM);
datos.put("estrategia", estrategia);
datos.put("escuchar", escuchar);
datos.put("conGeneral", conGeneral);
datos.put("tasar", tasar);
datos.put("mentir", mentir);
datos.put("empatia", empatia);
datos.put("liderazgo", liderazgo);
datos.put("intimidar", intimidar);
datos.put("callejeo", callejeo);
datos.put("comercio", comercio);
datos.put("seducir", seducir);
datos.put("protocolo", protocolo);
datos.put("convencer", convencer);
datos.put("actuar", actuar);
datos.put("timar", timar);
datos.put("disfraz", disfraz);
datos.put("tecnicasHechizos", poderes);
datos.put("inventario", inventario);
BaseDeDatos.insert("datos_personaje", null, datos);
BaseDeDatos.close();
And the debugger shows me the next output:
E/SQLiteLog: (1)
E/SQLiteDatabase: Error inserting table datos_personaje has no column named coherencia (Sqlite code 1): , while compiling: INSERT INTO datos_personaje(esconderse,sigilo,protocolo,resdolorf,carisma,genero,destreza,inventario,escalar,alerta,liderazgo,raza,rastrear,escuchar,inteligencia,pelea,edad,disfraz,abrircerr,buscar,manipulacion,comercio,evadir,nadar,actuar,resDolorM,bloquear,conGeneral,resistencia,coherencia,mentir,apariencia,estrategia,tasar,montar,esquivar,robar,nombre,fuerza,clase,supervivencia,atletismo,callejeo,apellido,intimidar,timar,tecnicasHechizos,concentracion,voluntad,percepcion,seducir,convencer,empatia) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?), (OS error - 2:No such file or directory)

Convert java.util.GregorianCalender to java.lang.Integer?

Im using the followin script/function (named getQuarter) to get a last day of current quarter:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)/3 * 3 + 2);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return cal.getTime();
And Im calling this function in my trigger which supposed to set this date every time when certain events occurs:
setAttribute('EffectiveDate', adf.util.getQuarter(today()))
But then the following error occurs when the trigger should start working:
Exception in expression "util" global function getQuarter2(java.util.Date) : groovy.lang.MissingMethodException : No signature of method: java.util.GregorianCalendar.set() is applicable for argument types: (java.lang.Integer, java.math.BigDecimal) values: [2, 5] Possible solutions: set(int, int), get(int), set(int, int, int), set(java.util.Map), next(), isSet(int)
I figured out it needs the type GregorianCalendar converted to Integer, but how Im supposed to do this in my script? Any ideas?
Alex
You can cast the values to int:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.MONTH as int, (cal.get(Calendar.MONTH)/3 * 3 + 2) as int);
cal.set(Calendar.DAY_OF_MONTH as int, (cal.getActualMaximum(Calendar.DAY_OF_MONTH)) as int);
If you're on Java 8, can I suggest you us the new java.time classes?
Given todays date:
def date = LocalDate.now()
You can calculate the last day of the quarter by:
int quarter = YearMonth.from(date).get(IsoFields.QUARTER_OF_YEAR)
LocalDate lastDayOfQuater = date.withMonth(quarter * 3).with(TemporalAdjusters.lastDayOfMonth())

Groovy default method parameters

How to call the below method def to pass only interactionId, transactionId, awResponseFile, testMethodName and expectedNumberOfConsumers?
method def
void verifyPersistence(String interactionId, String transactionId,
File rawResponseFile, String testMethodName,
int expectedNumberOfDatasourceProductResponseRaws=1,
String dsProductName=datasourceSpec['creditreport.name'],
int expectedNumberOfDatasourceProductResponses=1,
int expectedNumberOfConsumers=1,
int expectedNumberOfAddresses=1 )
method call
verifyPersistence interactionId.persistedVersion(), transactionId,
results, testName.methodName, expectedNumberOfConsumers:8
Exception
groovy.lang.MissingMethodException: No signature of method: verifyPersistence() is applicable for argument types: (java.util.LinkedHashMap, java.lang.String, java.lang.String, java.io.File, java.lang.String) values: [[expectedNumberOfConsumers:8], 3130001030065, 10003000000890059, C:\results, multiple consumers contracts]
Possible solutions:
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String),
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int),
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int, java.lang.String),
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int, java.lang.String, int), verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int, java.lang.String, int, int)
at HitmultipleconsumersPersistSpec.groovy:151)
When you include a map entry (key:value), they get automatically collected, put into a hashmap and passed as the first parameter. Groovy, right now, doesn't have named parameters. For that specific case, i think you are better using a map for that method:
def verifyPersistence(Map map) {
String interactionId = map.interactionId
String transactionId = map.transactionId
File rawResponseFile = map.rawResponseFile
String testMethodName = map.testMethodName
int expectedNumberOfDatasourceProductResponseRaws = map.expectedNumberOfDatasourceProductResponseRaws ?: 1
String dsProductName = map.dsProductName ?: datasourceSpec['creditreport.name']
int expectedNumberOfDatasourceProductResponses = map.expectedNumberOfDatasourceProductResponses ?: 1
int expectedNumberOfConsumers = map.expectedNumberOfConsumers ?: 1
int expectedNumberOfAddresses = map.expectedNumberOfAddresses ?: 1
}
verifyPersistence(
interactionId : interactionId.persistedVersion(),
transactionId : transactionId,
rawResponseFile : results,
expectedNumberOfDatasourceProductResponseRaws : 14,
expectedNumberOfConsumers:8
)
You obviously don't need to assign every parameter to a variable, you can simply use the map.field syntax. If you want it to be type checked, then i think you are better using an object to encapsulate the parameters.

Retrieve integer value of enum

Given:
public enum myType
{
Val1 = 1,
Val2 = 2,
Val3 = 3
}
and code elsewhere in the app where a value :
...
row.myType // resolves to Val1
...
I need to translate row.myType to 1
Simply cast to an int:
int enumValue = (int)row.MyTime;
cast it to int
(int)row.myTime
You can simply cast it to an integer:
(int)row.myType;
myType someEnumVal = myType.Val1;
int intValOfEnum = (int)someEnumVal;

Resources