Thread: Problem with conversion of values

1. Problem with conversion of values

I have a routine that reads an int value from database:
Code:
```        public int RunSqlReturnInt(SQLiteConnection conn, string sql, int AltValForNull = 0, int AltValForNoRec = 0)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return AltValForNoRec;
else
{
var retVal = rdr[0];

if ((retVal == null) || (retVal == DBNull.Value))
return AltValForNull;

return Convert.ToInt32(retVal);
}
}
}```
And it works fine.

Now, I am trying to write similar routines for: long, string, float, double and decimal

As for "long", it is quite obvious that the conversion must be like this:
Code:
`                    return Convert.ToInt64(retVal);`
And for string, it is again obvious that it must be like this:
Code:
`                    return Convert.ToString(retVal);`
But, for the other three it is not obvious because the type names in the convert method and the type names in C# are different.
For example the type name int in C# is the same as Int32 in the conversion method.
And type name long in C# is the same as Int64 in the conversion method.

That is why I don't want to take any chances.

I THINK (but I am not sure) that for float, it should be like this:
Code:
`                    return Convert.ToSingle(retVal);`
And for double:
Code:
`                    return Convert.ToDouble(retVal);`
And for decimal it should be:
Code:
`                    return Convert.ToDecimal(retVal);`
Are the above conversions correct?
Do they have the same range of values as the C# data types that I am linking them to?
For example does Convert.ToSingle, retun a value that is in the range of ±1.5 x 10^−45 to ±3.4 x 10^38 ?
And the same question for double and decimal data types.

Thanks.

2. Re: Problem with conversion of values

You could also try the Type.Parse method ... -- actually no... now that I'm thinking about it, that's more appropriate for string to Type conversions ...

But I think you've got the right conversions there.

-tg

3. Re: Problem with conversion of values

Why do you need to convert anything? If you're getting an Int32 from the database then you already have an Int32, so why would you need to convert it to an Int32? Etc. If the data is already that type then you should be casting as that type or having the data reader return that type in the first place.

4. Re: Problem with conversion of values

Originally Posted by jmcilhinney
Why do you need to convert anything? If you're getting an Int32 from the database then you already have an Int32, so why would you need to convert it to an Int32? Etc. If the data is already that type then you should be casting as that type or having the data reader return that type in the first place.
Hi.
Thanks a lot for your response.
I think maybe I didn't clarify my question properly. So, I will explain in here.

I am writing a routine like this:
Code:
```        public float RunSqlReturnFloat(SQLiteConnection conn, string sql, float AltValForNull = 0f, float AltValForNoRec = 0f)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return AltValForNoRec;
else
{
var retVal = rdr[0];

if ((retVal == null) || (retVal == DBNull.Value))
return AltValForNull;

return Convert.ToSingle(retVal);    //   Here is the question. Is this correct or is this wrong?
}
}
}```
It is supposed to read ONE value from the database and that value when returned to C# is to be of type float.
As you see in the above (the first line), that routine returns a float (the routine's output is of type float).

Now, having said that, I need to know whether this line (the last line of code in the above snippet):
Code:
`                    return Convert.ToSingle(retVal);    //   Here is the question. Is this correct or is this wrong?`
is correct or wrong.

Thanks again

5. Re: Problem with conversion of values

You could probably just do

Code:
`return (float) retVal;`

6. Re: Problem with conversion of values

Yes, a C# float is equivalent to Single.

That information is readily available via your favorite search engine, faster than it would have taken you to even start creating this thread, much less waiting two days and posting a second time still in the dark.

7. Re: Problem with conversion of values

Originally Posted by IliaPreston
Hi.
Now, having said that, I need to know whether this line (the last line of code in the above snippet):
Code:
`                    return Convert.ToSingle(retVal);    //   Here is the question. Is this correct or is this wrong?`
is correct or wrong.
As I said, why would you need to convert a value to a Single when that's what it already is? Convert.ToSingle should be used when you have a value that is not a Single, e.g. a String or a Double, and you want to actually convert it. If the value is already a Single then there's no conversion to be done. If that Single is accessed via an Object reference then, as with any object accessed that way, you simply need to cast as the type it is. That's what PD has demonstrated in post #5. Alternatively, the data reader has a GetSingle method that will return the value as a Single instead of as an Object reference boxing a Single value, so the cast would be unnecessary. That's perhaps less great in this case because you are already getting the value to test for null, so you'd be getting the value twice.

8. Re: Problem with conversion of values

Originally Posted by jmcilhinney
As I said, why would you need to convert a value to a Single when that's what it already is? Convert.ToSingle should be used when you have a value that is not a Single, e.g. a String or a Double, and you want to actually convert it. If the value is already a Single then there's no conversion to be done. If that Single is accessed via an Object reference then, as with any object accessed that way, you simply need to cast as the type it is. That's what PD has demonstrated in post #5. Alternatively, the data reader has a GetSingle method that will return the value as a Single instead of as an Object reference boxing a Single value, so the cast would be unnecessary. That's perhaps less great in this case because you are already getting the value to test for null, so you'd be getting the value twice.
Thanks a lot for your help.
The reason I wrote it that way is that I learned from your code in post#6 in https://www.vbforums.com/showthread....-single-value:
Originally Posted by jmcilhinney
......
If you really want to do it that way, the code should look like this:
Code:
```public int New3_RunSqlReturnInt(SQLiteConnection conn, string sql, int altValForNull = 0, int altValForNoRec = 0)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return altValForNoRec;

var retVal = rdr[0];

if (retVal == null || retVal == DBNull.Value)
return AltValForNull;

return Convert.ToInt32(retVal);
}
}```
Note that I have fixed all the inconsistencies and removed all unnecessary code.
......
In there, why did you write it like this:
Code:
`        return Convert.ToInt32(retVal);`
Why not like this (as you suggest in post#7 in this thread):
Code:
`        return (int)retVal;`
Or even like this (as you suggest in post#7 in this thread):
Code:
`        return retVal;`
I am just trying to understand the reason.
That is why I am asking.

Thanks again.

9. Re: Problem with conversion of values

In all but one of my code snippets in that thread, I used the GetInt32 method of the data reader. In the last snippet in post #6, I simply started with your snippet from post #5 and streamlined the logic. Don't blame me that you used Convert.ToInt32 in the first place. prior to that, I said this:
Code:
`return Convert.ToInt32(RetVal);`
What's the point of converting an int to an int?
I already addressed that point. I just didn't change it in that particular code snippet because that was not the focus of what I was correcting. It's a pretty simple concept: you don't need to convert something to a type it already is. If a value is already a particular type but is referred to by a reference of a base type, e.g. an Object variable referring to an Int32 value, then that is exactly what casting is for. Converting is for changing the type of the object. Casting is for changing the type of the reference referring to the same object.

10. Re: Problem with conversion of values

Originally Posted by jmcilhinney
In all but one of my code snippets in that thread, I used the GetInt32 method of the data reader. In the last snippet in post #6, I simply started with your snippet from post #5 and streamlined the logic. Don't blame me that you used Convert.ToInt32 in the first place. prior to that, I said this:

I already addressed that point. I just didn't change it in that particular code snippet because that was not the focus of what I was correcting. It's a pretty simple concept: you don't need to convert something to a type it already is. If a value is already a particular type but is referred to by a reference of a base type, e.g. an Object variable referring to an Int32 value, then that is exactly what casting is for. Converting is for changing the type of the object. Casting is for changing the type of the reference referring to the same object.
Thanks a lot for your help.
It makes a lot of sense and is very informative.

Don't blame me that you used Convert.ToInt32 in the first place.
I am sorry if my question was viewed as blaming.
I ABSOLUTELY don't blame anyone.
The only reason why I asked it, was that I was confused about it.
It is a good thing that you have cleared that confusion.

Thanks again
Ilia

11. Re: Problem with conversion of values

No problem. All's well that ends well.

12. Re: Problem with conversion of values

Looks like there is still a problem in here.

Code:
`                      return Convert.ToSingle(retVal);`
works, but, there were a lot of objections raised against it in this thread, and those objections are very valid.

So, I tried to change my code according to the advice given to me in here.
The first attempt at improving my code is as follows (no conversion and no cast):
Code:
`                      return retVal;`
It results in a compile-time error:
https://i.imgur.com/LKHGt4F.jpeg

The second attempt at improving my code is as follows (explicit type cast to float):
Code:
`                      return (float)retVal;`
It results in a run-time error:
https://i.imgur.com/gQoxMSN.jpeg

What is it that I am missing?
And what is the best way of doing it?

Once again, I am ABSOLUTELY NOT blaming anyone or anything.
I am just having a strange observation and I believe it is interesting enough to share it with everybody in here.

Thanks

13. Re: Problem with conversion of values

In your run-time error screenshot, the Locals window at the bottom appears to indicate that retVal is an object containing a double. So try changing the cast from float to double and see what happens. Of course, you would also need to change the return type of the function itself to double, and any code calling that function would need to be properly handling a returned double as well.

Edit: Or you could just change back to the "bad", but working code that uses Convert.ToSingle.

14. Re: Problem with conversion of values

We said that that code was bad because, based on the information you provided, the data was already type Single and there's no point converting a type to itself. Now you're telling us that it was in fact type Double to begin with and not Single at all. The question now is, why are you trying to return data that is type Double as type Single? Is there a valid reason for reducing the precision or not? If there is then Convert.ToSingle is actually the right option. If not then you should be changing either the database to store the data as single precision in the first place or changing your application to use double precision. The most appropriate solution depends on the actual details of the situation, which we have not been provided.

15. Re: Problem with conversion of values

Originally Posted by jmcilhinney
We said that that code was bad because, based on the information you provided, the data was already type Single and there's no point converting a type to itself. Now you're telling us that it was in fact type Double to begin with and not Single at all. The question now is, why are you trying to return data that is type Double as type Single? Is there a valid reason for reducing the precision or not? If there is then Convert.ToSingle is actually the right option. If not then you should be changing either the database to store the data as single precision in the first place or changing your application to use double precision. The most appropriate solution depends on the actual details of the situation, which we have not been provided.

First of all the query is:
Code:
`    sql = "select STD_NUM * 15.37 from STUDENTS  where STD_NUM = 1001"`
So, obviously, the output is expected to be 15385.37 but Visual Studio calculates it to be 15385.369999999999

(If that query doesn't make much sense, it is because it is a test program, and I am experimenting with everything to learn C#.
So, in that perspective, it makes sense.)

In this and similar situations, the output should be stored in a C# float variable (and not double).
So, basically based on your latest comments, I believe that I should use:
Code:
`                      return Convert.ToSingle(retVal);`
I also have another routine very similar to this one but one that returns double. That could be used for returning larger real numbers
So, basically for the current routine that is the subject of this discussion (that returns a float) I guess and I am almost sure that the correct code to output its value is this:
Code:
`                      return Convert.ToSingle(retVal);`
Thanks a lot for your help.

16. Re: Problem with conversion of values

I looked back through your posts and you never actually said that the value coming from the database was a Single, but you never actually said that it was a Double either. This is an example of why you need to provide all the relevant information. If you don't, we need to guess or assume and, if we do that, we may get it wrong, as I (we?) did in this case. As it stands, if you want to convert a value that isn't a Single to a Single, Convert.ToSingle is the correct choice.

That said, are you converting to Single because you actually need a Single or are you just trying to round the number so you get the expected number of decimal places? If the latter, that's probably not the best way to go about it.

17. Re: Problem with conversion of values

Originally Posted by jmcilhinney
That said, are you converting to Single because you actually need a Single or are you just trying to round the number so you get the expected number of decimal places?
I am converting to single because I need a single.

18. Re: Problem with conversion of values

So this all boils down to lack of communication. You had it right in the first place but didn't provide us with all the relevant information, so we made incorrect assumptions.

Having said all that, there may still be a bit of an issue. You say that you need a Single. That's fine. The data is stored in the database as a Double. Is that appropriate? If the data is always only used as a Single then I would suggest that that's how it should be stored. If you do use it as a Double in some cases and as a Single in others then I'd suggest that you should be retrieving it from the database as the Double it is, then converting it yourself in the places that you need a different type. Conceptually, it's not really the job of your data access code to change the type of the data it's accessing because that change is part of your business logic. If you're mixing business logic with data access code already though, it won't make it any worse to do it a bit more.

19. Re: Problem with conversion of values

Originally Posted by jmcilhinney
So this all boils down to lack of communication. You had it right in the first place but didn't provide us with all the relevant information, so we made incorrect assumptions.

Having said all that, there may still be a bit of an issue. You say that you need a Single. That's fine. The data is stored in the database as a Double. Is that appropriate? If the data is always only used as a Single then I would suggest that that's how it should be stored. If you do use it as a Double in some cases and as a Single in others then I'd suggest that you should be retrieving it from the database as the Double it is, then converting it yourself in the places that you need a different type. Conceptually, it's not really the job of your data access code to change the type of the data it's accessing because that change is part of your business logic. If you're mixing business logic with data access code already though, it won't make it any worse to do it a bit more.
The data is stored in the database as a Double.
You may be right on that, but I am not sure.
An sqLite column is stored as sqLite REAL datatype which is the equivalent of C# double datatype.
So, in that regard, I am sure you are right.

However, I am not sure about other cases.
For example the query that I provided in post #15 reads a value that is not a column. Rather a calculation:
Code:
`    sql = "select STD_NUM * 15.37 from STUDENTS  where STD_NUM = 1001"`
Is it the c# equivalent of double?
Not sure?
Kindly, let me know.

You say that you need a Single. That's fine. The data is stored in the database as a Double. Is that appropriate?
The short answer is that (Please correct me if I am wrong) I guess it is appropriate, unless you can provide good reasons to the contrary, because sometimes the value is sure to be small.

The long answer is that I am learning c# and therefore I am writing a number of little routines that I will need in a whole lot of programs later.
As a good example that we already discussed in another thread, I have written this with your kind help:
Code:
```        public int RunSqlReturnInt(SQLiteConnection conn, string sql, int AltValForNull = 0, int AltValForNoRec = 0)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return AltValForNoRec;
else
{
var retVal = rdr[0];

if ((retVal == null) || (retVal == DBNull.Value))
return AltValForNull;

return Convert.ToInt32(retVal);
}
}
}```
If you think about it as a general routine that will be needed and will be used in a whole lot of programs, it makes sense.

By the same logic, and for use in all future programs, I have this:
Code:
```        public long RunSqlReturnLong(SQLiteConnection conn, string sql, long AltValForNull = 0, long AltValForNoRec = 0)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return AltValForNoRec;
else
{
var retVal = rdr[0];

if ((retVal == null) || (retVal == DBNull.Value))
return AltValForNull;

return Convert.ToInt64(retVal);
}
}
}```
And the above general routines provide the answer to your question as to why I need to return a float.
Because I have these two routines too:
Code:
```        public float RunSqlReturnFloat(SQLiteConnection conn, string sql, float AltValForNull = 0f, float AltValForNoRec = 0f)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return AltValForNoRec;
else
{
var retVal = rdr[0];

if ((retVal == null) || (retVal == DBNull.Value))
return AltValForNull;

return Convert.ToSingle(retVal);
}
}
}```
And:
Code:
```        public double RunSqlReturnDouble(SQLiteConnection conn, string sql, double AltValForNull = 0f, double AltValForNoRec = 0f)
{
using (var cmd = new SQLiteCommand(sql, conn))
{
if (!rdr.HasRows)
return AltValForNoRec;
else
{
var retVal = rdr[0];

if ((retVal == null) || (retVal == DBNull.Value))
return AltValForNull;

return Convert.ToDouble(retVal);
}
}
}```
I need to have both of these two routines RunSqlReturnFloat and RunSqlReturnDouble available to ne in all programs that I will develop, so that I could use whichever one of them appropriate in any given situation.

As for float, I guess there are SOME situations where the value being returned is 100% guaranteed to be small enough to suit a c# float variable.
So, why not use RunSqlReturnFloat in those cases?
In all other cases, I can use RunSqlReturnDouble.

Do you agree that the above four routines (especially the last two) are good and useful?

20. Re: Problem with conversion of values

I don't know how SQLite handles things but, generally speaking, if you use a number with decimal places in a calculation, that number will be treated as a double-precision floating point number and the result will be that type too. It wouldn't be hard to work out though. Just get the data as an Object reference and use the debugger to look at the actual type. We already know that it's Double though, because the error message told you that when you tried to cast as Single.

What you do is up to you but, again, if you're writing data access code then it should just handle data access. It should be used to get the data as it is. Converting to some other type would then be the job of your business or presentation logic, depending on the circumstances. Other than that though, those methods are useful in principle. The lack of parameter support makes them less so in practice though.

21. Re: Problem with conversion of values

Originally Posted by jmcilhinney
I don't know how SQLite handles things but, generally speaking, if you use a number with decimal places in a calculation, that number will be treated as a double-precision floating point number and the result will be that type too. It wouldn't be hard to work out though. Just get the data as an Object reference and use the debugger to look at the actual type. We already know that it's Double though, because the error message told you that when you tried to cast as Single.

What you do is up to you but, again, if you're writing data access code then it should just handle data access. It should be used to get the data as it is. Converting to some other type would then be the job of your business or presentation logic, depending on the circumstances. Other than that though, those methods are useful in principle. The lack of parameter support makes them less so in practice though.
Thanks a lot for your help.

The lack of parameter support makes them less so in practice though.
I am not quite sure what that means.
Those routines have everything that they need passed to them as input parameters.
So, what parameter do you think is missing in there?

Thanks again.

22. Re: Problem with conversion of values

Originally Posted by IliaPreston
So, what parameter do you think is missing in there?
SQL parameters. If, for instance, you have a WHERE clause, you're going to have to use string concatenation to get the filter values into the SQL. That is a cardinal sin for multiple reasons.

I'd also suggest that the way you're handling your connection object(s) looks like it could be rather dodgy too.

23. Re: Problem with conversion of values

Originally Posted by jmcilhinney
SQL parameters. If, for instance, you have a WHERE clause, you're going to have to use string concatenation to get the filter values into the SQL. That is a cardinal sin for multiple reasons.

I'd also suggest that the way you're handling your connection object(s) looks like it could be rather dodgy too.
Thanks a lot for your help.
You are basically raising two points, both of which are valid and interesting.

Let me first discuss the second point, that is the issue with connections.
You told me before that connections must be local (as opposed to global or form-level or the like) amd must be disposed of immediately after they are used.
I totally understand and i agree with that.

However, trying to use a local connection, I get an error.
Here is my code:
Code:
```        private void btnTestIntLocalConn_Click(object sender, EventArgs e)
{
string DispStr;
string sql;
int Res;

string DBFilePath = @"D:\Dev\VCS\Tests\Test001SqLite\zOther\Data\Test001.db";

// Creating the database (if it doesn't exist)
if (!(File.Exists(DBFilePath)))
{
SQLiteConnection.CreateFile(DBFilePath);
}

// Opening the database
using SQLiteConnection conn = new SQLiteConnection("Data Source=" + DBFilePath);
conn.Open();

txtDetails.Text = "";
DispStr = "";
DispStr = DispStr + "<<<int>>> Alt values of -1 and -2 :: " + "\r\n\r\n";

sql = "select count(*) from STUDENTS  where STD_NUM = 1001";
Res = RunSqlReturnInt(conn, sql, -1, -2);
DispStr = DispStr + "Count of one record: " + Res + "\r\n";

sql = "select count(*) from STUDENTS  where STD_NUM > 1002";
Res = RunSqlReturnInt(conn, sql, -1, -2);
DispStr = DispStr + "Count of more than one record: " + Res + "\r\n";

sql = "select STD_NUM from STUDENTS  where STD_NUM = 1001";
Res = RunSqlReturnInt(conn, sql, -1, -2);
DispStr = DispStr + "An integer column value: " + Res + "\r\n";

sql = "select -STD_NUM from STUDENTS  where STD_NUM = 1001";
Res = RunSqlReturnInt(conn, sql, -1, -2);
DispStr = DispStr + "A negative integer column value: " + Res + "\r\n";

sql = "select STD_NUM + null from STUDENTS  where STD_NUM = 1001";
Res = RunSqlReturnInt(conn, sql, -1, -2);
DispStr = DispStr + "An (integer) null value: " + Res + "\r\n";

sql = "select STD_NUM from STUDENTS  where STD_NUM = 5001";
Res = RunSqlReturnInt(conn, sql, -1, -2);
DispStr = DispStr + "No record found: " + Res + "\r\n";

txtDetails.Text = DispStr;
}```
It gives me a compile-time error:
Severity Code Description Project File Line Suppression State
Error CS8370 Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater. Test001SqLite D:\Dev\VCS\Tests\Test001SqLite\Test001SqLite\Form1.cs 1021 Active
Here is a screen print:
https://i.imgur.com/iuPLRJX.jpeg

I google searched this error and found a number of webpages that explain it. For example this one:
https://www.thecodebuzz.com/feature-...rsion-greater/

It says:

Please explicitly instruct the compiler that you would like to use the specific version of the language.
https://www.thecodebuzz.com/wp-conte...r.-768x190.jpg
But, I cannot find the project file in the Visual Studio IDE for example in the Solution Explorer:
https://i.imgur.com/ncSyv57.jpeg

Nothing useful in the "Properties" either.

Now, I try to find the csproj file in the folder where my Solution is located in Windows Explorer.
Not sure if it is right to do so or not. but I find it here:
D:\Dev\VCS\Tests\Test001SqLite\Test001SqLite\Test001SqLite.csproj
When I open this file in Notepad++, there is no such node as <LangVersion>.

Here is the whole project file:
https://www.mediafire.com/file/ly0s7...te.csproj/file

I don't know what to do.
Thanks again

24. Re: Problem with conversion of values

It's not that connections have to be local but they should be if they can be. ADO.NET connections are made to be cheap to create and discarded once they're used. You should only keep one of you specifically need to reuse that connection, e.g. you use the same connection when calling Fill and Update on a data adapter.

25. Re: Problem with conversion of values

You need to use a using statement rather than a using declaration, i.e. like this:
Code:
```using (var obj = new SomeType())
{
// Use obj here.
}```
rather than like this:
Code:
```using var obj = new SomeType();

// Use obj here.```
As the error message states, the second syntax was introduced in C# 8.0, so cannot be used in projects using an earlier language version. In both cases, the object is disposed when the variable falls out of scope. The first version may allow you to make that happen sooner but the second version makes the code a bit more readable by reducing indentation. The second version is preferable unless it's really important that the object be disposed as soon as possible.

26. Re: Problem with conversion of values

Originally Posted by jmcilhinney
You need to use a using statement rather than a using declaration, i.e. like this:
Code:
```using (var obj = new SomeType())
{
// Use obj here.
}```
rather than like this:
Code:
```using var obj = new SomeType();

// Use obj here.```
As the error message states, the second syntax was introduced in C# 8.0, so cannot be used in projects using an earlier language version. In both cases, the object is disposed when the variable falls out of scope. The first version may allow you to make that happen sooner but the second version makes the code a bit more readable by reducing indentation. The second version is preferable unless it's really important that the object be disposed as soon as possible.
Thanks a lot for your help.

I can certainly do it like this:
Code:
```using (var obj = new SomeType())
{
// Use obj here.
}```
but, I also wish I could do it by means of the declaration, like this:
Code:
```            using SQLiteConnection conn = new SQLiteConnection("Data Source=" + DBFilePath);
conn.Open();```
The reason is that I am learning C# and therefore, I should learn the "using declaration" as well.
I don't think it is a good idea to skip learning it.

That is the reason why I would like to learn how to fix that error.

As I said, (please correct me if I am wrong): the project file is not accessible through the IDE.
And when I found the file in Windows Explorer:
https://www.mediafire.com/file/ly0s7...te.csproj/file
it does not contain any node like "LangVersion" or "LangVer" or anything like that.

So, basically the question boils down to this:
How can I change the version "7.3" to "8.0" so that the "using declaration" works ?

Thanks again.

27. Re: Problem with conversion of values

Originally Posted by IliaPreston
The reason is that I am learning C# and therefore, I should learn the "using declaration" as well.
I don't think it is a good idea to skip learning it.
Changing it to a using statement is fixing it. No one is suggesting that you "skip" that feature but, even if you change the language version to 8.0, you're still going to be missing out on features from later versions, so why do you think it's OK to "skip" those but not this?
Originally Posted by IliaPreston
As I said, (please correct me if I am wrong): the project file is not accessible through the IDE.
.NET Core projects allow you to open the project file while they are loaded. .NET Framework projects do not. Given that .NET Core 3 and later use at least C# 8.0, you are presumably targeting .NET Framework. If you simply start targeting .NET 6 or later then you'll automatically get at least C# 10.0.

Otherwise, select the solution node in Solution Explorer and click the Show All Files button. Next, right-click the project node and unload the project. You should then be able to simply click the project node to see the project file. If not, right-click that node and select the Edit Project File option.
Originally Posted by IliaPreston
And when I found the file in Windows Explorer:
https://www.mediafire.com/file/ly0s7...te.csproj/file
it does not contain any node like "LangVersion" or "LangVer" or anything like that.
So add one. Do that, save the project file and then reload the project. Your using declaration should then compile successfully. You may have to choose Rebuild rather than Build.

28. Re: Problem with conversion of values

So add one. Do that, save the project file and then reload the project. Your using declaration should then compile successfully. You may have to choose Rebuild rather than Build.
Yessssssssssss !!!!!!!!!!
It works now.
I am so happy.

Thanks a lot for your help.

30. Re: Problem with conversion of values

Originally Posted by jmcilhinney
Changing it to a using statement is fixing it. No one is suggesting that you "skip" that feature but, even if you change the language version to 8.0, you're still going to be missing out on features from later versions, so why do you think it's OK to "skip" those but not this?

.NET Core projects allow you to open the project file while they are loaded. .NET Framework projects do not. Given that .NET Core 3 and later use at least C# 8.0, you are presumably targeting .NET Framework. If you simply start targeting .NET 6 or later then you'll automatically get at least C# 10.0.

Otherwise, select the solution node in Solution Explorer and click the Show All Files button. Next, right-click the project node and unload the project. You should then be able to simply click the project node to see the project file. If not, right-click that node and select the Edit Project File option.

So add one. Do that, save the project file and then reload the project. Your using declaration should then compile successfully. You may have to choose Rebuild rather than Build.
As I said in my previous post, your recommendations worked perfectly.

But then I realised that that was an EXISTING solution/project.
So, I thought the initial cause of the problem was (maybe) that when I had created that solution/project, I had selected SOMETHING that was not the latest version.
After all, with the existinf solution/project if I add that node in https://www.thecodebuzz.com/wp-conte...r.-768x190.jpg then it works.
So, I decided to test this theory:

I created a totally new solution and project, and this time I made sure that I selected EVERYTHING as the latest version:
https://i.imgur.com/fGvCnxS.jpeg
https://i.imgur.com/4uiR9nM.jpeg

And yet the problem repeats:
https://i.imgur.com/SXMDIZg.jpeg

So, I am wondering what I should do to PREVENT this error.

I know that I can apply your recommendations to the new solution/project and then it will work.
But, now that I have created the new solution/project with the latest version of everything, then that problem should not happen in the first place.
So, why does it happen?

Please correct me if I am wrong: I think when a solution/project is created with the latest verion of everything, then it has the latest version of CSharp compiler which understands all the newest syntax and newest CShart instructions (such as "using declaration"), so, why doesn't it understand that syntax?

Thanks again.

31. Re: Problem with conversion of values

It's like you didn't even read what I posted. You're still targeting .NET Framework. If you want to use later than C# 7.3 then you have to target .NET Core. .NET 5 and later are based on .NET Core. You can keep targeting .NET Framework if you want - I believe that 4.6.2 is the oldest version still supported and 4.8.1 will be supported for some time yet, but then you are stuck with C# 7.3 so stop complaining about features it doesn't have. If you want the new features, target at least .NET 6, although .NET 8 is the most recent version.

32. Re: Problem with conversion of values

Originally Posted by jmcilhinney
It's like you didn't even read what I posted. You're still targeting .NET Framework. If you want to use later than C# 7.3 then you have to target .NET Core. .NET 5 and later are based on .NET Core. You can keep targeting .NET Framework if you want - I believe that 4.6.2 is the oldest version still supported and 4.8.1 will be supported for some time yet, but then you are stuck with C# 7.3 so stop complaining about features it doesn't have. If you want the new features, target at least .NET 6, although .NET 8 is the most recent version.
Thanks a lot for your help.
But, looks like I am stuck.

I deleted the whole new solution/project and tried to create it from scratch.
But, looks like there is no item to choose for ".Net Core" + "Windows Forms".
Even there isn't any option to choose for ".Net Core" alone (even without "Windows Forms", let alone with it).

Here is the screen:
https://i.imgur.com/MjFpyY0.jpeg

And here is the result of my search:
https://i.imgur.com/7yFfVtc.jpeg

And another search:
https://i.imgur.com/9S4UQfo.jpeg

So, how do I create a solution/project that is ".Net Core" + "Windows Forms"?

Thanks again.

33. Re: Problem with conversion of values

Look at the first screenshot in post #32. Look at the first two project templates in the list. The second one specifically states that it targets .NET Framework. The first one does not. What do you suppose it targets?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•