Raised This Month: $ Target: $400
 0% 

[Terrible Idea] Using the same function to set/get a string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
humbugtheman
Member
Join Date: Jan 2012
Old 04-05-2016 , 17:41   [Terrible Idea] Using the same function to set/get a string
Reply With Quote #1

You shouldn't do this as it's pointless and ugly, but I put this together because I was interested in the idea. The Name function can be used to set or get a string, depending on what parameter you pass to it.
PHP Code:
// string 'type'
#define LENGTH 255
#define BUFFER "a_string_nobody_would_ever_use"

char g_Name[LENGTH];

public 
void OnPluginStart(){
    
    
// We set g_Name with the Name function
    
Name("Hello world!");
    
    
// We get g_Name with the Name function
    
char NameBuffer[LENGTH] = BUFFER;
    
Name(NameBuffer);
    
}

void Name(char[] Name){
    
    
// 'get'
    
if (IsBuffer(Name)){
        
strcopy(NameLENGTHg_Name);
    
    
// 'set'
    
} else {
        
strcopy(g_NameLENGTHName);
    }
}

bool IsBuffer(const char[] s){
    return 
StrEqual(sBUFFER);


Last edited by humbugtheman; 04-05-2016 at 17:41.
humbugtheman is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 04-06-2016 , 10:32   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #2

Well done, you made me puke!
h3bus is offline
Farbror Godis
AlliedModders Donor
Join Date: Feb 2016
Old 04-06-2016 , 11:12   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #3

That's the worst piece of code on this forums

Last edited by Farbror Godis; 04-06-2016 at 11:22.
Farbror Godis is offline
humbugtheman
Member
Join Date: Jan 2012
Old 04-06-2016 , 11:27   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #4

Where do I collect my award?
humbugtheman is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 04-06-2016 , 13:34   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #5

The thing you set to name is hardcoded...
__________________
Chdata is offline
humbugtheman
Member
Join Date: Jan 2012
Old 04-06-2016 , 14:03   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #6

Correct! It is temporarily set to a hardcoded value before being overriden by the Name function. I did warn you: "terrible idea". Just a little 'what if' concept that shouldn't actually be used.
humbugtheman is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 04-06-2016 , 14:54   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #7

Why not just use a blank string
Mitchell is offline
humbugtheman
Member
Join Date: Jan 2012
Old 04-06-2016 , 15:16   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #8

Because you might want to set the string to a blank string.
humbugtheman is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-07-2016 , 05:45   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #9

Why not pass string max length as always (default value = 0) and in the function check:

if the length is > 0 - Get
if the length is <= 0 - Set

PHP Code:
SetGetFunction(String:myString[], stringMaxLength 0)
{
    if (
stringMaxLength 0) { // get
        
strcopy(myStringstringMaxLengthSomeStringTakenFromSomewhere);
    } else { 
// set
        
strcopy(SomeStringTakenFromSomewheresizeof(SomeStringTakenFromSomewhere), myString);
    }

P.S.: If you totally want to do this sh*t...
__________________
Plugins: TeamGames
Includes: Menu stocks, ColorVariables, DownloadTableConfig

> No help through PM, make a topic.

Last edited by KissLick; 04-07-2016 at 05:45.
KissLick is offline
humbugtheman
Member
Join Date: Jan 2012
Old 04-07-2016 , 05:54   Re: [Terrible Idea] Using the same function to set/get a string
Reply With Quote #10

Yeah that would be slightly less insane.
humbugtheman is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode