rallez
rallez
CC#
Created by rallez on 2/26/2024 in #help
Avalonia UI Unable to find suitable setter or adder for property Styles
Hey im starting with Avlonia UI. (BTW give your thought sif should I go for it or something else?). My really simple program doesn't comiple for reason:
Error AVLN:0004 Avalonia: Unable to find suitable setter or adder for property Styles of type Avalonia.Base:Avalonia.StyledElement for argument System.Runtime:System.Object, available setter parameter lists are:
Avalonia.Styling.IStyle
Avalonia.Styling.IStyle Line 23, position 57.
Error AVLN:0004 Avalonia: Unable to find suitable setter or adder for property Styles of type Avalonia.Base:Avalonia.StyledElement for argument System.Runtime:System.Object, available setter parameter lists are:
Avalonia.Styling.IStyle
Avalonia.Styling.IStyle Line 23, position 57.
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
Width="400"
Height="600"
CanResize="False"
WindowStartupLocation="CenterScreen"
WindowState="Normal"
Background="#0D1B2A"
x:Class="Calculator.MainWindow"
Title="Calculator"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaTitleBarHeightHint="-1"
ExtendClientAreaChromeHints="NoChrome"
PointerPressed="DragMove">

<Grid ShowGridLines="True" RowDefinitions="1.5*,3*,15*">

<Grid Grid.Row="0" ColumnDefinitions="*, Auto, 50">
<TextBlock Grid.Column="0" Text="Calculator" />
<Button Grid.Column="2" Click="CloseWindow" Styles="{StaticResource CloseButtonStyle}"/>


</Grid>

<Grid Grid.Row="1" />

<Grid Grid.Row="2" />

</Grid>

</Window>
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
Width="400"
Height="600"
CanResize="False"
WindowStartupLocation="CenterScreen"
WindowState="Normal"
Background="#0D1B2A"
x:Class="Calculator.MainWindow"
Title="Calculator"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaTitleBarHeightHint="-1"
ExtendClientAreaChromeHints="NoChrome"
PointerPressed="DragMove">

<Grid ShowGridLines="True" RowDefinitions="1.5*,3*,15*">

<Grid Grid.Row="0" ColumnDefinitions="*, Auto, 50">
<TextBlock Grid.Column="0" Text="Calculator" />
<Button Grid.Column="2" Click="CloseWindow" Styles="{StaticResource CloseButtonStyle}"/>


</Grid>

<Grid Grid.Row="1" />

<Grid Grid.Row="2" />

</Grid>

</Window>
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Design.PreviewWith>
<Border Padding="20">
</Border>
</Design.PreviewWith>

<Style x:Key="CloseButtonStyle" Selector="Button">
<Setter Property="Background" Value="#E6E8E6"/>
</Style>

</Styles>
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Design.PreviewWith>
<Border Padding="20">
</Border>
</Design.PreviewWith>

<Style x:Key="CloseButtonStyle" Selector="Button">
<Setter Property="Background" Value="#E6E8E6"/>
</Style>

</Styles>
5 replies
CC#
Created by rallez on 7/1/2023 in #help
✅ Basic http client get request
Hi why my code won't return any value
static async Task<int> GetServerTime()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("http://XXX/");
response.EnsureSuccessStatusCode();

string result = await response.Content.ReadAsStringAsync();
int serverTime = int.Parse(result);
return serverTime;
}
catch (HttpRequestException ex)
{
Console.WriteLine("Błąd: " + ex.Message);
throw;
}
}
}

async void Test()
{
Console.WriteLine(await GetServerTime());
}

Test();
static async Task<int> GetServerTime()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("http://XXX/");
response.EnsureSuccessStatusCode();

string result = await response.Content.ReadAsStringAsync();
int serverTime = int.Parse(result);
return serverTime;
}
catch (HttpRequestException ex)
{
Console.WriteLine("Błąd: " + ex.Message);
throw;
}
}
}

async void Test()
{
Console.WriteLine(await GetServerTime());
}

Test();
When i dont use Test method instead just using
Console.WriteLine(await GetServerTime());
Console.WriteLine(await GetServerTime());
it works normally
33 replies
CC#
Created by rallez on 6/10/2023 in #help
print out all pairs of natural numbers whose sum isequal to the entered number n
Write an algorithm that prints out all pairs of natural numbers whose sum is equal to the entered number n. Example: n = 7 Paris: 0 + 7 1 + 6 2 + 5 3 + 4 I have no clue how to introduce it into c# lang
34 replies
CC#
Created by rallez on 2/13/2023 in #help
✅ Give object default values (destroy?)
Hi I have a program which after sign in assignes object user some values. While sign out i need to delete/destroy or set as default those vaules.
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public List<Character> Characters { get; set; }

public User(string username, int userid)
{
Username = username;
UserId = userid;
Characters = new List<Character>();
}

~User()
{
Username = "";
UserId = default;
Characters = new List<Character>();;
}
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public List<Character> Characters { get; set; }

public User(string username, int userid)
{
Username = username;
UserId = userid;
Characters = new List<Character>();
}

~User()
{
Username = "";
UserId = default;
Characters = new List<Character>();;
}
}
I've heard about deconstructor but i dont know how to use them to be honest. Please help me
22 replies
CC#
Created by rallez on 2/9/2023 in #help
Unable to connect to SQL
using System.Data.SqlClient;
Console.WriteLine("Database connector");
string connectionString = "Data Source=localhost,3306;Initial Catalog=testcon;User id=root;Password=~'!(J+P1W%YYM-0q9y~H;";
SqlConnection cnn;
cnn = new SqlConnection(connectionString);

try
{
Console.WriteLine("Connecting...");
cnn.Open();
Console.WriteLine("Connected!");

}
catch(SqlException)
{
Console.WriteLine("Unable to connect to database!");
}
finally
{
cnn.Close();
}


Console.ReadLine();
using System.Data.SqlClient;
Console.WriteLine("Database connector");
string connectionString = "Data Source=localhost,3306;Initial Catalog=testcon;User id=root;Password=~'!(J+P1W%YYM-0q9y~H;";
SqlConnection cnn;
cnn = new SqlConnection(connectionString);

try
{
Console.WriteLine("Connecting...");
cnn.Open();
Console.WriteLine("Connected!");

}
catch(SqlException)
{
Console.WriteLine("Unable to connect to database!");
}
finally
{
cnn.Close();
}


Console.ReadLine();
13 replies
CC#
Created by rallez on 2/8/2023 in #help
Generate random array and quicksort algorithm
Want to make a program that contains functions that generates random array, prints it and sorts it.
void GenArray(int[] myArray, int size, int minSize, int maxSize)
{
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < size; i++)
{
myArray[i] = random.Next(minSize, maxSize + 1);
}
}

void PrintArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
Console.WriteLine("[" + i + "] " + myArray[i]);
}
}

int Partition(int[] myArray, int l, int r)
{
int pivot = myArray[l];
int count = 0;

for(int i = l + 1; i <= r; i++)
{
if(myArray[i] <= pivot)
{
count++;
}
}

int pivotIndex = l + count;
(myArray[pivotIndex], myArray[l]) = (myArray[l], myArray[pivotIndex]);

int i2 = l, j = r;
while(i2 < pivotIndex && j > pivotIndex)
{
while(myArray[i2] <= pivot)
{
i2++;
}

while(myArray[j] > pivot)
{
j--;
}

if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2++], myArray[j--]) = (myArray[j--], myArray[i2++]);
}
}

return pivotIndex;
}

void QuickSort(int[] myArray, int l, int r)
{
if (l >= r)
{
return;
}
int p = Partition(myArray, l, r);
QuickSort(myArray, l, p - 1);
QuickSort(myArray, p + 1, r);
}
Console.Write("Provide size of array: ");
int size = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Provide range of array: ");
Console.Write("From: ");
int minSize = Convert.ToInt32(Console.ReadLine());

Console.Write("To: ");
int maxSize = Convert.ToInt32(Console.ReadLine());

int[] tab1 = new int[size];

Console.WriteLine("");
Console.WriteLine("Unsorted array: ");
GenArray(tab1, size, minSize, maxSize);
PrintArray(tab1, size);

Console.WriteLine("");
Console.WriteLine("Sorted array: ");
QuickSort(tab1, 0, size - 1);
PrintArray(tab1, size);

Console.ReadLine();
void GenArray(int[] myArray, int size, int minSize, int maxSize)
{
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < size; i++)
{
myArray[i] = random.Next(minSize, maxSize + 1);
}
}

void PrintArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
Console.WriteLine("[" + i + "] " + myArray[i]);
}
}

int Partition(int[] myArray, int l, int r)
{
int pivot = myArray[l];
int count = 0;

for(int i = l + 1; i <= r; i++)
{
if(myArray[i] <= pivot)
{
count++;
}
}

int pivotIndex = l + count;
(myArray[pivotIndex], myArray[l]) = (myArray[l], myArray[pivotIndex]);

int i2 = l, j = r;
while(i2 < pivotIndex && j > pivotIndex)
{
while(myArray[i2] <= pivot)
{
i2++;
}

while(myArray[j] > pivot)
{
j--;
}

if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2++], myArray[j--]) = (myArray[j--], myArray[i2++]);
}
}

return pivotIndex;
}

void QuickSort(int[] myArray, int l, int r)
{
if (l >= r)
{
return;
}
int p = Partition(myArray, l, r);
QuickSort(myArray, l, p - 1);
QuickSort(myArray, p + 1, r);
}
Console.Write("Provide size of array: ");
int size = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Provide range of array: ");
Console.Write("From: ");
int minSize = Convert.ToInt32(Console.ReadLine());

Console.Write("To: ");
int maxSize = Convert.ToInt32(Console.ReadLine());

int[] tab1 = new int[size];

Console.WriteLine("");
Console.WriteLine("Unsorted array: ");
GenArray(tab1, size, minSize, maxSize);
PrintArray(tab1, size);

Console.WriteLine("");
Console.WriteLine("Sorted array: ");
QuickSort(tab1, 0, size - 1);
PrintArray(tab1, size);

Console.ReadLine();
7 replies
CC#
Created by rallez on 2/8/2023 in #help
Function that swap two numbers
Hi i need a function that swap 2 numbers I have written code below but it seems to doesn't work. Where am I wrong?
(int, int) Swap(int first, int second)
{
int temp = first;
first = second;
second = temp;
return (first, second);
}

int a = 5;
int b = 10;

Console.WriteLine(a);
Console.WriteLine(b);
Swap(a, b);
Console.WriteLine(a);
Console.WriteLine(b);
(int, int) Swap(int first, int second)
{
int temp = first;
first = second;
second = temp;
return (first, second);
}

int a = 5;
int b = 10;

Console.WriteLine(a);
Console.WriteLine(b);
Swap(a, b);
Console.WriteLine(a);
Console.WriteLine(b);
18 replies