I can't set my reference numbers's format to “byte” or “short” in console project?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Actually this is my first question on this site and i hope it's right way to ask this type question.
I'm just learning C# from scratch in Youtube.
I created simple console project that has multiply function as you can see below, but I can't set my reference numbers (number1, number2) to "short" or "byte". But I can set them "long", "decimal" "int" etc.
I did little search and I have theory that this is about bits and bytes counts of each parameter but can't really comprehend the concept.
May anyone explain this error that I'm encountering with simple language any chance? Thanks for any explanation :)
using System;
namespace HelloWorld
{
class Program
{
static void Main(string args)
{
try
{
Start:
int number1;
int number2;
Console.Write("enter number to multiply:");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("enter another number:");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the result is:" + number1 * number2 + "(" + number1 + "X" + number2 + ")");
Console.WriteLine();
goto Start;
}
catch (Exception)
{
Console.WriteLine("hey silly! that's not even a number:)");
}
}
}
}
c# console
add a comment |
Actually this is my first question on this site and i hope it's right way to ask this type question.
I'm just learning C# from scratch in Youtube.
I created simple console project that has multiply function as you can see below, but I can't set my reference numbers (number1, number2) to "short" or "byte". But I can set them "long", "decimal" "int" etc.
I did little search and I have theory that this is about bits and bytes counts of each parameter but can't really comprehend the concept.
May anyone explain this error that I'm encountering with simple language any chance? Thanks for any explanation :)
using System;
namespace HelloWorld
{
class Program
{
static void Main(string args)
{
try
{
Start:
int number1;
int number2;
Console.Write("enter number to multiply:");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("enter another number:");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the result is:" + number1 * number2 + "(" + number1 + "X" + number2 + ")");
Console.WriteLine();
goto Start;
}
catch (Exception)
{
Console.WriteLine("hey silly! that's not even a number:)");
}
}
}
}
c# console
I am not sure how to answer your question because I couldn't clearly understand it. can you include the error you got when you tried to set yournumber1
tobyte
orshort
? Moreover, just a hint: Don't usegoto
, just put your whole code in a loop instead.
– Paul Karam
Nov 23 '18 at 14:22
Thanks for advice. I will check for loop. If I set them byte or short, Visual Studio can't compile them and says "there were build errors."
– Carla Köhler
Nov 23 '18 at 14:31
What errors are you getting when declaring those variables as short or byte?
– Alex Leo
Nov 23 '18 at 14:53
add a comment |
Actually this is my first question on this site and i hope it's right way to ask this type question.
I'm just learning C# from scratch in Youtube.
I created simple console project that has multiply function as you can see below, but I can't set my reference numbers (number1, number2) to "short" or "byte". But I can set them "long", "decimal" "int" etc.
I did little search and I have theory that this is about bits and bytes counts of each parameter but can't really comprehend the concept.
May anyone explain this error that I'm encountering with simple language any chance? Thanks for any explanation :)
using System;
namespace HelloWorld
{
class Program
{
static void Main(string args)
{
try
{
Start:
int number1;
int number2;
Console.Write("enter number to multiply:");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("enter another number:");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the result is:" + number1 * number2 + "(" + number1 + "X" + number2 + ")");
Console.WriteLine();
goto Start;
}
catch (Exception)
{
Console.WriteLine("hey silly! that's not even a number:)");
}
}
}
}
c# console
Actually this is my first question on this site and i hope it's right way to ask this type question.
I'm just learning C# from scratch in Youtube.
I created simple console project that has multiply function as you can see below, but I can't set my reference numbers (number1, number2) to "short" or "byte". But I can set them "long", "decimal" "int" etc.
I did little search and I have theory that this is about bits and bytes counts of each parameter but can't really comprehend the concept.
May anyone explain this error that I'm encountering with simple language any chance? Thanks for any explanation :)
using System;
namespace HelloWorld
{
class Program
{
static void Main(string args)
{
try
{
Start:
int number1;
int number2;
Console.Write("enter number to multiply:");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("enter another number:");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the result is:" + number1 * number2 + "(" + number1 + "X" + number2 + ")");
Console.WriteLine();
goto Start;
}
catch (Exception)
{
Console.WriteLine("hey silly! that's not even a number:)");
}
}
}
}
c# console
c# console
edited Nov 23 '18 at 14:20
fubo
31.1k970107
31.1k970107
asked Nov 23 '18 at 14:19
Carla KöhlerCarla Köhler
183
183
I am not sure how to answer your question because I couldn't clearly understand it. can you include the error you got when you tried to set yournumber1
tobyte
orshort
? Moreover, just a hint: Don't usegoto
, just put your whole code in a loop instead.
– Paul Karam
Nov 23 '18 at 14:22
Thanks for advice. I will check for loop. If I set them byte or short, Visual Studio can't compile them and says "there were build errors."
– Carla Köhler
Nov 23 '18 at 14:31
What errors are you getting when declaring those variables as short or byte?
– Alex Leo
Nov 23 '18 at 14:53
add a comment |
I am not sure how to answer your question because I couldn't clearly understand it. can you include the error you got when you tried to set yournumber1
tobyte
orshort
? Moreover, just a hint: Don't usegoto
, just put your whole code in a loop instead.
– Paul Karam
Nov 23 '18 at 14:22
Thanks for advice. I will check for loop. If I set them byte or short, Visual Studio can't compile them and says "there were build errors."
– Carla Köhler
Nov 23 '18 at 14:31
What errors are you getting when declaring those variables as short or byte?
– Alex Leo
Nov 23 '18 at 14:53
I am not sure how to answer your question because I couldn't clearly understand it. can you include the error you got when you tried to set your
number1
to byte
or short
? Moreover, just a hint: Don't use goto
, just put your whole code in a loop instead.– Paul Karam
Nov 23 '18 at 14:22
I am not sure how to answer your question because I couldn't clearly understand it. can you include the error you got when you tried to set your
number1
to byte
or short
? Moreover, just a hint: Don't use goto
, just put your whole code in a loop instead.– Paul Karam
Nov 23 '18 at 14:22
Thanks for advice. I will check for loop. If I set them byte or short, Visual Studio can't compile them and says "there were build errors."
– Carla Köhler
Nov 23 '18 at 14:31
Thanks for advice. I will check for loop. If I set them byte or short, Visual Studio can't compile them and says "there were build errors."
– Carla Köhler
Nov 23 '18 at 14:31
What errors are you getting when declaring those variables as short or byte?
– Alex Leo
Nov 23 '18 at 14:53
What errors are you getting when declaring those variables as short or byte?
– Alex Leo
Nov 23 '18 at 14:53
add a comment |
2 Answers
2
active
oldest
votes
You are on the right track; it's about implicit conversion of types and strong typing.
Not to overload you with all the details, you can make some adjustments for
short:
note the ToInt16
short number1;
//etc
number1 = Convert.ToInt16(Console.ReadLine());
byte:
note: the ToByte
byte number1;
//etc
number1 = Convert.ToByte(Console.ReadLine());
long
left as an exercise
Crucial is that, although all the types consists of bits, and are basically numbers, C# want to be sure you mean what you are saying and enforces you to use the correct type
.
So, why does it work for
decimal
etc.?Is because C# thinks it's valid to implicitly convert them, and therefor the compiler is doing it for you.
In the link you can see that for int
(aka Int32
) the folowing implicit conversions are predefined:
//from | to
//int | long, float, double, or decimal
In the linked table you can also see that the other way round, from byte
to int
is allowed.
The big lesson: C# is strong typed: if you say it's an Int16 (aka short), you must use it only as an
Int16
(and not an Int32
)Happy coding ;-)
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
add a comment |
if I understand your problem - in order to "set" a variable to a given type you will need to declare it as such before compiling (before the program run).
So if you want to work with long numbers you will need to declare:
long number1;
long number2;
Your conversions should reflect it:
number1 = Convert.ToInt64(Console.ReadLine());
For decimal
decimal number1;
decimal number2;
Conversion e.g. decimal.parse(number1).
For byte
byte number1;
byte number2;
Again your conversion must enforced per each type.
Just a note. Using a goto is something that I don't approve neither suggest. Learn to use a loop. If those videos are using the goto, I would advise to learn from a better source.
Hope it helps.
1
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448380%2fi-cant-set-my-reference-numberss-format-to-byte-or-short-in-console-projec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are on the right track; it's about implicit conversion of types and strong typing.
Not to overload you with all the details, you can make some adjustments for
short:
note the ToInt16
short number1;
//etc
number1 = Convert.ToInt16(Console.ReadLine());
byte:
note: the ToByte
byte number1;
//etc
number1 = Convert.ToByte(Console.ReadLine());
long
left as an exercise
Crucial is that, although all the types consists of bits, and are basically numbers, C# want to be sure you mean what you are saying and enforces you to use the correct type
.
So, why does it work for
decimal
etc.?Is because C# thinks it's valid to implicitly convert them, and therefor the compiler is doing it for you.
In the link you can see that for int
(aka Int32
) the folowing implicit conversions are predefined:
//from | to
//int | long, float, double, or decimal
In the linked table you can also see that the other way round, from byte
to int
is allowed.
The big lesson: C# is strong typed: if you say it's an Int16 (aka short), you must use it only as an
Int16
(and not an Int32
)Happy coding ;-)
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
add a comment |
You are on the right track; it's about implicit conversion of types and strong typing.
Not to overload you with all the details, you can make some adjustments for
short:
note the ToInt16
short number1;
//etc
number1 = Convert.ToInt16(Console.ReadLine());
byte:
note: the ToByte
byte number1;
//etc
number1 = Convert.ToByte(Console.ReadLine());
long
left as an exercise
Crucial is that, although all the types consists of bits, and are basically numbers, C# want to be sure you mean what you are saying and enforces you to use the correct type
.
So, why does it work for
decimal
etc.?Is because C# thinks it's valid to implicitly convert them, and therefor the compiler is doing it for you.
In the link you can see that for int
(aka Int32
) the folowing implicit conversions are predefined:
//from | to
//int | long, float, double, or decimal
In the linked table you can also see that the other way round, from byte
to int
is allowed.
The big lesson: C# is strong typed: if you say it's an Int16 (aka short), you must use it only as an
Int16
(and not an Int32
)Happy coding ;-)
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
add a comment |
You are on the right track; it's about implicit conversion of types and strong typing.
Not to overload you with all the details, you can make some adjustments for
short:
note the ToInt16
short number1;
//etc
number1 = Convert.ToInt16(Console.ReadLine());
byte:
note: the ToByte
byte number1;
//etc
number1 = Convert.ToByte(Console.ReadLine());
long
left as an exercise
Crucial is that, although all the types consists of bits, and are basically numbers, C# want to be sure you mean what you are saying and enforces you to use the correct type
.
So, why does it work for
decimal
etc.?Is because C# thinks it's valid to implicitly convert them, and therefor the compiler is doing it for you.
In the link you can see that for int
(aka Int32
) the folowing implicit conversions are predefined:
//from | to
//int | long, float, double, or decimal
In the linked table you can also see that the other way round, from byte
to int
is allowed.
The big lesson: C# is strong typed: if you say it's an Int16 (aka short), you must use it only as an
Int16
(and not an Int32
)Happy coding ;-)
You are on the right track; it's about implicit conversion of types and strong typing.
Not to overload you with all the details, you can make some adjustments for
short:
note the ToInt16
short number1;
//etc
number1 = Convert.ToInt16(Console.ReadLine());
byte:
note: the ToByte
byte number1;
//etc
number1 = Convert.ToByte(Console.ReadLine());
long
left as an exercise
Crucial is that, although all the types consists of bits, and are basically numbers, C# want to be sure you mean what you are saying and enforces you to use the correct type
.
So, why does it work for
decimal
etc.?Is because C# thinks it's valid to implicitly convert them, and therefor the compiler is doing it for you.
In the link you can see that for int
(aka Int32
) the folowing implicit conversions are predefined:
//from | to
//int | long, float, double, or decimal
In the linked table you can also see that the other way round, from byte
to int
is allowed.
The big lesson: C# is strong typed: if you say it's an Int16 (aka short), you must use it only as an
Int16
(and not an Int32
)Happy coding ;-)
edited Nov 23 '18 at 14:50
answered Nov 23 '18 at 14:28
StefanStefan
8,67473962
8,67473962
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
add a comment |
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
Thanks Stefan! That link helped me a lot to understand my struggle and ToInt16 solved it :)
– Carla Köhler
Nov 23 '18 at 17:28
add a comment |
if I understand your problem - in order to "set" a variable to a given type you will need to declare it as such before compiling (before the program run).
So if you want to work with long numbers you will need to declare:
long number1;
long number2;
Your conversions should reflect it:
number1 = Convert.ToInt64(Console.ReadLine());
For decimal
decimal number1;
decimal number2;
Conversion e.g. decimal.parse(number1).
For byte
byte number1;
byte number2;
Again your conversion must enforced per each type.
Just a note. Using a goto is something that I don't approve neither suggest. Learn to use a loop. If those videos are using the goto, I would advise to learn from a better source.
Hope it helps.
1
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
add a comment |
if I understand your problem - in order to "set" a variable to a given type you will need to declare it as such before compiling (before the program run).
So if you want to work with long numbers you will need to declare:
long number1;
long number2;
Your conversions should reflect it:
number1 = Convert.ToInt64(Console.ReadLine());
For decimal
decimal number1;
decimal number2;
Conversion e.g. decimal.parse(number1).
For byte
byte number1;
byte number2;
Again your conversion must enforced per each type.
Just a note. Using a goto is something that I don't approve neither suggest. Learn to use a loop. If those videos are using the goto, I would advise to learn from a better source.
Hope it helps.
1
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
add a comment |
if I understand your problem - in order to "set" a variable to a given type you will need to declare it as such before compiling (before the program run).
So if you want to work with long numbers you will need to declare:
long number1;
long number2;
Your conversions should reflect it:
number1 = Convert.ToInt64(Console.ReadLine());
For decimal
decimal number1;
decimal number2;
Conversion e.g. decimal.parse(number1).
For byte
byte number1;
byte number2;
Again your conversion must enforced per each type.
Just a note. Using a goto is something that I don't approve neither suggest. Learn to use a loop. If those videos are using the goto, I would advise to learn from a better source.
Hope it helps.
if I understand your problem - in order to "set" a variable to a given type you will need to declare it as such before compiling (before the program run).
So if you want to work with long numbers you will need to declare:
long number1;
long number2;
Your conversions should reflect it:
number1 = Convert.ToInt64(Console.ReadLine());
For decimal
decimal number1;
decimal number2;
Conversion e.g. decimal.parse(number1).
For byte
byte number1;
byte number2;
Again your conversion must enforced per each type.
Just a note. Using a goto is something that I don't approve neither suggest. Learn to use a loop. If those videos are using the goto, I would advise to learn from a better source.
Hope it helps.
answered Nov 23 '18 at 14:40
Alex LeoAlex Leo
8872314
8872314
1
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
add a comment |
1
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
1
1
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Thanks Alex! I solved with using ToInt16 and stopped using "goto" :)
– Carla Köhler
Nov 23 '18 at 17:29
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
Good stuff. Happy coding :-)
– Alex Leo
Nov 24 '18 at 18:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448380%2fi-cant-set-my-reference-numberss-format-to-byte-or-short-in-console-projec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am not sure how to answer your question because I couldn't clearly understand it. can you include the error you got when you tried to set your
number1
tobyte
orshort
? Moreover, just a hint: Don't usegoto
, just put your whole code in a loop instead.– Paul Karam
Nov 23 '18 at 14:22
Thanks for advice. I will check for loop. If I set them byte or short, Visual Studio can't compile them and says "there were build errors."
– Carla Köhler
Nov 23 '18 at 14:31
What errors are you getting when declaring those variables as short or byte?
– Alex Leo
Nov 23 '18 at 14:53