display value of database [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have code like this
Global.dbCon.Open();
int idQuestion;
kalimatSql = kalimatSql;
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
//messageBox.Show(idQuestion.ToString()); -->first message box
}
}
//messageBox.Show(idQuestion.ToString()); -->second message box
Global.dbCon.Close();
i don't have problem to display first messagebox, but how to display second messagebox
edited
i try to make code from #rhughes become a function (class) like this following code
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach (int id in idQuestions) {
return id;
}
but it's no work because not all code paths return a value...i wonder how corret way to do it?

Try something like this:
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
kalimatSql = kalimatSql;
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach (int id in idQuestions)
{
messageBox.Show(id.ToString());
}
What we are doing here is adding all of the question ids into a list and then displaying each of them afterwards.

Related

Determine a range is in the set of range? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 days ago.
Improve this question
I have some sets (10,23), (15,22), (20,25), (29,31), (30, 31).
I have to write a function in C# to determine below scenario.
(22,30) is not in the range.
and (19,23) is in the range.
How to prove (22,30) is not in the range comparing with each existing sets?
at the same time prove (19,23) is in the range.
How can I prove this in c#?
void Main()
{
List<MyRange> ranges = new List<MyRange> {
new MyRange{Start=10, End=23},
new MyRange{Start=15, End=22},
new MyRange{Start=20, End=25},
new MyRange{Start=29, End=31},
new MyRange{Start=30, End=31},
};
var testRange1 = new MyRange { Start = 22, End = 30 };
var testRange2 = new MyRange { Start = 19, End = 23 };
Console.WriteLine(IsInclusiveRange(testRange1, ranges));
Console.WriteLine(IsInclusiveRange(testRange2, ranges));
}
private bool IsInclusiveRange(MyRange toTest, IEnumerable<MyRange> ranges)
{
return ranges.Any(r => r.Start <= toTest.Start && r.End >= toTest.End);
}
class MyRange
{
public int Start { get; set; }
public int End { get; set; }
}

finding amount of same texts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
hello I am making a survival game but I don't know how to make if there is some same text in a string it deletes it and then makes it to
<amount><item.title>
here is the code
string[] needs = new string[items.Length];
string text = "";
for (int i = 0; i < items.Length; i++)
{
text += items[i].title + ",";
}
If you're trying to create a string without duplicates based on your list of items, you can first create another list of items that doesn't contain duplicates. You will need LINQ for this.
var distinctItemGroups = items.GroupBy(item => item.Title);
I would also recommend using a StringBuilder for the text. You can then do something like this:
var displayText = new StringBuilder();
foreach (var group in distinctItemGroups)
{
displayText.Append(group.First().Title + ", ");
}
To actually display the text from the StringBuilder just call ToString() on it.
I think you need:
Create a class for count items
public class MyResultModel
{
public string Title { get; set; }
public int Count { get; set; }
public override string ToString()
{
return $"{Count}{Title}";
}
}
And fill the items with
IEnumerable<MyResultModel> myModels = items.GroupBy(x => x)
.Select(x => new MyResultModel() { Count = x.Count(), Title = x.Key });
Then you can generate a string:
string result = string.Join(",", myModels.Select(x=> x.ToString()));

Kattis - Recount C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I'm trying to solve https://open.kattis.com/problems/recount in C#. I have my code written but I know I'm missing something but I'm stuck and I've been working on this for a few days. Whenever I try to run my code I receive a runtime error and I know it's because I need to be able to add a user inputted list to convert to a dictionary but I'm not sure how.
Running on .NET 3.1 for school
Here is the code I have currently
namespace Kattis_Solution___Recount
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<string, int> votes = new SortedDictionary<string, int>();
List<string> names = new List<string>();
string name = "";
name = Console.ReadLine();
Console.Write(name);
Console.Write("\n");
int max = 0;
string winner = "";
while (name[0] != '*')
{
if (votes[name] == 0)
{
names.Add(name);
}
votes[name]++;
if (votes[name] > max)
{
max = votes[name];
winner = name;
}
name = Console.ReadLine();
Console.Write(name);
Console.Write("\n");
}
Console.Write(winner);
Console.Write("\n");
for (int i = 1; i < names.Count; i++)
{
if (votes[names[i]] == max && winner != names[i])
{
Console.Write("Runoff!");
Console.Write("\n");
break;
}
}
}
}
}
You cannot do this if the entry isnt there
if (votes[name] == 0) {
names.Add(name);
}
(unlike some other languages - c++ for example)
You need to do
if (!votes.ContainsKey(name)) {
names.Add(name);
votes[name] = 0;
}
See docs here https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.item?view=net-6.0
The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.

C# Can I identify what Random selects between the range? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to be able to determine what the value is selected when I get a random number in windows forms.
For example, int mynum = numbers[rand.Next(0,7)];
I know the range is between 0 and 7, but I want to be able to identify what number is selected. e.g. numbers[3]
The following is the code I have. I have read binary values into an array int [] numbers and passed it through the following method. It generates my random 3 bit binary value, but I am making a small quiz where the number identified is the value the random number selects (e.g. the location 3 is the correct value for the item stored there, numbers[i] ==answer).
Is there a way to access this value?
public void generateBinary3bit(int[] numbers, Form1 f1)
{
Random rand = new Random();
int mynum = numbers[rand.Next(0,7)];
**int ans = numbers[rand];**
f1.lblBinaryText.Text = mynum.ToString();
}
For context, I am reading in the values into the array here:
int [] numbers = new int[8];
public void readNumbers(int[]numbers, Form1 F1)
{
try
{
StreamReader sr = new StreamReader(#"C:\BinaryNumbers.csv");
while(sr.ReadLine() !=null)
{
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = Convert.ToInt32(sr.ReadLine());
answer = i;
}
}
sr.Close();
}
catch ( Exception e)
{
}
}
Thank you,
Just assign some variable to the result of the rand.Next() before using it in the array:
Random rand = new Random();
int randIndex = rand.Next(0,7);
int mynum = numbers[randIndex];
and you are good to go.

Cannot implicitly convert type 'int' to 'string[]'? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to count the row on table. Based on that count I have to Assign Team. While using the given below code showing an error Cannot implicitly convert type 'int' to 'string[]'.
Code:
protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
{
ArrayList teamList = new ArrayList();
ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
DataTable dt = new DataTable();
dt = cd.GetAvailableData(ddlCircle.SelectedValue);
int x;
x = dt.Rows.Count;
String[] a;
for (int i = 1; i <= x; i++)
{
a = teamList.Add(String.Format("Team{0}", i)); // error popup here
}
}
The returned value type of Add() method is int, and you are assigning it to string[] array. Change your code to:
for (int i = 1; i <= x; i++)
{
teamList.Add(String.Format("Team{0}", i));
}
Also if you want to convert ArrayList to string[], you can use following code:
a = teamList.ToArray(typeof(string)) as string[];
If I were you, I would change teamList to something like this...
var teamList = new List<string>();
and then when it comes to populating the List do this:
for (int i = 1; i <= x; i++)
{
teamList.Add(string.Format("Team{0}",i));
}
Then once the loop is finished you can just call:
a = teamList.ToArray()
and then your string array will be teamList in array form.
Is there a specific reason you would need to asign it to that array and could not just pass the list out?

Categories

Resources