1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
program SS; const Stroka : array [0..15] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); var a,b,strok, y,x,x3, n,i, code:integer; x1, DOO1, POSLE1 :string; x2:char; {для первого массива} NN,DOOO1: Longint; {Любая система сч из 10} H : String; begin writeln('Программа переводит любое чилсо из любой СС в в любую СС: '); write('Введите число с точностью до 2-х знаков после запятой: '); readln(x1); write('Введите СС этого числа: '); readln(n); write('Введите СС в которую нужно перевести: '); readln(y); //---------------------------------------------------------- strok:=length(x1); for i:=1 to strok do begin x2:=x1[i]; x3:=i-1; if x2 = '.' then DOO1:= Copy(x1,1,x3); {до} POSLE1 := Copy(x1,x3,strok) ; {после} end; Writeln (DOO1); Writeln (POSLE1); //------------------------------------------------------------- {Из ДЕСЯТИЧНОЙ СС в ЛЮБУЮ целой части} begin Write('Введите положительное число в десятичной СС: '); ReadLn(NN); repeat Val(DOO1,DOOO1,code); H := Stroka[DOOO1 mod y] + H; DOOO1 := DOO1 div y; until DOOO1 = 0; if code <>0 then Writeln('Ошибка при преобразовании в позиции:', code) else WriteLn('В',y,' - ричной системе = ', H); end; end. |
I keep getting the debug error «cannot implicitly convert type ‘string’ to ‘int'» in C#.
Here is a snippet of my code:
private void button2_Click(object sender, EventArgs e) //button to start takedown
{
byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes("<Packet OF Data Here>");
string IP = "127.0.0.1";
int port = "80";
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(packetData, ep);
}
dee-see
23.5k5 gold badges58 silver badges91 bronze badges
asked Jun 30, 2012 at 13:48
0
If possible:
int port = 80;
If you cannot have an int variable you will have to parse it:
int port = Int32.Parse("80");
e.g.
string a = "80";
int port = Int32.Parse(a);
answered Jul 2, 2012 at 10:04
JenninhaJenninha
1,3472 gold badges20 silver badges42 bronze badges
2
Here is the error:
int port = "80";
convert it into
int port=80;
answered Jun 30, 2012 at 13:49
Ashwin SinghAshwin Singh
7,1674 gold badges36 silver badges55 bronze badges
You have to convert string
to int
here:
int port = "80"; // can't assign string to int
Just pass it as int:
int port = 80;
answered Jun 30, 2012 at 13:49
ZbigniewZbigniew
27k6 gold badges58 silver badges65 bronze badges
In your case, everyone else’s answer that port needs to be of type «int» instead of type «string» is correct. However, if you really had a string from user input, and you needed to convert it back into an int Int32.TryParse or Int32.Parse will suffice.
answered Jun 30, 2012 at 18:02
selbieselbie
97.9k14 gold badges102 silver badges171 bronze badges
int port = "80";
is incorrect because int
expected an integer, not a string. By using speech marks, you’re provding 80
as a string, not as a integer. Simply remove the speech marks so that you’re assigning the variable as an integer.
int port = 80;
answered Jun 30, 2012 at 13:51
keyboardPkeyboardP
68.6k13 gold badges156 silver badges205 bronze badges
You can’t mention integer in «» as you have done int port = "80";
correct version should be int port = 80;
answered Jul 2, 2012 at 10:55
LearningLearning
19.4k39 gold badges176 silver badges370 bronze badges
change
int port = «80»;
to
var port = «80»;
And
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
to
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), Convert.ToInt32(port));
answered May 10, 2017 at 16:37
BalaBala
456 bronze badges
Узнай цену своей работы
Формулировка задачи:
Всем привет.
Необходимо исправить ошибку (в 26 строке преобразование типа string к типу integer).
Так же по возможности прошу преобразовать, упростить код.
ЗАРАНЕЕ БЛАГОДАРЮ!!!
Код к задаче: «Исправление ошибки (преобразование string=>integer) + оптимизация кода»
textual
Листинг программы
var names:array[1..20] of string; i,n,k:integer; name:string; // Тело begin randomize; // Количество игроков write('Количество игроков: '); readln(n); // Имена writeln('Введите имена игроков'); for i:=1 to n do begin write(i,'. '); readln(names[i]); end; write(names[random(n+1)]); end.
Полезно ли:
13 голосов , оценка 4.154 из 5
Похожие ответы
- Оптимизация кода.
- Что не так с кодом?
- Ошибка в программе «встречено ‘.’, а ожидвлось «
- Кодирование текста в двоичный код
- Ошибка «Ожидалось имя процедуры или функции»
- Проверьте код
- Составить блок-схему к готовому коду
- Нельзя преобразовать тип real к integer
- Операция ‘and’ не применима к типу KeyValuePair
- Ошибка в коде, Incompatible types
- Выдает ошибку
Console.WriteLine ("Please enter some numbers");
int sum = 0;
for(;;)
{
string input = Console.ReadLine ();
if (string.IsNullOrEmpty (input))
{
break;
}
int inputParsed = int.Parse (input.ToString ());
int sumParsed = int.Parse (sum.ToString ());
sum = sum + input; // throws an error here
Console.WriteLine (sum);
I want my programme to show the sum of all numbers entered by user, by even though I have parsed all the variables needed, it throws an «cannot implicitly convert type ‘string’ to ‘int'» error. What’s wrong?
asked Jul 23, 2013 at 17:15
5
sum = sum + input; //throws an error here
should be:
sum = sum + inputParsed ;
You are using the original input instead of the parsed value. And you don’t need sumParsed
because you just keep the total sum in sum
and you do no need to cast the int to a string and then parse it back to an integer.
answered Jul 23, 2013 at 17:16
aweisaweis
5,2484 gold badges29 silver badges46 bronze badges
1
to check if the input of the user is right i would prefer
int userInput = 0;
if( int.TryParse( input, out userInput ) == false )
{
break;
}
This is just an advise and not directly a solution to your problem.
There are enough answers =)
answered Jul 23, 2013 at 17:21
ViperViper
2,2161 gold badge21 silver badges41 bronze badges
int inputParsed = int.Parse (input.ToString ());
//int sumParsed = int.Parse (sum.ToString ());//no need
sum = sum + inputParsed ;
answered Jul 23, 2013 at 17:18
AD.NetAD.Net
13.4k2 gold badges28 silver badges47 bronze badges
I would rewrite this entirely (Your original error was because you were trying to add a string
to an int
, and not the parsed input as an int
)
Console.WriteLine ("Please enter some numbers");
int sum = 0;
while (true)
{
int parsedInput = 0;
string input = Console.ReadLine();
if (!string.IsNullOrEmpty(input) && int.TryParse(input, out parsedInput))
{
sum += parsedInput;
Console.WriteLine (sum);
}
else
break;
}
answered Jul 23, 2013 at 17:24
CyralCyral
13.9k6 gold badges50 silver badges89 bronze badges
2
- Remove From My Forums
-
Question
-
User-1564517746 posted
Hi,
I know it is a very simple question, but unfortunately i am very new to these concepts, hence kindly help me. I am trying to add a new user into the database, hence i decided to use Textboxes inorder to add. But some of the columns such as ID is of
integer datatype, while Gender is of Char datatype, DateofBirth is of DateTime type.Now how do i save a value for ID, Gender etc into the database using textboxes.
I am primarily working with the UserInfo class. The Userinfo is a class object that stores the data representation of the users table in the database.
ui.ID=tbID.text; // this is where i am having the error » Cannot implicitly convert type ‘string’ to ‘int’ «
Thanks
Zaid Papa
Answers
-
User211842812 posted
You could try something like this.
ui.ID = Int32.Parse(tbID.text)
-
Marked as answer by
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by
-
User445179017 posted
ui.ID = Convert.ToInt32(tbID.Text);
Thanks
-
Marked as answer by
Anonymous
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by