Method wont work C# When still called out on main [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am a new coder and I don't fully understand coding C# but my method wont work for some reason and I did call it out on main.
static void Main(string[] args)
{
BOT();
}
void BOT()
{
Random numberGen = new Random();
String ID = "A.P.B" + numberGen.Next(50, 1000);
int serial = numberGen.Next(10000000, 1000000000);
String PU = "AMD RYZEN" + numberGen.Next(1, 11);
String model = "PAC" + numberGen.Next(10, 1000);
Console.WriteLine("Hello, My name or my Combat ID is" + ID);
}

Your call to BOT is in a static method. You either need BOT to be static or you need to create an instance of the class that contains BOT to be able to call it.
static void BOT()
{
Random numberGen = new Random();
String ID = "A.P.B" + numberGen.Next(50, 1000);
int serial = numberGen.Next(10000000, 1000000000);
String PU = "AMD RYZEN" + numberGen.Next(1, 11);
String model = "PAC" + numberGen.Next(10, 1000);
Console.WriteLine("Hello, My name or my Combat ID is" + ID);
}
When I run it now I get:
Hello, My name or my Combat ID isA.P.B188

Related

Check if cyclic string or not

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 hours ago.
Improve this question
Enter a string of characters S. Indicates whether the string S is cyclic or not.
For example S='abcdabcdabcdabcd' is cyclic.
My teacher want me to do this but i think it's too hard. I'm thinking using SplitString but it's just an idea and i dont know how to do. I'm very thankful if someone gives me help
using System;
using System.Collections;
public class MYCLASSNAME {
public static void Main(string[] args){
var str = "abcdabcdabcdabcd";
Console.Write(checkCyclicString(str));
}
static bool checkCyclicString (string str){
var checkString ="";
for(var i=0;i<str.Length-1;i++){
checkString= checkString+ str[i];
var countOfCycles =0;
var haveCycles = false;
for(var j=i+1;j<str.Length-checkString.Length+1;j= j+checkString.Length){
if(checkString==str.Substring(j,checkString.Length)){
haveCycles = true;
countOfCycles++;
}else{
haveCycles =false;
}
}
if(haveCycles && countOfCycles == Math.Ceiling((double)(str.Length/checkString.Length))-1){
return true;
}
}
return false;
}
}
check the cyclic behavior by creating substrigs and comparing them with all the parts of the given string

Console application that validates address [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 5 years ago.
Improve this question
So I'm trying to figure out how to build a Console Application in C# that essentially mimics google maps. I need to be able to enter an address (even if I abbreviate a word. EX: Silverbell Dr) and output the correct spelling (Silverbell Drive).
The goal here is to be able to enter an address in the search, even if the address spelling is incorrect, and output a value that is close enough to the user input that it won't send back a null value.
If anyone has anything that is similar to this, I would greatly appreciate the help!
Boss gave me this assignment knowing that I hardly have a base knowledge on the subject
One way to do it would be to use Googles geocode API. You can pass it a partial address and it will do it's best to return the normalized address for you. If the address isn't very specific, you will get back more than one.
Here's a code example for calling the API and parsing the results:
private static List<string> GetNormalizedAddresses(string address)
{
// Generate request Uri
var baseUrl = "http://maps.googleapis.com/maps/api/geocode/xml";
var requestUri = $"{baseUrl}?address={Uri.EscapeDataString(address)}&sensor=false";
// Get response
var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xDoc = XDocument.Load(response.GetResponseStream());
var results = xDoc.Element("GeocodeResponse")?.Elements("result").ToList();
var normalizedAddresses = new List<string>();
// Populate results
if (results != null)
{
normalizedAddresses.AddRange(results
.Select(result => result.Element("formatted_address")?.Value)
.Where(formattedAddress => !string.IsNullOrWhiteSpace(formattedAddress)));
}
return normalizedAddresses;
}
Then, you could call it like so:
while(true)
{
Console.Write("Enter a partial address: ");
var partialAddress = Console.ReadLine();
Console.WriteLine(new string ('-', 25 + partialAddress.Length));
var normalizedAddress = GetNormalizedAddresses(partialAddress);
if (!normalizedAddress.Any())
{
Console.WriteLine("Sorry, couldn't find anything.");
}
else
{
Console.WriteLine("That address normalizes to:");
Console.WriteLine($" - {string.Join($"\n - ", normalizedAddress)}");
}
Console.WriteLine("\n");
}
Output

How do I "Unit Test" in C#? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I have this method in my project:
public static String MD5Hash(string TextToHash)
{
if ((TextToHash == null) || (TextToHash.Length == 0))
{
return String.Empty;
}
MD5 md5 = new MD5CryptoServiceProvider();
byte[] textToHash = Encoding.Default.GetBytes(TextToHash);
byte[] result = md5.ComputeHash(textToHash);
return System.BitConverter.ToString(result);
}
And I've tried testing like this:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BugMon;
namespace BugMonTesting
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
string pwd = "Password";
string expected = "DC-64-7E-B6-5E-67-11-E1-55-37-52-18-21-2B-39-64";
frmLogIn.MD5Hash(pwd);
Assert.AreEqual(pwd, expected);
}
}
}
But the string pwd does not seem to be passing through the Method when I run the test and stays as "Password".
What am I doing wrong?
Sorry if this is obvious but I've never had to use these tests before.
You're never doing anything with the return value from MD5Hash.
Try this:
string hash = frmLogIn.MD5Hash(pwd);
Assert.AreEqual(hash, expected);
Note that this will only work if MD5Hash returns a string formatted like the expected variable.

program not updating database [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Im trying to create a form that allows users to edit a value in the database. My code below runs without error however
This is the calling method
private void BTNSavePool_Click(object sender, EventArgs e)
{
RWCStatTracker.Database.CLSDB.EditPool(TXTEditPool.Text, CMBBXSearchPool.SelectedItem.ToString());
CMBBXSearchPool.Text = "Please select a Pool....";
MessageBox.Show("Pool edited", "Alert");
this.FRMEditPool_Load(this, null);
PNLEditPoolSearch.Show();
}
This is the code in my database connection class
public static void EditPool(String OldName, String NewName)
{
string UPDTStmt = "UPDATE TBL_Pool SET Name = #NewName WHERE Name = #OldName";
SqlConnection conn = GetConnection();
SqlCommand UPDTCmd = new SqlCommand(UPDTStmt, conn);
UPDTCmd.Parameters.AddWithValue("#NewName", NewName);
UPDTCmd.Parameters.AddWithValue("#OldName", OldName);
try { conn.Open(); UPDTCmd.ExecuteNonQuery(); }
catch (SqlException ex) { throw ex; }
finally { conn.Close(); }
}
Any ideas why it's not updating?
Most likely the WHERE clause isn't selecting any records.
It may have spaces: "Smith " will not match "Smith" or it may be case sensitive: "Smith" will not match "smith".
Check the data to see if it has spaces and string trim your parameters.
I've just figured out the issue with it, the arguments are in the wrong order
This
public static void EditPool(String OldName, String NewName)
Should be
public static void EditPool(String NewName, String OldName)

How do I get the duration of a video file using C#? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Any library in C# that allows me to do that?
google result for http://johndyer.name/post/2005/07/22/Retreiving-the-duration-of-a-WMV-in-C.aspx
using WMPLib; // this file is called Interop.WMPLib.dll
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia("myfile.wmv");
// write duration
Console.WriteLine("Duration = " + mediaInfo.duration);
// write named attributes
for (int i=0; i<mediaInfo.attributeCount; i++)
{
Console.WriteLine(mediaInfo.getAttributeName(i) + " = " + mediaInfo.getItemInfo(mediaInfo.getAttributeName(i)) );
}
You can try this Extension method.
using Shell32;
public static class Extension
{
public static string GetLength(this FileInfo info)
{
var shell = new ShellClass();
var folder = shell.NameSpace(info.DirectoryName);
var item = folder.ParseName(info.Name);
return folder.GetDetailsOf(item, 27);
}
}
I hope following code snippet will help you :
using WMPLib;
// ...your code here...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
and don't forget to add the reference of wmp.dll which will be
present in System32 folder.

Categories

Resources