pass a string by a value in function call [closed] - string

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 yesterday.
Improve this question
I would like to pass a string instead of the number. But don't know how to do that.
#define MODULE_A 5
#define MODULE_B 5
void function_2(unsigned moduleId, IdData)
{
if ( MODULE_A == moduleId) // want to compare the string instead of number 5 since both A and B are same.
{
// do something
}
if (MODULE_B == moduleId) //want to compare the string instead of number 5 since both A and B are same.
{
//do something
}
}
void function_1()
{
function_2(MODULE_B, IdData);
}
void function_3()
{
function_1(MODULE_A, IdData);
}
Welcome to do any workaround to archive the expected result. Basically, I have a function_2 that has the 1st element as unsigned integer, But when other function calling, I would like to pass the string inside each if condition but rather the value, since both module IDs are macro defined as the same number. Thank you.

Related

Is this code a good candidate for a lambda function? [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 last year.
Improve this question
I have 27 of these in my function:
auto i = m_lbLanguage.AddString(_T("Afrikaans (Afrikaans)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Afrikaans));
i = m_lbLanguage.AddString(_T("Cebuano (Cebuano)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Cebuano));
i = m_lbLanguage.AddString(_T("汉语(简化字)Chinese (Simplified)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::ChineseSimplified));
Is this type of code a good candidate to use a lambda function? I know I can add a regular function, eg:
void Cxxx::AddLanguageToLB(CString strLanguage, LanguageMSA eLanguage)
{
auto i = m_lbLanguage.AddString(strLanguage);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(eLanguage);
}
And the use:
AddLanguageToLB(_T("汉语(简化字)Chinese (Simplified)"), LanguageMSA::ChineseSimplified);
This type of code is a good candidate for a table-driven implementation. Something along these lines, perhaps:
struct {
LPCTSTR name;
LanguageMSA code;
} languages[] = {
{_T("Afrikaans (Afrikaans)"), LanguageMSA::Afrikaans},
{_T("Cebuano (Cebuano)"), LanguageMSA::Cebuano},
// ...
};
for (auto& lang : languages) {
auto i = m_lbLanguage.AddString(lang.name);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(lang.code));
}

How to set a global property on the string constructor in NodeJS? [duplicate]

This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 2 years ago.
I want to define a property on the string constructor, and I want then to access this property from any file in my project. for example:
file1.js
String.capitalize = function(){
this.//...etc
}
file2.js
"Hello nice to meet you".capitalize()
You can define a new String prototype function like in the example below
String.prototype.capitalize = function(){
return this.toUpperCase();
};
console.log("Hello nice to meet you".capitalize());

Is chaining in conditional statements idiomatic rust? [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 2 years ago.
Improve this question
Consider the following condition in a if statement, is there a more idiomatic, clean, or readable way to write this?
if some_path_to_text_maybe
.extension()
.and_then(|ext| Some(ext == "txt"))
.unwrap_or(false) {
// do something
}
I guess this is one line less:
if some_path_to_text_maybe
.extension()
.map_or(false, |ext| ext == "txt") {
// do something
}
Alternatively, you could try the two-step:
if let Some(ext) = some_path_to_text_maybe.extension() {
if ext=="txt" {
// do something
}
}

How to call a method from another class in groovy? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
For Instance, I have a class like this:
class firstOne{
....
def A (){
}
}
class secondOne{
// I need to call and use method A from class firstOne
// even I get error if I try to follow Java like calls
// firstOne method = new firstOne();
// method.A()
}
I already tried http://groovy.codehaus.org/Scripts+and+Classes and http://groovy.codehaus.org/Groovy+Beans but no way. Any kind of suggestion or examples would be really helpful.
I don't see any problem in this:
class FirstOne {
def a() {
println "a"
}
}
class SecondOne {
def b() {
new FirstOne().a()
println "b"
}
}
new FirstOne().a()
println("")
new SecondOne().b()
Output:
a
a
b
This is not specific to Groovy/Grails:
firstOne first = new firstOne()
first.A()
Also you should capitalize the first letter of classes, but not methods (as is best practice in Java).

Get the name of the current invoking method at runtime [duplicate]

This question already has answers here:
Groovy, get enclosing function's name?
(5 answers)
Closed 8 years ago.
Is it possible to get the name of the current method in Groovy code?
def myMethod() {
// want to get the string myMethod here
}
Thanks
Tim_yates gives you a link to general solution.
This is shorter way to get method name. May be optimized, leawed as is for the sake of clarity.
class A {
def myMethod() {
for (m in new Throwable().stackTrace) {
if (m.className == this.class.name)
return m.methodName
}
}
}
println new A().myMethod()

Resources