xamarin camera - No overload for method 'Exists' takes '1' arguments - xamarin

I am trying to use the camera in Xamarin and have a method that gets a unique path as follows
private string GetUniquePath(string path, string name)
{
string ext = Path.GetExtension(name);
if (ext == string.Empty)
ext = ".jpg";
name = Path.GetFileNameWithoutExtension(name);
string newName = name + ext;
int i=1;
while (File.Exists(Path.Combine(path,newName)))
newName = name + "_" + (i++) + ext;
return Path.Combine(path, newName);
}
I get an error with -- while (File.Exists(Path.Combine(path,newName)))
The error is no overload for method 'Exists' takes 1 argument.
Yet thats the same format I see everywhere. any suggestions?

Error CS1501: No overload for method Exists' takes1' arguments
I would assume that it is picking up Java.IO.File.Exists which does not take any arguments.
Try fully qualifying the namespaces:
while (System.IO.File.Exists(System.IO.Path.Combine(path,newName)))
newName = name + "_" + (i++) + ext;

Related

Why does this ADO.NET query return no results?

I have the following code that executes a SQL statement and looks for a result.
var sql = #"select BOQ_IMPORT_ID "
+ "from ITIS_PRJ.PRJ_BOQ_IMPORT_HEADER "
+ "where PROJECT_ID = :Projectid "
+ "order by CREATED_ON desc "
+ "fetch first 1 row only";
using (var conn = new OracleConnection(ApplicationSettings.ConnectionString))
using (var cmd = new OracleCommand(sql, conn))
{
conn.Open();
cmd.Parameters.Add(LocalCreateParameterRaw("ProjectId", projectId));
var reader = cmd.ExecuteReader();
if (reader.Read())
{
byte[] buffer = new byte[16];
reader.GetBytes(0, 0, buffer, 0, 16);
var boqId = new Guid(buffer);
return boqId;
}
return null;
}
Where LocalCreateParameterRaw is declared as:
public static OracleParameter LocalCreateParameterRaw(string name, object value)
{
OracleParameter oracleParameter = new OracleParameter();
oracleParameter.ParameterName = name;
oracleParameter.OracleDbType = OracleDbType.Raw;
oracleParameter.Size = 16;
oracleParameter.Value = value;
return oracleParameter;
}
The underlying type for 'projectId' is 'Guid'.
The if (reader.Read()) always evaluates to false, despite there being exactly one row in the table. It normally should return only one row.
Using GI Oracle Profiler I can catch the SQL sent to the db, but only once did the profiler provide a value for the :ProjectId parameter, and it was in lower case. Like that it returned no results, but as soon as I applied UPPER to that value, I get a result.
It looks like I somehow have to get my parameter into uppercase for the query to work, but I have no idea how. Yet if I do a ToString().ToUpper() on the projectId GUID, I get a parameter binding error.
VERY IMPORTANT:
I have tried removing the where clause altogether, and no longer add a parameter, so all rows in the table should be returned, yet still no results.
I don't know how, but making the SQL string a verbatim string (prefixed with #) causes the proc to work. So, it doesn't work with:
var sql = #"SELECT BOQ_IMPORT_ID "
+ "FROM ITIS_PRJ.PRJ_BOQ_IMPORT_HEADER "
+ "WHERE PROJECT_ID = :projectid "
+ "ORDER BY CREATED_ON DESC "
+ "FETCH FIRST ROW ONLY";
Yet the same command string in SQL Developer executes and returns results. When I make my SQL string verbatim, as below, I get results.
var sql = #"select BOQ_IMPORT_ID
from ITIS_PRJ.PRJ_BOQ_IMPORT_HEADER
where PROJECT_ID = :ProjectId
order by CREATED_ON desc
fetch first 1 row only";
Using a more general approach, try the following
var sql = "SELECT BOQ_IMPORT_ID "
+ "FROM ITIS_PRJ.PRJ_BOQ_IMPORT_HEADER "
+ "WHERE PROJECT_ID = :projectid "
+ "ORDER BY CREATED_ON DESC "
+ "FETCH FIRST ROW ONLY";
using (DbConnection conn = new OracleConnection(ApplicationSettings.ConnectionString))
using (DbCommand cmd = conn.CreateCommand()) {
DbParameter parameter = cmd.CreateParameter();
parameter.ParameterName = "projectid";
parameter.Value = projectId.ToString("N").ToUpper(); //<-- NOTE FORMAT USED
cmd.Parameters.Add(parameter);
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
conn.Open();
var reader = cmd.ExecuteReader();
if (reader.Read()) {
var boqId = new Guid((byte[])reader[0]);
return boqId;
}
return null;
}
It looks like I somehow have to get my parameter into uppercase for the query to work, but I have no idea how. Yet if I do a ToString().ToUpper() on the projectId GUID, I get a parameter binding error.
Reference Guid.ToString Method
Specifier N formats it to 32 digits: 00000000000000000000000000000000
When no format is provided the default format is D which would include 32 digits separated by hyphens.
00000000-0000-0000-0000-000000000000
That would explain your binding error.

Why Kotlin doesn't implement Int.plus(value: String)?

It causes discomfort when you can do that:
val string = " abc "
val integer = 8
val result = string + integer
and can't do:
val result = integer + string
It has hidden meaning or it's an omission?
Kotlin is static typed language and in basicly you can't add String to Integer. But there are possible to overload operators, so we can now.
In case when we want add any object to string, it's clear: every object can be implicitly converted to String (Any#toString())
But in case of Int + smthg it's not so clear, so only Int + kotlin.Number is defined in standard library.
I suggest to use string interpolation:
val result = "${integer}${string}"
Or define own overloaded plus operator:
operator fun Int.plus(string: String): String = string + this

Groovy MissingMethodException when working with UTF-8

Currently I have the following String:
String str = "Hello my name\n\t\t\t\tis Earl."
The problem is that the remote process that handles this String doesn't like the character encoding of the newline and tab characters. This remote process expects UTF-8.
So I wrote convertSpecCharsToUtf8() method:
private String convertSpecCharsToUtf8() {
// "\n\t\t\t\t" as UTF-8
char[] utf8 = new char[6]
char[0] = '\\u000D'
char[1] = '\\u000A'
char[2] = char[3] = char[4] = char[5] = '\\u0009'
new String(utf8)
}
And then changed my str String to:
String str = "Hello my name" + convertSpecCharsToUtf8() + "is Earl."
When I run:
println "Testing UTF8"
String str = "Hello my name" + utf8CRLFTabFormat() + "is Earl."
println str
I get:
Testing UTF8
Caught: groovy.lang.MissingMethodException: No signature of method: static char.putAt() is applicable for argument types: (java.lang.Integer, java.lang.String) values: [0, \u000D]
groovy.lang.MissingMethodException: No signature of method: static char.putAt() is applicable for argument types: (java.lang.Integer, java.lang.String) values: [0, \u000D]
at com.me.myapp.convertSpecCharsToUtf8(Widget.groovy:133)
at com.me.myapp.execute(Widget.groovy:111)
at com.me.myapp$execute.call(Unknown Source)
at com.me.myapp.main(Widget.groovy:37)
Why, and what's the solution here?
There's a typo. Should be:
private String convertSpecCharsToUtf8() {
// "\n\t\t\t\t" as UTF-8
char[] utf8 = new char[6]
utf8[0] = '\\u000D'.toCharacter()
utf8[1] = '\\u000A'.toCharacter()
utf8[2] = utf8[3] = utf8[4] = utf8[5] = '\\u0009'.toCharacter()
new String(utf8)
}
You can write a list with each character and use the as operator to coerce to char[]. You may also use /str/ string declaration to avoid double escaping the backslash:
String convertSpecCharsToUtf8() {
new String( [/\u000D/, /\u000A/] + [/\u0009/] * 4 as char[] )
}
def str = "Hello my name" + convertSpecCharsToUtf8() + "is Earl."
assert str == """Hello my name
is Earl."""

Get file name from path [duplicate]

This question already has answers here:
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 2 years ago.
How can I split the path to get the file name 1_Lighthouse_20140306143834816.jpg? And split the 1_Lighthouse_20140306143834816.jpg to get the 1 as for my reference that number 1 is already exist.
Use Path.GetFileName
if(countUser.Length > 0)
{
var file = Path.GetFileName(countUser[0]);
....
and then get the first character from the resulting string using the string indexer
char firstChar = file[0];
if(firstChar == '1')
.....
}
Use Path.GetFileName or Path.GetFileNameWithoutExtension to get the file name.
And string.Split to get the first part of the file name.
string filePath = "E:\\folder\1_Lighthouse_XXX.jpg";
var s = Path.GetFileNameWithoutExtension(filePath); //returns without the .jpg
var parts = s.Split(new[] { '_' });
var indexer = Convert.ToInt32(parts[0]);
for (int i = 0; i < files.Count; i++)
{
//string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
//string filename = Path.GetFileName(Request.Files[i].FileName);
HttpPostedFileBase file = files[i];
//string fname;
// Checking for Internet Explorer
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
}

Selecting values from IQueryable with IsNullOrWhitespace check

I am trying to do the following with a IQueryable expression:
(from Person p in s
select new
{
label = p.FirstName + " "
+ (string.IsNullOrWhiteSpace(p.MiddleName) ? p.MiddleName + " " : "")
+ p.LastName,
value = p.Id
}).ToList();
I am getting following error:
LINQ to Entities does not recognize the method 'Boolean
IsNullOrWhiteSpace(System.String)' method, and this method cannot be
translated into a store expression.
What is the solution for this?
String.IsNullOrWhitespace is a static function of the string object and cannot be used with Entity Framework queries, whereas p.FirstName.StartsWith("S") is a method of the entity property and can be used.
To answer your question you will have to roll your own inline. Try this:
(from Person p in s
select new
{
label = p.FirstName + " "
+ ((p.MiddleName != null && p.MiddleName != string.Empty) ? p.MiddleName + " " : "")
+ p.LastName,
value = p.Id
}).ToList();

Resources